Inheritance in Java
Definition:
Inheritance is a fundamental concept in object-oriented programming where a class (called
the subclass or child class) inherits fields and methods from another class (called the
superclass or parent class).
This promotes code reusability, method overriding, and a hierarchical classification.
Types of Inheritance in Java:
Type Description Supported in Java
One class inherits from another
1. Single Inheritance Yes
class.
2. Multilevel A class inherits from a class
Yes
Inheritance which inherits from another class.
3. Hierarchical Multiple classes inherit from the
Yes
Inheritance same superclass.
4. Multiple
One class inherits from more than Not supported with classes
Inheritance (with
one class. (ambiguity problem)
classes)
Combination of two or more Not fully supported, but can
5. Hybrid Inheritance
types of inheritance. be achieved using interfaces
Single Inheritance – Definition:
In Single Inheritance, a child class inherits the properties and behaviors (fields and
methods) of a single parent class.
This allows code reuse and promotes cleaner design.
Example:
Manager inherits from Employee → Manager gets access to work() from Employee.
class Employee {
void work() {
[Link]("Employee is working.");
}
}
class Manager extends Employee {
void manageTeam() {
[Link]("Manager is managing the team.");
}
}
public class Main {
public static void main(String[] args) {
Manager m = new Manager();
[Link](); // Inherited from Employee
[Link](); // Manager's own method
}
}
Multilevel Inheritance – Definition:
In Multilevel Inheritance, a class is derived from a child class which is already derived from
another class.
This forms a chain of inheritance.
Example:
PremiumSavingsAccount → inherits from SavingsAccount → which inherits from Account.
class Account {
void openAccount() {
[Link]("Account opened.");
}
}
class SavingsAccount extends Account {
void calculateInterest() {
[Link]("Interest calculated for Savings Account.");
}
}
class PremiumSavingsAccount extends SavingsAccount {
void offerPremiumBenefits() {
[Link]("Premium benefits offered.");
}
}
public class Main {
public static void main(String[] args) {
PremiumSavingsAccount p = new PremiumSavingsAccount();
[Link](); // From Account
[Link](); // From SavingsAccount
[Link]();// Own method
}
}
3. Hierarchical Inheritance – Definition:
In Hierarchical Inheritance, multiple child classes inherit from the same parent class.
This allows common functionality to be written once in the parent and shared across
subclasses.
Example:
Car and Bike both inherit from Vehicle → they share start() behavior.
class Vehicle {
void start() {
[Link]("Vehicle started.");
}
}
class Car extends Vehicle {
void driveCar() {
[Link]("Car is being driven.");
}
}
class Bike extends Vehicle {
void rideBike() {
[Link]("Bike is being ridden.");
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
[Link]();
[Link]();
Bike b = new Bike();
[Link]();
[Link]();
}
}
4. Multiple Inheritance (via Interface) – Definition:
Java does not support multiple inheritance with classes to avoid ambiguity (known as the
Diamond Problem).
However, Java supports multiple inheritance through interfaces — a class can implement
multiple interfaces.
Example:
MultiFunctionMachine implements both Printer and Scanner interfaces.
interface Printer {
void print();
}
interface Scanner {
void scan();
}
class MultiFunctionMachine implements Printer, Scanner {
public void print() {
[Link]("Printing document...");
}
public void scan() {
[Link]("Scanning document...");
}
}
public class Main {
public static void main(String[] args) {
MultiFunctionMachine m = new MultiFunctionMachine();
[Link]();
[Link]();
}
}
5. Hybrid Inheritance – Definition:
Hybrid Inheritance is a combination of two or more types of inheritance (e.g., multilevel
+ multiple).
Though Java doesn't fully support hybrid inheritance with classes, it can be achieved using
interfaces + class inheritance.
Hybrid inheritance is a combination of multiple and other types of inheritance (like
multilevel or hierarchical).
Java does not support multiple inheritance with classes to avoid ambiguity — especially
when two parent classes have methods with the same name.
This is known as the Diamond Problem.
Example Showing the Problem (Diamond Problem):
class A {
void show() {
[Link]("Class A");
}
}
class B extends A {
void show() {
[Link]("Class B");
}
}
class C extends A {
void show() {
[Link]("Class C");
}
}
// Java does NOT allow this:
class D extends B, C { // Compilation error!
void display() {
show(); // Which 'show()' to call? B or C? → Ambiguity
}}
Feature Classes Interfaces
Multiple Inheritance Not allowed Allowed
Hybrid Inheritance Not with classes Possible using interfaces
Hybrid Inheritance using Interface – Example
interface Device {
void powerOn();
}
interface Connectivity {
void connectToWiFi();
}
// Class implementing one interface
class Laptop implements Device {
public void powerOn() {
[Link]("Laptop is powering on.");
}
}
// Another class implementing one interface
class SmartDevice implements Connectivity {
public void connectToWiFi() {
[Link]("Smart device connected to WiFi.");
}
}
// Hybrid Inheritance: MyGadget gets features from both
class MyGadget extends Laptop implements Connectivity {
public void connectToWiFi() {
[Link]("MyGadget connected to WiFi.");
}
void displayFeatures() {
powerOn();
connectToWiFi();
}
}
public class Main {
public static void main(String[] args) {
MyGadget g = new MyGadget();
[Link]();
}
}
Explanation:
• Device and Connectivity are two separate interfaces.
• Laptop implements Device.
• MyGadget extends Laptop (inherits class) and also implements Connectivity
(inherits interface).
• This forms a hybrid of:
o Single inheritance (class-to-class)
o Multiple inheritance via interface
Interface in Java
• An interface in java is a blueprint of a class.
• It has static constants and abstract methods only.
• The interface in java is a mechanism to achieve fully abstraction.
• There can be only abstract methods in the java interface not
method body. It is used to achieve fully abstraction and multiple
inheritance in Java.
• Java Interface also represents IS-A relationship
Why use Java interface?
• It is used to achieve fully abstraction.
• By interface, we can support the functionality of multiple
inheritance.
Understanding relationship between classes and interfaces
• a class extends another class,
• an interface extends another interface
• but a class implements an interface.
Example
interface printable
{
void print();
}
class A6 implements printable
{
public void print()
{
[Link]("Hello");
}
public static void main(String args[])
{
A6 obj = new A6();
[Link]();
}
}
Example
interface Drawable
{
int a=10;
void draw();
}
class Rectangle implements Drawable
{
public void draw()
{
[Link]("drawing rectangle");
}
}
class Circle implements Drawable
{
public void draw()
{
[Link]("drawing circle");
[Link](a);
}
}
class TestInterface1
{
public static void main(String args[])
{
Drawable d=new Circle();
[Link]();
}}
Example
interface Printable
{
void print();
}
interface Showable extends Printable
{
void show();
}
class TestInterface4 implements Showable
{
public void print()
{
[Link]("Hello");
}
public void show()
{
[Link]("Welcome");
}
public static void main(String args[])
{
TestInterface4 obj = new TestInterface4();
[Link]();
[Link]();
}
}
Example
interface Drawable
{
void draw();
static int cube(int x)
{
return x*x*x;
}
}
class Rectangle implements Drawable
{
public void draw()
{
[Link]("drawing rectangle");
}
}
class TestInterfaceStatic
{
public static void main(String args[])
{
Drawable d=new Rectangle();
[Link]();
[Link]([Link](3));
[Link]([Link](5));
[Link]([Link](10));
[Link]([Link](100));
}}