Nearby lessons
41 of 125Java - Abstraction
- What class and object mean, with real-life examples
- The four pillars: Encapsulation, Abstraction, Inheritance, Polymorphism
- Constructors, this, super, static and final
- Method overloading and method overriding (with a comparison table)
- Abstract classes and interfaces (including the modern changes)
- Association, Aggregation and Composition
Abstraction is a core concept of the Java language. This lesson explains Abstraction in Java — abstract class and interface, Abstract Class — The Full Procedure and Concrete vs Abstract Methods — Question and Answer with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Abstraction in Java — abstract class and interface
Abstract class
An abstract class is a class that cannot create objects (cannot be instantiated). It may contain some complete methods and some abstract methods (methods with no body). A child class must provide the body of the abstract methods.
Interface
An interface is a 100% blueprint — it only declares what to do (the contract), not how to do it. A class implements the interface and gives the actual code.
Abstraction in Java — abstract class and interface
Abstract Class — The Full Procedure
The classic procedure for using an abstract class has six steps:
- Declare the abstract class with the abstract keyword.
- Declare concrete and abstract methods as needed.
- Declare a sub class for the abstract class (extends).
- Implement all abstract methods in the sub class.
- In main(), create the object of the sub class, but declare the reference as the abstract class OR the sub class.
- Access the members.
Key point: abstract class A cannot be created with new A() — you can only declare a reference variable (A a = null; is fine, new A() is an error).
Concrete vs Abstract Methods — Question and Answer
| Point | Concrete method | Abstract method |
|---|---|---|
| Body | Has declaration AND implementation | Only declaration, ends with ; |
| Keyword | None needed | abstract keyword must be used |
| Allowed in | Classes, abstract classes | Abstract classes, interfaces |
| Sharability | Less | More |
- Class = blueprint; Object = real thing made from it (memory in heap).
- Four pillars: Encapsulation (hide data), Abstraction (show only essentials), Inheritance (reuse parent code), Polymorphism (one name, many forms).
- Overloading = same class, different parameters, decided at compile time. Overriding = child redefines parent method, decided at run time.
- Constructor initialises the object; name = class name, no return type.
- this = current object; super = parent class; static = belongs to class; final = cannot change.
- abstract class cannot be instantiated; interface is a contract a class implements.
- Association (weak), Aggregation (has-a, part independent), Composition (strong has-a, part dies with whole).