1) What is Inheritance?
Inheritance in Java is a core principle of Object-Oriented Programming (OOP) that allows a subclass (child) to acquire the properties (fields) and behaviors (methods) of a superclass (parent).
Syntax:
class ParentClass { // fields (variables)
// methods }
class ChildClass extends ParentClass { // additional fields
// additional methods }
Enter fullscreen mode
Exit fullscreen mode
2) Types of Inheritance
Single Inheritance
One child class inherits from one parent class.
public class Animal { public void eat () { System . out . println ( "Eating" ); } }
public class Dog extends Animal { public void bark () { System . out . println ( "Barking" ); } }
Enter fullscreen mode
Exit fullscreen mode
Multilevel Inheritance
A class inherits from another class, which itself is inherited from another class.
public class Animal { public void eat () { System . out . println ( "Eating" ); } }
public class Dog extends Animal { public void bark () { System . out .…