INHERITANCE IN JAVA - ASSIGNMENT
What is Inheritance?
Inheritance is an Object-Oriented Programming
concept where one class acquires the properties and
behaviors of another class. It helps in code reusability
and reduces duplication. In Java, we use the keyword
'extends'.
1. Single Inheritance
One parent class and one child class.
class Animal {
void eat() {
[Link]("Eating");
}
}
class Dog extends Animal {
void bark() {
[Link]("Barking");
}
}
2. Multilevel Inheritance
A chain of inheritance (Grandparent -> Parent ->
Child).
class Animal {
void eat() {}
}
class Dog extends Animal {
void bark() {}
}
class Puppy extends Dog {
void weep() {}
}
3. Multiple Inheritance
One child inherits from multiple parents. Java does
not support this with classes but supports it using
interfaces.
interface A {
void show();
}
interface B {
void display();
}
class C implements A, B {
public void show() {}
public void display() {}
}
4. Hierarchical Inheritance
One parent class and multiple child classes.
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
class Cow extends Animal {}
5. Hybrid Inheritance
Combination of more than one type of inheritance. In
Java, this is achieved using interfaces.
6. Diamond Problem
When two parent classes have the same method, the
child class gets confused about which method to
inherit. Java avoids this by not allowing multiple
inheritance with classes.
Conclusion
Inheritance improves code reuse, reduces
redundancy, and makes programs easier to maintain.
It is one of the most important concepts in Java OOP.