Inheritance in Java
1. Single Inheritance
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}
}
public class TestSingleInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
[Link](); // Inherited from Animal class
[Link](); // Defined in Dog class
}
}
Output:
This animal eats food.
The dog barks.
2. Multilevel Inheritance
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}
}
class Puppy extends Dog {
void play() {
[Link]("The puppy plays.");
}
}
public class TestMultilevelInheritance {
public static void main(String[] args) {
Puppy puppy = new Puppy();
[Link](); // Inherited from Animal
[Link](); // Inherited from Dog
[Link](); // Defined in Puppy
}
}
Output:
This animal eats food.
The dog barks.
The puppy plays.
3. Hierarchical Inheritance:
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}
}
class Cat extends Animal {
void meow() {
[Link]("The cat meows.");
}
}
public class TestHierarchicalInheritance {
public static void main(String[] args) {
Dog dog = new Dog();
[Link](); // Inherited from Animal
[Link](); // Defined in Dog
Cat cat = new Cat();
[Link](); // Inherited from Animal
[Link](); // Defined in Cat
}
}
Output:
This animal eats food.
The dog barks.
This animal eats food.
The cat meows.
protected Members in Inheritance
class Animal {
protected String name;
protected void display() {
[Link]("I am an animal.");
}
}
class Dog extends Animal {
public void getInfo() {
[Link]("My name is " + name);
}
}
class Main {
public static void main(String[] args) {
// create an object of the subclass
Dog labrador = new Dog();
// access protected field and method
// using the object of subclass
[Link] = "Rocky";
[Link]();
[Link]();
}
}
Output
I am an animal.
My name is Rocky
super Keyword in Java Inheritance
the super keyword is used to call the method of the parent class from the
method of the child class.
class Animal {
// method in the superclass
public void eat() {
[Link]("I can eat");
}
}
// Dog inherits Animal
class Dog extends Animal {
// overriding the eat() method
@Override
public void eat() {
// call method of superclass
[Link]();
[Link]("I eat dog food");
}
// new method in subclass
public void bark() {
[Link]("I can bark");
}
}
class Main {
public static void main(String[] args) {
// create an object of the subclass
Dog labrador = new Dog();
// call the eat() method
[Link]();
[Link]();
}
}
Output
I can eat
I eat dog food
I can bark
In the above example, the eat() method is present in both the base
class Animal and the derived class Dog . Notice the statement,
[Link]();
Here, the super keyword is used to call the eat() method present in the
superclass.
We can also use the super keyword to call the constructor of the superclass
from the constructor of the subclass.
Inheritance Example
class Teacher {
//fields of parent class
String designation = "Teacher";
String collegeName = "Beginnersbook";
//method of parent class
void does(){
[Link]("Teaching");
}
}
public class PhysicsTeacher extends Teacher{
//field of child class
String mainSubject = "Physics";
public static void main(String args[]){
PhysicsTeacher obj = new PhysicsTeacher();
//accessing the fields of parent class
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
//accessing the method of parent class
[Link]();
}
}
Output:
Beginnersbook
Teacher
Physics
Teaching