⬅ Previous Topic
Java final classNext Topic ⮕
Java Method Overriding⬅ Previous Topic
Java final classNext Topic ⮕
Java Method OverridingInheritance 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.
Java uses the extends
keyword to establish inheritance between classes:
class Parent {
// fields and methods
}
class Child extends Parent {
// additional fields and methods
}
Let’s understand with a simple example:
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
Explanation: The class Dog
inherits the sound()
method from the Animal
class using extends
. This demonstrates how a subclass reuses parent functionality.
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...
Explanation: The Puppy
class inherits from Dog
, which in turn inherits from Animal
. This chain forms a multilevel hierarchy.
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...
Explanation: Both Dog
and Cat
classes inherit from Animal
. This forms a hierarchical structure where multiple subclasses share a common superclass.
Sometimes the child class needs to provide its own version of a method inherited from the parent class. This is known as method overriding.
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
Explanation: Even though a
is an Animal
type reference, it points to a Dog
object. Hence, the overridden method in Dog
is executed.
super
to call parent class methods or constructors.super()
.Inheritance lets us reuse code and model real-world relationships.
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();
}
}
⬅ Previous Topic
Java final classNext Topic ⮕
Java Method OverridingYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.