Nearby lessons

41 of 125

Java - Abstraction

📌 What You Will Learn
  • 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.

In simple words: An interface only says what to do, not how to do it. The implementing class must write the real code for every interface method.
Example01
JCode Cell
1abstract class Vehicle {
2 abstract void start(); // no body - child must give body
3 
4 void stop() { // normal method with body
5 System.out.println("Vehicle stopped");
6 }
7}
8 
9class Car extends Vehicle {
10 void start() {
11 System.out.println("Car started");
12 }
13}

Abstraction in Java — abstract class and interface

Trainer's Note: Updated knowledge (Java 8+): interfaces are no longer just empty blueprints. Since Java 8 they can have default methods and static methods with a body; since Java 9 they can have private methods too. A class can implement many interfaces (this is how Java supports multiple inheritance safely). This is covered in detail in the Java 8 chapter.
Example02
JCode Cell
1interface Bank {
2 void deposit(double amount);
3 void withdraw(double amount);
4}
5 
6class SBI implements Bank {
7 public void deposit(double amount) {
8 System.out.println("Deposited in SBI: " + amount);
9 }
10 public void withdraw(double amount) {
11 System.out.println("Withdrawn from SBI: " + amount);
12 }
13}

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).

Trainer's Note: What the reference type controls: if you declare A a, you can call only the members of A. If you declare B b, you can call everything (A's members + B's own members). This 'reference vs object' idea is central to interfaces too.
Example03
JCode Cell
1abstract class A {
2 void m1() { System.out.println("m1-A"); }
3 abstract void m2();
4}
5 
6class B extends A {
7 void m2() { System.out.println("m2-B"); }
8 void m4() { System.out.println("m4-B"); }
9}
10 
11class Test {
12 public static void main(String[] args) {
13 A a = new B(); // reference = abstract class, object = sub class
14 a.m1();
15 a.m2();
16 // a.m4(); // ERROR - m4 is not in the abstract class
17 
18 B b = new B(); // reference = sub class
19 b.m4(); // OK
20 }
21}

Concrete vs Abstract Methods — Question and Answer

PointConcrete methodAbstract method
BodyHas declaration AND implementationOnly declaration, ends with ;
KeywordNone neededabstract keyword must be used
Allowed inClasses, abstract classesAbstract classes, interfaces
SharabilityLessMore
Example04
JCode Cell
1void add(int i, int j) { // concrete - has body
2 System.out.println(i + j);
3}
4 
5abstract void add(int i, int j); // abstract - no body
📝 Key Takeaways
  • 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).

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1