1. It's an Idea, Not a Thing:
You can have a Vehicle concept, but you can only drive a Real Car. You cannot "new up" an abstract class.
2. The "Must-Do" List:
Abstract methods are promises. They say: "I don't know HOW this works yet, but any child class MUST figure it out."
3. Shared DNA:
It can still hold common traits (like a color or fuelLevel) so you don't have to rewrite them for every child.
// The Template
public abstract class Vehicle {
// Shared behavior (All vehicles do this the same)
public void honk() {
System.out.println("Beep!");
}
// The Requirement (Every vehicle does this differently!)
public abstract void move();
}
// The Real Version
public class Car extends Vehicle {
public void move() {
System.out.println("Driving on four wheels");
}
}
public class AbstractClass {
public void main(String[] args) {
Student IS247=new Student();
}
public abstract class Student{
}
}
// Cat class implementing makeSound()
class Cat extends Animal {
void makeSound() {
System.out.println("Meow! Meow!");
}
}
1. A 100% Empty Template:
Unlike abstract classes, interfaces usually have no code at all—only a list of "promises" (methods).
2. The "All or Nothing" Rule:
If a class signs the contract (implements), it must provide code for every single method listed.
3. Multiple Superpowers:
In Java, a class can only have one parent (class), but it can sign many contracts (interfaces).
e.g., A SmartPhone is a Phone, but it also implements 'Camera', 'GPS', and 'MusicPlayer'.
interface PowerSwitch {
void turnOn(); // No body, just a promise
}
// Marketing signs the 'Revenue' contract
class Marketing implements Revenue, PowerSwitch {
public void turnOn() {
System.out.println("Lights on in Marketing!");
}
}
"Interfaces define behavior (what it can do), not identity (what it is)."
Github code sample Abstract/Interface
class Cat implements Animal {
public void makeSound() {
System.out.println("Meow! Meow!");
}
}
Github code sample Abstract
Github code sample Interface
source:media.geeksforgeeks.org
abstract class Shape{}
class Circle extends Shape{}
interface Shape{}
class Circle implements Shape{}
| Feature | Abstract Class | Interface |
|---|---|---|
| Definition | A class that can have both abstract and concrete methods. | A blueprint that only contains abstract methods (until Java 8, which introduced default methods). |
| Method Implementation | Can have both abstract and fully implemented methods. | Only abstract methods (default and static methods were added in Java 8). |
| Variables | Can have instance variables (fields) with any access modifier. | Only public, static, and final variables are allowed. |
| Access Modifiers | Methods can be public, protected, or private. | Methods are public by default. |
| Multiple Inheritance | A class can extend only one abstract class. | A class can implement multiple interfaces. |
| Constructors | Can have constructors. | Cannot have constructors. |
| Use Case | Used when classes share behavior but also need customization. | Used for defining contracts that multiple unrelated classes must follow. |
1. Shared Identity (DNA):
Use it when classes are closely related.
Example: A Person class sharing name, dob, and gender with Student and Employee.
2. The "Semi-Finished" Product:
Use it when you want to provide some working code but leave the rest for the children to finish.
3. Privacy Matters:
Unlike Interfaces, Abstract classes allow private and protected data. This is Encapsulation—keeping the internal "guts" of the family business safe.
"If you can say 'Subclass IS A Parent' and they share actual attributes (not just behaviors), go Abstract."
// --- THE IDENTITY (Abstract Class) ---
// Use this for "Shared DNA"
abstract class Animal {
String name;
Animal(String name) { this.name = name; }
// All animals breathe the same way (Implemented)
void breathe() {
System.out.println(name + " takes a breath...");
}
// Every animal makes a different sound (Abstract)
abstract void makeNoise();
}
// --- THE SKILLSETS (Interfaces) ---
// Use these for "Extra Abilities"
interface Pet {
void play(); // Only some animals are pets
}
interface Flyable {
void fly(); // Only some animals fly
}
// --- THE CONCRETE REALITY ---
// A Dog IS-AN Animal and CAN-BE a Pet
class Dog extends Animal implements Pet {
Dog(String name) { super(name); }
@Override
void makeNoise() { System.out.println("Woof!"); }
@Override
public void play() { System.out.println(name + " fetches the ball."); }
}
// An Eagle IS-AN Animal and CAN-FLY
class Eagle extends Animal implements Flyable {
Eagle(String name) { super(name); }
@Override
void makeNoise() { System.out.println("Screech!"); }
@Override
public void fly() { System.out.println(name + " soars through the clouds."); }
}
Github code sample Abstract