BASE CLASS AND DERIVED CLASS EXAMPLE
// Base class (Superclass)
class Animal {
String name;
// Constructor
Animal(String n) {
name = n;
// Method in base class
void eat() {
System.out.println(name + " is eating.");
// Derived class (Subclass)
class Dog extends Animal {
// Constructor calls base class constructor using super
Dog(String n) {
super(n);
// Method specific to derived class
void bark() {
System.out.println(name + " is barking.");
}
// Main class
public class InheritanceExample {
public static void main(String[] args) {
// Create object of derived class
Dog d = new Dog("Tommy");
// Access base class method
d.eat();
// Access derived class method
d.bark();
OUTPUT:
Tommy is eating.
Tommy is barking.
SINGLE INHERITANCE:
class Animal {
void eat() {
System.out.println("Animal is eating");
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
public class SingleInheritance {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // Inherited method
d.bark(); // Child class method
OUTPUT:
Animal is eating
Dog is barking
MULTILEVEL INHERITANCE(GRANDPARENT-PARENT-CHILD)
class Animal {
void eat() {
System.out.println("Eating");
class Mammal extends Animal {
void walk() {
System.out.println("Walking");
class Dog extends Mammal {
void bark() {
System.out.println("Barking");
public class MultilevelInheritance {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // from Animal
d.walk(); // from Mammal
d.bark(); // from Dog
}
HIERARCHICAL INHERITANCE:
class Animal {
void eat() {
System.out.println("Animals eat food");
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
public class HierarchicalInheritance {
public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
d.eat();
d.bark();
c.eat();
c.meow();
}
OUTPUT:
Animals eat food
Dog barks
Animals eat food
Cat meows
4️⃣ Multiple Inheritance Example (Using Interfaces)
👉 Java doesn’t support multiple inheritance using classes,
but it allows it through interfaces.
interface Animal {
void eat();
interface Pet {
void play();
class Dog implements Animal, Pet {
public void eat() {
System.out.println("Dog eats bone");
public void play() {
System.out.println("Dog loves to play fetch");
public class MultipleInheritance {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.play();
OUTPUT:
Dog eats bone
Dog loves to play fetch
Type Description Example Classes
Single One class inherits from another Animal → Dog
Multilevel Chain of inheritance Animal → Mammal → Dog
Animal → Dog, Animal →
Hierarchical One class acts as parent for multiple Cat
Multiple (via A class implements more than one Dog implements Animal,
interface) interface Pet
POLYMORPHISM EXAMPLE
// Base class
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
// Derived class 1
class Dog extends Animal {
// Overriding the base class method
void makeSound() {
System.out.println("Dog barks");
// Derived class 2
class Cat extends Animal {
// Overriding the base class method
void makeSound() {
System.out.println("Cat meows");
// Main class
public class PolymorphismExample {
public static void main(String[] args) {
Animal a; // Reference of base class
a = new Dog(); // Base class reference → Dog object
a.makeSound(); // Calls Dog's version of makeSound()
a = new Cat(); // Base class reference → Cat object
a.makeSound(); // Calls Cat's version of makeSound()
Explanation:
1. Polymorphism means many forms.
o The same method call (makeSound()) behaves differently depending on the
object type.
2. Method Overriding:
o Both Dog and Cat classes override the makeSound() method of Animal.
3. Dynamic Method Dispatch:
o The method that gets executed is decided at runtime, based on the object type
(Dog or Cat).
OUTPUT:
Dog barks
Cat meows
PROGRAM TO DIFFERENTIATE OVERLOADING AND OVERLOADING
// Base class
class Shape {
// Method 1: Simple method
void draw() {
System.out.println("Drawing a shape");
// Method Overloading (same method name, different parameters)
void draw(String color) {
System.out.println("Drawing a shape with color: " + color);
// Derived class
class Circle extends Shape {
// Method Overriding (same method name and parameters as in base class)
@Override
void draw() {
System.out.println("Drawing a circle");
// Main class
public class OverloadOverrideExample {
public static void main(String[] args) {
Shape s1 = new Shape(); // Base class object
Shape s2 = new Circle(); // Base class reference, derived class object
System.out.println("---Method Overloading Example---");
s1.draw(); // Calls draw() of Shape
s1.draw("Red"); // Calls overloaded draw(String) of Shape
System.out.println("\n---Method Overriding Example---");
s2.draw(); // Calls overridden draw() of Circle
Explanation
Concept Definition Example in Program
Same method name, different parameter list (type or draw() and draw(String
Overloading
number of arguments). Happens within the same class. color) in Shape class.
Concept Definition Example in Program
Same method name and parameters, but defined in
draw() in Circle overrides
Overriding both base and derived classes. Happens across classes
draw() in Shape.
(inheritance).
---Method Overloading Example---
Drawing a shape
Drawing a shape with color: Red
---Method Overriding Example---
Drawing a circle