Inheritance is a fundamental concept in object-oriented programming that allows one class to inherit properties and methods from another class. This mechanism promotes code reusability and establishes a relationship between the parent class (superclass) and the child class (subclass).
In single inheritance, a class inherits from only one superclass. This is the most basic form of inheritance.
Here's a simple Java program to demonstrate single inheritance:
// Superclass
class Animal {
// Method in the superclass
void eat() {
System.out.println("This animal eats food.");
}
}
// Subclass
class Dog extends Animal {
// Method in the subclass
void bark() {
System.out.println("The dog barks.");
}
}
// Main class to test the inheritance
public class Main {
public static void main(String[] args) {
// Create an object of the subclass
Dog myDog = new Dog();
// Call the methods of the superclass and the subclass
myDog.eat(); // Inherited method from Animal class
myDog.bark(); // Method of the Dog class
}
}
Explanation:
Superclass (Animal):
- It has a method
eat()
which prints a message.
Subclass (Dog):
- It extends the
Animal
class, inheriting its properties and methods. - It has an additional method
bark()
which prints a message.
Main class:
- It creates an object of the
Dog
class. - It calls the
eat()
method from the Animal
class and the bark()
method from the Dog
class.
This example demonstrates how the Dog
class inherits the eat()
method from the Animal
class, allowing the Dog
object to use both the inherited method and its own method.
In Java, the super
keyword is used in two main contexts:
To call the superclass's constructor:
- It is used to invoke the constructor of the superclass from the subclass.
- This is particularly useful for reusing the initialization code of the superclass and ensuring that the superclass is properly initialized before the subclass's constructor code is executed.
To call the superclass's methods and access the superclass's fields:
- It can be used to call a method or access a field that is defined in the superclass from within a subclass.
- This is useful when the subclass overrides a method of the superclass and still needs to call the overridden method.
Example:
// Superclass
class Animal {
String name;
// Superclass constructor
Animal(String name) {
this.name = name;
}
void displayInfo() {
System.out.println("Animal Name: " + name);
}
}
// Subclass
class Dog extends Animal {
String breed;
// Subclass constructor
Dog(String name, String breed) {
super(name); // Call to the superclass constructor
this.breed = breed;
}
// Overriding the displayInfo method
@Override
void displayInfo() {
super.displayInfo(); // Call to the superclass method
System.out.println("Breed: " + breed);
}
}
// Main class to test the inheritance
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy", "Golden Retriever");
dog.displayInfo();
}
}
Program to try
// Superclass
class Shape {
// Properties of the shape
double length;
double width;
// Default constructor
Shape() {
length = 0;
width = 0;
}
// Parameterized constructor
Shape(double l, double w) {
length = l;
width = w;
}
// Method to display the dimensions
void displayDimensions() {
System.out.println("Length: " + length + ", Width: " + width);
}
}
// Subclass
class Rectangle extends Shape {
// Default constructor
Rectangle() {
super();
}
// Parameterized constructor
Rectangle(double l, double w) {
super(l, w);
}
// Method to calculate the area of the rectangle
double calculateArea() {
return length * width;
}
// Method to calculate the perimeter of the rectangle
double calculatePerimeter() {
return 2 * (length + width);
}
}
// Main class to test the Rectangle class
public class Main {
public static void main(String[] args) {
// Create a Rectangle object using the default constructor
Rectangle rect1 = new Rectangle();
System.out.println("Default Rectangle:");
rect1.displayDimensions();
System.out.println("Area: " + rect1.calculateArea());
System.out.println("Perimeter: " + rect1.calculatePerimeter());
// Create a Rectangle object using the parameterized constructor
Rectangle rect2 = new Rectangle(5, 3);
System.out.println("\nParameterized Rectangle:");
rect2.displayDimensions();
System.out.println("Area: " + rect2.calculateArea());
System.out.println("Perimeter: " + rect2.calculatePerimeter());
}
}
Comments
Post a Comment