ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3114Java Api Abstract Clas…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3114

    Advanced Computer Programming

    Progress0 / 12 done
    Topics12
    1Abstract classes and Interfaces2Packages and Exception handling3Advanced issues of GUI and event handling4Applets and Swing
    5JDBC6Multithreading7Building Client/Server and implementing protocols8RMI (Remote Method Invocation)9Java Secure Socket Extension and Secure Sockets Layer (SSL)10SSL Socket and SSL Server Socket classes
    11HTTPS12Developing TCP/IP client and server with telnet
    4 min read
    Topic 1 of 12
    Topic Study Note Illustration
    01

    Java API: Abstract classes and Interfaces

    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.

    πŸ“˜ 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)

    Class diagram
    Visual ReferenceClass
    Wikipedia
    • 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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    683words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Computer Programming