Inheritance in Java
Inheritance allows a class to inherit fields and methods from another class, promoting code reuse and logical hierarchy. In simple terms, inheritance lets us create new classes based on existing ones.
Inheritance allows a class to inherit fields and methods from another class, promoting code reuse and logical hierarchy. In simple terms, inheritance lets us create new classes based on existing ones.
Java supports the following types of inheritance:
Note: Java does not support multiple inheritance with classes to avoid ambiguity. However, it can be achieved using interfaces.
This syntax shows how a class inherits from another using the extends keyword. The Child class gains access to all the fields and methods of the Parent class, and can also define its own.
class Parent {
// fields and methods
}
class Child extends Parent {
// additional fields and methods
}
This example shows how a subclass inherits methods from a single superclass. The Dog class inherits the sound() method from Animal, and also defines its own method bark(). In the main method, both inherited and own methods are accessible through the Dog object.
class Animal {
void sound() {
System.out.println("Animals make sounds");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound(); // inherited method
d.bark(); // own method
}
}
Animals make sounds
Dog barks
This example demonstrates multilevel inheritance, where a class inherits from a derived class, forming a chain. The class Puppy inherits from Dog, which in turn inherits from Animal. As a result, Puppy has access to methods of both Dog and Animal.
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Weeping...");
}
}
public class Main {
public static void main(String[] args) {
Puppy p = new Puppy();
p.eat();
p.bark();
p.weep();
}
}
Eating...
Barking...
Weeping...
This example demonstrates hierarchical inheritance where multiple classes (Dog and Cat) inherit from a single base class (Animal). Both subclasses reuse the breathe() method from Animal, while also having their own specific methods (bark() and meow()).
class Animal {
void breathe() {
System.out.println("Breathing...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Meowing...");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.breathe();
d.bark();
Cat c = new Cat();
c.breathe();
c.meow();
}
}
Breathing...
Barking...
Breathing...
Meowing...
When a subclass provides its own implementation of a method that is already defined in its parent class, it is called method overriding. This allows the subclass to define behavior specific to its type.
The @Override annotation is used to indicate that a method is being overridden.
In the example, the Dog class overrides the sound() method of the Animal class. When sound() is called on an Animal reference pointing to a Dog object, the overridden method in Dog executes, demonstrating runtime polymorphism.
class Animal {
void sound() {
System.out.println("Some generic animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // calls overridden method
}
}
Dog barks
super to call parent class methods or constructors.super().class Animal {
void sound() {
System.out.println("Some generic animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}