Inheritance in Java
Definition: Inheritance is an important feature of Object-Oriented Programming (OOP) in which one class acquires the properties and behaviors (fields and methods) of another class.
It helps in:
Code reusability
Hierarchical classification
Easy maintenance
Extensibility
Superclass and Subclass
Superclass (Parent/Base class) → Class whose properties are inherited.
Subclass (Child/Derived class) → Class that inherits from another class.
extends keyword is used for inheritance.
Child class can access public and protected members of parent class.
Object of child class can use parent methods.
Example:
class Parent {
int x = 10;
void display() {
[Link]("Parent Method");
class Child extends Parent {
void show() {
[Link]("Child Method");
}
Advantages of Inheritance:
Code reusability
Reduces duplication
Improves maintainability
Supports method overriding
Helps achieve polymorphism
Types of Inheritance in Java:
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance (through interfaces)
5. Hybrid Inheritance
1. Single Inheritance: When one child class inherits from one parent class, it is called single inheritance.
Parent
Child
Example:
class Animal {
void eat() {
[Link]("Animal eats food");
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
class Demo {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
Output
Animal eats food
Dog barks
Advantages:
Simple and easy to understand
Promotes code reuse
2. Multilevel Inheritance: When a class inherits from a derived class, it is called multilevel inheritance.
Grandparent
Parent
Child
Example:
class Animal {
void eat() {
[Link]("Animal eats");
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
class Puppy extends Dog {
void weep() {
[Link]("Puppy weeps");
class Demo {
public static void main(String[] args) {
Puppy p = new Puppy();
[Link]();
[Link]();
[Link]();
Output:
Animal eats
Dog barks
Puppy weeps
Advantages:
Supports gradual specialization
Enables deep inheritance hierarchy
3. Hierarchical Inheritance: When multiple child classes inherit from one parent class, it is called hierarchical inheritance.
Animal
/\
Cat Dog
Example:
class Animal {
void eat() {
[Link]("Animal eats");
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
class Cat extends Animal {
void meow() {
[Link]("Cat meows");
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
Cat c = new Cat();
[Link]();
[Link]();
Output:
Animal eats
Dog barks
Animal eats
Cat meows
4. Multiple Inheritance (Using Interfaces in Java): Multiple inheritance means a class can inherit properties and methods from more than one parent.
Java does not support multiple inheritance using classes because if two parent classes contain the same method, the child class becomes confused about which method should be
inherited. This confusion is called the Diamond Problem or Ambiguity Problem.
To avoid this problem, Java does not allow multiple inheritance with classes. Instead, Java supports multiple inheritance using interfaces. A class can implement more than one
interface and use their methods without ambiguity.
Example using Interface:
interface Animal {
void eat();
interface Pet {
void play();
class Dog implements Animal, Pet {
public void eat() {
[Link]("Dog eats");
public void play() {
[Link]("Dog plays");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
5. Hybrid Inheritance:
Hybrid inheritance is a combination of two or more types of inheritance, such as single, multiple, hierarchical, and multilevel inheritance.
Example:
class Animal {
void eat() {
[Link]("Animal eats");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
[Link]("Puppy weeps");
}
}
class Cat extends Animal {
void meow() {
[Link]("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Puppy p = new Puppy();
[Link]();
[Link]();
[Link]();
Cat c = new Cat();
[Link]();
[Link]();
}
}
OUTPUT:
Animal eats
Dog barks
Puppy weeps
Animal eats
Cat meows
Visibility Modifiers (Access Specifiers):
Visibility modifiers (also called access specifiers) are keywords in Java that define the accessibility (visibility) of classes, methods, and variables. They control where a member of a
class can be accessed from.
Java provides four main access specifiers:
1. private:
The private members are accessible only within the same class. They cannot be accessed from outside the class.
2. default (no keyword):
If no access modifier is specified, it is called default access. The members are accessible only within the same package.
3. protected:
The protected members are accessible within the same package and also in subclasses (even if they are in a different package).
4. public:
The public members are accessible from anywhere in the program.
private → only within class
default → within package
protected → package + subclasses
public → everywhere
Modifier Accessible Within Class Same Package Subclass
Everywhere
private Yes No No No
default Yes Yes No No
protected Yes Yes Yes No
public Yes Yes Yes Yes
Interface: An interface in Java is a blueprint of a class.
It contains abstract methods that must be implemented by another class.
A class implements an interface using the implements keyword.
Interfaces are mainly used for:
Abstraction
Multiple inheritance
Interface methods do not have a body
A class must implement all methods of the interface
Interface supports multiple inheritance
We cannot create objects of an interface
Syntax:
interface InterfaceName
{
void methodName();
}
Example1:
interface Animal {
void eat();
}
class Dog implements Animal {
public void eat() {
[Link]("Dog eats food");
}
}
public class Main {
public static void main(String args[]) {
Dog d = new Dog();
[Link]();
}
}
OUTPUT:
Dog eats food
Example2:
interface Animal {
void eat();
}
interface Pet {
void play();
}
class Dog implements Animal, Pet {
public void eat() {
[Link]("Dog eats");
}
public void play() {
[Link]("Dog plays");
}
}
Output
Dog eats
Dog plays
Polymorphism: Polymorphism means “many forms”.
In Java, polymorphism allows one method or object to behave differently in different situations.
Example:
A person can be a student, teacher, or parent.
Similarly, one method can perform different tasks.
Polymorphism is one of the important features of Object-Oriented Programming (OOP).
Types of Polymorphism:
There are two types of polymorphism in Java:
1. Compile-time polymorphism
2. Run-time polymorphism
1. Compile-Time Polymorphism(Method Overloading)
Compile-time polymorphism is achieved through method overloading.
Decided during compilation
Method overloading means:
Same method name
Different parameters
The compiler decides which method to call during compilation.
Rules for Method Overloading
Methods can differ by:
Number of parameters
Type of parameters
Order of parameters
Example:
class Add {
int sum(int a, int b) {
return a + b;
}
int sum(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String args[]) {
Add obj = new Add();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
}
}
OUTPUT:
30
60
Advantages of Method Overloading:
Improves readability
Reduces code duplication
Increases flexibility
2. Run-Time Polymorphism(Method Overriding)
Run-time polymorphism is achieved through method overriding.
Decided during execution.
Method overriding means:
Child class provides its own implementation of parent class method.
Method name and parameters must be same.
The method call is decided during program execution.
Rules for Method Overriding
Inheritance is required
Method name must be same
Parameters must be same
Return type must be same or compatible
Example Program:
class Animal {
void sound() {
[Link]("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
public class Main {
public static void main(String args[]) {
Dog d = new Dog();
[Link]();
}
}
OUTPUT:
Dog barks