Inheritance (Detailed Definition + Explanation +
Example)
Definition:
Inheritance in Java is an object-oriented mechanism where one class (child/subclass) acquires
properties and behaviors of another class (parent/superclass). It allows reuse of existing code
and supports method overriding, enabling runtime polymorphism. With inheritance, programmers
avoid redundant code and build a structured, hierarchical model.
Explanation:
Inheritance establishes an IS-A relationship, meaning the child class is a type of parent class
(e.g., Dog is an Animal). When child extends parent, it inherits methods and variables and may
add its own features. In Java, inheritance promotes reusability, maintainability, and allows
polymorphic behavior.
Example:
class Animal {
void eat(){ [Link]("Animal eats food"); }
}
class Dog extends Animal {
void bark(){ [Link]("Dog barks"); }
}
public class Test {
public static void main(String[] args){
Dog d = new Dog();
[Link](); // inherited method
[Link]();
}
}
2. Use of Inheritance (Purpose of Inheritance)
Definition:
The primary use of inheritance is to enable code reuse, extension of existing features, and
implementation of polymorphism. It simplifies development by allowing new classes to use
existing tested code.
Explanation:
Instead of writing the same code again, we can extend a class and use its members. Inheritance
helps in large software projects, where parent classes define general behavior and children
specialize it.
Example:
class Vehicle {
void start(){ [Link]("Vehicle starts"); }
}
class Car extends Vehicle {
void music(){ [Link]("Music system on"); }
}
3. Object Class (Root of Java Classes)
Definition:
Object is the parent class of all classes in Java. If you don't explicitly extend any class, Java
automatically extends Object class. It provides essential methods used by all objects.
Important Methods: toString(), equals(), hashCode(), getClass(), finalize() (deprecated)
Example:
class A { }
public class Test {
public static void main(String[] args){
A obj = new A();
[Link]([Link]());
}
}
4. Types of Inheritance in Java
(a) Single Inheritance
One parent one child
Example:
class A { void show(){ [Link]("A"); } }
class B extends A { }
(b) Multilevel Inheritance
One class derived from another which is also derived from another
(A B C)
Example:
class Animal {}
class Mammal extends Animal {}
class Dog extends Mammal {}
(c) Hierarchical Inheritance
One parent multiple children
Example:
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
Multiple Inheritance (Not supported for classes)
Java avoids ambiguity (diamond problem).
Supported through interfaces only
Example:
interface A{ void show(); }
interface B{ void show(); }
class C implements A, B {
public void show(){ [Link]("Hello"); }
}
(d) Hybrid Inheritance
Combination (achieved using interfaces)
5. Coupling and Typecasting
(A) Coupling
Definition:
Coupling refers to how strongly two classes depend on each other.
Tight coupling classes directly depend on each other
Loose coupling (preferred) classes interact using interfaces/abstractions
Example (Loose coupling):
interface Vehicle{ void run(); }
class Car implements Vehicle {
public void run(){ [Link]("Car running"); }
}
(B) Typecasting in Java
Upcasting: Child Parent (safe)
Animal a = new Dog();
Downcasting: Parent Child (explicit)
Dog d = (Dog)a;
6. Abstract Class
Definition:
An abstract class is a class that cannot be instantiated and may contain abstract as well as
concrete methods. It provides a common template for subclasses.
Example:
abstract class Shape {
abstract void draw();
void color(){ [Link]("Color selected"); }
}
7. Abstract Method
Definition:
A method without body declared using abstract keyword. Subclasses must override it. Helps
achieve partial abstraction.
Example:
abstract class Animal{
abstract void sound();
}
8. Dynamic Method Dispatch (Runtime
Polymorphism)
Definition:
Java resolves method calls at runtime, based on the actual object — not reference type. Enables
overriding and dynamic execution.
Example:
class A { void show(){ [Link]("A"); } }
class B extends A { void show(){ [Link]("B"); } }
A obj = new B();
[Link](); // prints B
9. final Keyword (variable, method, class)
Use Meaning Example
final variable cannot change value final int x=10;
final method cannot be overridden final void show(){}
final class cannot be inherited final class A{}
10. super Keyword
super refers to parent class and is used to call parent constructor or access parent methods/
variables.
Example:
class A { A(){ [Link]("Parent"); } }
class B extends A {
B(){ super(); }
}
11. Interface
An interface provides a complete abstraction with only method declarations (before Java 8). It
supports multiple inheritance.
Example:
interface Animal { void eat(); }
class Dog implements Animal {
public void eat(){ [Link]("Eating"); }
}
12. Interface Naming Conflicts
When two interfaces have same method name, implementing class must override to resolve
conflict.
Example:
interface A{ void show(); }
interface B{ void show(); }
class C implements A, B {
public void show(){ [Link]("Resolved"); }
}
13. Method Naming Conflict
Handled by overriding method as shown above.
14. Variable Naming Conflict
If two interfaces have same variable name, access by interface name.
Example:
interface X{ int a=10; }
interface Y{ int a=20; }
class Z implements X,Y {
void show(){ [Link](X.a); }
}
15. Marker Interface
Interface with no methods, used to mark class for JVM behavior.
Examples: Serializable, Cloneable.
Example:
class Student implements [Link] {}
16. Interface vs Abstract vs Concrete Class
Feature Interface Abstract Class Concrete Class
Object creation
Methods abstract + default/static abstract + normal normal only
Variables final & static any type any type
Multiple inheritance