Introduction to Inheritance in Java:
A Beginner's Guide with Sample Code
Presented By :-
Monish Rai (23/CS/266)
Nikhil Kumar (23/CS/280)
Nikshay Yadav (23/CS/284)
Introduction to Inheritance
• In Java, inheritance is a fundamental concept
that allows a class to inherit properties and
methods from another class.
• This promotes code reusability and
establishes a hierarchical relationship
between classes.
• In this presentation, we will explore the
basics of inheritance and provide sample
code for better understanding.
What is
Inheritance ?
• Inheritance is a mechanism where a new class,
known as a subclass, derives from an existing
class, known as a superclass.
• The subclass inherits attributes and methods
from the superclass, allowing for extension and
specialization of functionality.
• This leads to cleaner and more organized code.
Types of
Inheritance
• Java supports several types of
inheritance: single, multilevel,
hierarchical, hybrid and multiple
inheritance (through interfaces).
• Each type serves different design
needs.
• Understanding these types is crucial
for effective class design.
• In Java, the extends keyword is used to
Using the 'extends' Keyword indicate that a class is inheriting from a
superclass.
• For example, class Dog extends Animal
signifies that Dog inherits all methods and
attributes from Animal.
• This keyword is essential for establishing
inheritance relationships.
Single Inheritance
• Single inheritance occurs when a class
inherits from one superclass. This is the
simplest form of inheritance and helps
maintain a clear class structure.
• For example, if you have a class Animal, a
subclass Dog can inherit its properties and
methods, making it specific.
Sample Code: Single Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food.");
} Output:
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited method
myDog.bark(); // Method of Dog class
}
}
Multilevel Inheritance
• In multilevel inheritance, a class derives
from another subclass, creating a chain of
inheritance.
• For instance, if Animal is the superclass and
Dog is a subclass, then Puppy can inherit
from Dog. This allows for a more detailed
classification of classes.
Sample Code: Multilevel Inheritance
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
} Output:
class Mammal extends Animal {
void walk() {
System.out.println("This mammal walks.");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.eat(); // Inherited from Animal class
myDog.walk(); // Inherited from Mammal class
myDog.bark(); // Method of Dog class
}
}
Hierarchical Inheritance • In hierarchical inheritance, multiple subclasses inherit from
a single superclass.
• For example, if Animal is the superclass, both Dog and Cat
can inherit from it.
• This structure promotes code reuse and allows for the
implementation of shared characteristics among different
classes.
Sample Code: Hierarchial Inheritance
//Base Class public class Main {
class Animal { public static void main(String[] args) {
void eat() { // Creating an instance of Fish
System.out.println("eats"); Fish fish = new Fish();
} fish.eat(); // Inherited method
void breathe() { fish.swim(); // Specific to Fish class
System.out.println("Breathes");
} // Creating an instance of Mammals
} Mammals mammal = new Mammals();
mammal.breathe(); // Inherited method
//1st Subclass mammal.walk(); // Specific to Mammals class
class Fish extends Animal { }
void swim() { }
System.out.println("swims");
}
} Output:
//2nd Subclass
class Mammals extends Animal {
void walk() {
System.out.println("walks ");
}
}
Hybrid
Inheritance
• The hybrid inheritance is the composition of two or more
types of inheritance.
• The main purpose of using hybrid inheritance is to modularize
the code into well-defined classes.
• It also provides the code reusability.
Sample Code: Hybrid Inheritance
class GrandFather { public static void main(String args[]) {
public void showG() { Son obj = new Son();
System.out.println("He is grandfather."); obj.showS(); // Accessing Son class method
} obj.showF(); // Accessing Father class method
} obj.showG(); // Accessing GrandFather class method
// inherits GrandFather properties Daughter obj2 = new Daughter();
obj2.showD(); // Accessing Daughter class method
class Father extends GrandFather {
obj2.showF(); // Accessing Father class method
public void showF() { obj2.showG(); // Accessing GrandFather class method
System.out.println("He is father."); }
} }
}
// inherits Father properties
class Son extends Father { Output:
public void showS() {
System.out.println("He is son.");
}
}
// inherits Father properties
public class Daughter extends Father {
public void showD() {
System.out.println("She is daughter.");
}
}
Multiple Inheritance
• Java does not support multiple inheritance
directly to avoid ambiguity.
• However, it can be achieved through interfaces.
• A class can implement multiple interfaces,
allowing it to inherit behaviors from multiple
sources while maintaining a clear structure.
Interfaces in Java
• An interface in Java is a blueprint of a class. It has
static constants and abstract methods.
• There can be only abstract methods in the Java
interface, not method body.
Reasons to use Interfaces in Java :
1. It is used to achieve abstraction.
2. By interface, we can support the functionality of
multiple inheritance.
Sample Code: Multiple Inheritance
interface Character {
void attack();
}
Output:
interface Weapon {
void use();
}
class Warrior implements Character, Weapon {
public void attack() {
System.out.println("Warrior attacks with a sword.");
}
public void use() {
System.out.println("Warrior uses a sword.");
}
}
public class Main {
public static void main(String[] args) {
Warrior warrior = new Warrior();
warrior.attack();
warrior.use();
}
}
Conclusion on Inheritance
• Learning inheritance in Java is essential for building efficient and
maintainable applications.
• It promotes code reusability, allows for better organization, and supports
polymorphism.
• By understanding the different types and their applications, you can
enhance your object-oriented programming skills.
Thanks!