ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Advanced Computer Programming
    COMP3114
    Progress0 / 12 topics
    Topics
    1. Java API: Abstract classes and Interfaces2. Packages and Exception handling3. Advanced issues of GUI and event handling4. Applets and Swing5. Network Programming Concepts: JDBC6. Multithreading7. Building Client/Server and implementing protocols8. RMI (Remote Method Invocation)9. Java Secure Socket Extension and Secure Sockets Layer (SSL)10. SSL Socket and SSL Server Socket classes11. Client and Server Authentication: HTTPS12. Developing TCP/IP client and server with telnet
    COMP3114›Java API: Abstract classes and Interfaces
    Advanced Computer ProgrammingTopic 1 of 12

    Java API: Abstract classes and Interfaces

    4 minread
    679words
    Beginnerlevel

    📘 Java API: Abstract Classes and Interfaces


    🔹 1. Definition

    ✅ Abstract Class

    An abstract class in Java is a class that cannot be instantiated (object cannot be created) and is used as a base class for other classes.

    • It may contain:

      • Abstract methods (without body)
      • Concrete methods (with body)
    • Declared using the keyword abstract


    ✅ Interface

    An interface is a completely abstract blueprint of a class that contains only abstract methods (by default) and constants.

    • Used to achieve full abstraction and multiple inheritance
    • Declared using the keyword interface

    🔹 2. Key Concepts

    ✨ Abstract Class Features

    • Cannot create objects directly

    • Can have:

      • Constructors
      • Instance variables
      • Both abstract and non-abstract methods
    • Supports single inheritance

    • Subclass must implement all abstract methods


    ✨ Interface Features

    • All methods are public and abstract (by default)
    • Variables are public, static, and final
    • No constructors
    • Supports multiple inheritance
    • A class implements an interface using implements

    🔹 3. Syntax and Examples

    🧩 Abstract Class Example

    abstract class Animal {
        abstract void sound();   // abstract method
    
        void sleep() {           // concrete method
            System.out.println("Sleeping...");
        }
    }
    
    class Dog extends Animal {
        void sound() {
            System.out.println("Barking");
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Dog d = new Dog();
            d.sound();
            d.sleep();
        }
    }
    

    ✔ Explanation (Step-by-step)

    1. Animal is an abstract class.

    2. It has:

      • sound() → abstract
      • sleep() → concrete
    3. Dog inherits Animal using extends.

    4. Dog must override sound().

    5. Object of Dog is created (not Animal).


    🧩 Interface Example

    interface Vehicle {
        void start();
    }
    
    class Car implements Vehicle {
        public void start() {
            System.out.println("Car starts");
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Car c = new Car();
            c.start();
        }
    }
    

    ✔ Explanation

    1. Vehicle is an interface.
    2. Car implements it using implements.
    3. Method start() must be defined in Car.

    🔹 4. Abstract Class vs Interface

    Feature Abstract Class Interface
    Methods Abstract + Concrete Mostly Abstract
    Variables Normal variables allowed Only constants
    Inheritance Single Multiple
    Constructors Allowed Not allowed
    Keyword extends implements

    🔹 5. Important Rules

    📌 Abstract Class Rules

    • Must use abstract keyword
    • Cannot create object directly
    • Can have constructor
    • If a class contains abstract method → must be abstract

    📌 Interface Rules

    • Methods are public abstract by default
    • Variables are public static final
    • A class can implement multiple interfaces
    • Use implements keyword

    🔹 6. When to Use What?

    ✔ Use Abstract Class when:

    • You want common base functionality
    • Classes share some behavior

    ✔ Use Interface when:

    • You want only method declarations
    • You need multiple inheritance

    🔹 7. Multiple Inheritance Using Interface

    interface A {
        void show();
    }
    
    interface B {
        void display();
    }
    
    class Test implements A, B {
        public void show() {
            System.out.println("Show method");
        }
    
        public void display() {
            System.out.println("Display method");
        }
    }
    

    ✔ Java allows multiple inheritance using interfaces (not classes)


    🔹 8. Diagram Description (for exams)

    📊 Abstract Class Diagram (Description)

    • A base class Animal
    • Derived class Dog
    • Arrow showing inheritance (Dog → Animal)
    • Abstract method in base, implemented in child

    📊 Interface Diagram (Description)

    • Interface Vehicle
    • Class Car
    • Dotted arrow showing implementation (Car → Vehicle)
    • Method defined in class

    🔹 9. Important Points to Remember

    • Abstract class = partial abstraction
    • Interface = full abstraction
    • Abstract class uses extends
    • Interface uses implements
    • Interface supports multiple inheritance

    📝 Likely Exam Questions

    1. Define an abstract class with example.
    2. What is an interface in Java? Explain with syntax.
    3. Difference between abstract class and interface.
    4. Can we create object of abstract class? Why?
    5. Explain multiple inheritance using interface.
    6. Write a Java program using abstract class.
    7. Write a program implementing an interface.
    8. What are rules of interfaces in Java?
    9. When should you use abstract class instead of interface?

    📌 Quick Revision Summary

    • Abstract Class

      • Cannot instantiate
      • Has abstract + concrete methods
      • Supports single inheritance
    • Interface

      • Only method declarations
      • Supports multiple inheritance
      • Implemented using implements
    • Key Difference

      • Abstract class = partial abstraction
      • Interface = full abstraction

    Next topic 2
    Packages and Exception handling

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count679
      Code examples0
      DifficultyBeginner