Java Abstract Class, Interface, Inheritance & Polymorphism
1. Abstract Class in Java
Definition
An Abstract Class is a class declared using the keyword abstract.
It may contain abstract methods (without body) as well as normal methods
(with body).
An abstract class cannot be instantiated (you cannot create objects of it directly).
However, it can be extended by subclasses, and the subclasses must implement its
abstract methods.
Syntax
abstract class ClassName
{
abstract void methodName(); // abstract method
void normalMethod()
{
// implementation
}
}
Important Points about Abstract Class
1. An abstract class may contain abstract methods.
2. We cannot create objects of an abstract class.
3. It can contain a mixture of abstract and non-abstract methods.
4. A subclass must implement all abstract methods of the abstract class.
5. Abstract classes are mainly used for achieving abstraction.
2. Example: Abstract Class
Program Example
abstract class A
{
abstract void callme(); // abstract method
void callmetoo()
{
[Link]("Implementation of callmetoo method of class A");
}
}
class B extends A
{
void callme()
{
[Link]("Implementation of callme of class B");
}
}
class AbstractDemo
{
public static void main(String args[])
{
B b = new B();
[Link]();
[Link]();
}
}
Output
Implementation of callme of class B
Implementation of callmetoo method of class A
Explanation
● Class A is abstract.
● It contains:
○ one abstract method (callme)
○ one normal method (callmetoo)
● Class B extends class A and implements the abstract method.
3. Abstract Class Example – Area Calculation
Abstract Class
abstract class Figure
{
double dim1;
double dim2;
Figure(double a, double b)
{
dim1 = a;
dim2 = b;
}
abstract double area();
}
Rectangle Class
class Rectangle extends Figure
{
Rectangle(double a, double b)
{
super(a, b);
}
double area()
{
[Link]("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
Triangle Class
class Triangle extends Figure
{
Triangle(double a, double b)
{
super(a, b);
}
double area()
{
[Link]("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
Main Class
class DemoAreas
{
public static void main(String args[])
{
Rectangle r = new Rectangle(10, 10);
Triangle t = new Triangle(10, 8);
Figure figref; //reference variable of abstract class Figure.[It can refer to
objects of its subclasses.]
figref = r;
[Link]("Area is " + [Link]());
figref = t;
[Link]("Area is " + [Link]());
}
}
Output
Inside Area for Rectangle.
Area is 100
Inside Area for Triangle.
Area is 40
Concept Demonstrated
● Abstract method overriding
● Runtime polymorphism
4. Interface in Java
Definition
An Interface in Java is a blueprint of a class.
It contains:
● Abstract methods
● Static final constants
Interfaces are used to achieve complete abstraction.
Syntax
interface InterfaceName
{
int x = 10; // constant
void display(); // abstract method
}
5. Characteristics of Interface
Similar to a Class
1. Interface can contain multiple methods.
2. It is saved in .java file.
3. Compiled interface produces .class file.
Different from a Class
1. We cannot create objects of an interface.
2. Interface does not have constructors.
3. All methods are abstract by default.
4. Variables are public, static and final.
5. A class implements an interface.
6. Example of Interface
Interface
interface Sample
{
int a = 10;
void display();
}
Class Implementing Interface
class TestClass implements Sample
{
public void display()
{
[Link]("Value of a = " + a);
}
public static void main(String args[])
{
TestClass t = new TestClass();
[Link]();
}
}
Output
Value of a = 10
7. Multiple Inheritance Using Interface
Java does not support multiple inheritance with classes.
However, it supports multiple inheritance through interfaces.
Example
Interface 1
↑
Interface 2
↑
Class
A class can implement multiple interfaces.
8. Example Problem (Cylinder Volume)
Interface
Interface: Data
Data Member: pi = 3.142
Method: compute()
Base Class
Class Base
Data Member: radius
Method: compute() → area of circle
Derived Class
Class CalVol
Data Member: height
Method: compute() → volume of cylinder
Formula
Volume of Cylinder:
Volume = π × r² × h
Java Program: Area of Circle and Volume of
Cylinder Using Interface
interface Data
{
double pi = 3.142;
void compute();
}
class Base implements Data
{
double radius;
Base(double r)
{
radius = r;
}
public void compute()
{
double area;
area = pi * radius * radius;
[Link]("Area of Circle = " + area);
}
}
class CalVol extends Base
{
double height;
CalVol(double r, double h)
{
super(r);
height = h;
}
public void compute()
{
double volume;
volume = pi * radius * radius * height;
[Link]("Volume of Cylinder = " + volume);
}
}
class TestCylinder
{
public static void main(String args[])
{
Base b = new Base(5);
[Link]();
CalVol c = new CalVol(5,10);
[Link]();
}
}
Output (Example)
Area of Circle = 78.55
Volume of Cylinder = 785.5
9. Polymorphism in Java
Definition
The word Polymorphism comes from two Greek words:
● Poly → many
● Morph → forms
Polymorphism means one method behaving in different ways.
Example:
area()
Different shapes:
Circle → area()
Triangle → area()
Rectangle → area()
Each object responds differently.
class Shape
void area()
{
[Link]("Area of shape");
class Circle extends Shape
double r;
Circle(double radius)
r = radius;
void area()
double a = 3.14 * r * r;
[Link]("Area of Circle = " + a);
class Triangle extends Shape
{
double b, h;
Triangle(double base, double height)
b = base;
h = height;
void area()
double a = (b * h) / 2;
[Link]("Area of Triangle = " + a);
class Rectangle extends Shape
double l, w;
Rectangle(double length, double width)
{
l = length;
w = width;
void area()
double a = l * w;
[Link]("Area of Rectangle = " + a);
class TestPoly
public static void main(String args[])
Shape s;
s = new Circle(5);
[Link]();
s = new Triangle(4,6);
[Link]();
s = new Rectangle(5,3);
[Link]();
OUTPUT
Area of Circle = 78.5
Area of Triangle = 12.0
Area of Rectangle = 15.0
10. Types of Polymorphism
Polymorphism
|
-------------------------
| |
Compile Time Run Time
Polymorphism Polymorphism
1. Compile Time Polymorphism
● Method Overloading
● Operator Overloading
2. Runtime Polymorphism
● Method Overriding
11. Function Overloading vs Function Overriding
Function Overloading Function Overriding
Same method name in the same Same method name in different
class classes
Method signature must be Method signature must be same
different
Compile time polymorphism Runtime polymorphism
Does not require inheritance Requires inheritance
12. Multiple Inheritance vs Interface
Multiple Inheritance Interface
Not supported in Java Achieved using
classes interfaces
Causes ambiguity Avoids ambiguity
problem