This tutorial aims to provide a comprehensive understanding of inheritance and polymorphism in Java. We'll be implementing these concepts through practical examples.
By the end of this tutorial, you will:
- Understand the concepts of inheritance and polymorphism
- Be able to implement inheritance in Java
- Know how to use polymorphism in Java
Before starting this tutorial, you should have:
- Basic knowledge of Java programming language
- Installed Java Development Kit (JDK) on your computer
- Installed an Integrated Development Environment (IDE), such as Eclipse, IntelliJ IDEA, or NetBeans
Inheritance in Java is a mechanism where one class acquires the properties (methods and fields) of another. With the use of inheritance, information is managed in a hierarchical order.
To achieve inheritance, we use the extends
keyword.
class SuperClass {
// fields and methods
}
class SubClass extends SuperClass {
// fields and methods
}
Polymorphism in Java is a concept by which we can perform a single action in different ways. We can perform polymorphism by method overloading and method overriding.
Here is a simple example of inheritance where a child class inherits the properties of a parent class.
class Animal {
void eat() {
System.out.println("eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("barking...");
}
}
class TestInheritance {
public static void main(String args[]) {
Dog d = new Dog();
d.bark();
d.eat();
}
}
In this example, Dog class (subclass) is inheriting the Animal class (superclass). Hence, Dog can access the eat()
method of Animal class.
The expected output is:
barking...
eating...
Here is a simple example of polymorphism where one task is performed in different ways.
class Animal {
void sound() {
System.out.println("Animal is making a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog is barking");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat is meowing");
}
}
class Main {
public static void main(String args[]) {
Animal a1, a2, a3;
a1 = new Dog();
a2 = new Cat();
a3 = new Animal();
a1.sound();
a2.sound();
a3.sound();
}
}
In this example, we have three classes: Animal, Dog, and Cat. The Dog and Cat classes override the sound()
method of the Animal class. In the main method, we create objects of the Dog, Cat, and Animal classes but reference them with a variable of type Animal (superclass).
The expected output is:
Dog is barking
Cat is meowing
Animal is making a sound
In this tutorial, we've discussed the concepts of inheritance and polymorphism in Java. We've also seen how these concepts can be implemented through practical examples.
To further your understanding, you can explore topics like multiple inheritance, multilevel inheritance, and hybrid inheritance. Check out the official Java documentation for additional resources.
Exercise 1: Create a superclass named 'Shape' with a method to calculate area. Now, create two subclasses 'Rectangle' and 'Circle' and override the area method.
Exercise 2: Implement polymorphism by creating a superclass named 'Animal' with a method 'sound'. Now, create three subclasses 'Dog', 'Cat', 'Cow' and override the sound method.
Solutions and explanations of these exercises will be provided in the next tutorial. Remember, practice is key to mastering these concepts. Happy coding!