what is the inheritance in java
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (subclass or child class) to inherit properties and behaviors from another class (superclass or parent class). The subclass can reuse and extend the functionality of the superclass, promoting code reuse and creating a hierarchical relationship between classes.
In Java, inheritance is implemented using the extends
keyword. Here’s a basic example to illustrate inheritance:
![inheritance in java inheritance in java](https://www.testpreparationz.com/wp-content/uploads/2023/12/Inheritance.png)
Example# 1
Example
// Parent class (superclass)
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
void sleep() {
System.out.println("Animal is sleeping.");
}
void move() {
System.out.println("Animal is moving.");
}
void makeNoise() {
System.out.println("Animal makes a noise.");
}
}
// Child class (subclass) inheriting from Animal
class Cow extends Animal {
// Overriding the eat() method
@Override
void eat() {
System.out.println("Cow is grazing.");
}
// Overriding the makeNoise() method
@Override
void makeNoise() {
System.out.println("Cow says moo.");
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of the Cow class
Cow myCow = new Cow();
// Accessing methods from both Animal and Cow classes
myCow.eat(); // Overridden method in Cow
myCow.sleep(); // Inherited from Animal
myCow.move(); // Inherited from Animal
myCow.makeNoise(); // Overridden method in Cow
}
}
- The
Animal
class is the superclass with methodseat()
,sleep()
,move()
, andmakeNoise()
. - The
Cow
class is the subclass that extendsAnimal
. It inherits the methodseat()
,sleep()
,move()
, andmakeNoise()
from theAnimal
class and overrides theeat()
andmakeNoise()
methods with its own implementations. - The
main
method demonstrates creating an instance of theCow
class and calling methods from both theAnimal
andCow
classes.
Key points about inheritance in Java:
- Code Reusability: Inheritance promotes code reuse by allowing subclasses to reuse the code of the superclass.
- Method Overriding: Subclasses can provide their own implementation for methods inherited from the superclass. This is known as method overriding.
- Access to Superclass Members: Subclasses have access to public and protected members of the superclass, allowing them to use or override those members.
- Single Inheritance: In Java, a class can extend only one superclass (single inheritance). However, a class can implement multiple interfaces, allowing it to inherit from multiple sources.
Example# 2
Example
// Parent class (superclass)
class Animal {
void eat() {
System.out.println("Animal is eating.");
}
void sleep() {
System.out.println("Animal is sleeping.");
}
}
// Child class (subclass) inheriting from Animal
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking.");
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of the Dog class
Dog myDog = new Dog();
// Accessing methods from both Animal and Dog classes
myDog.eat(); // Inherited from Animal
myDog.sleep(); // Inherited from Animal
myDog.bark(); // Specific to Dog
}
}
In this example:
- The
Animal
class is the superclass with methodseat()
andsleep()
. - The
Dog
class is the subclass that extendsAnimal
. It inherits the methodseat()
andsleep()
from theAnimal
class and adds its own methodbark()
. - The
main
method demonstrates creating an instance of theDog
class and calling methods from both theAnimal
andDog
classes.
Example of method overriding in the subclass:
Example
class Cat extends Animal {
// Method overriding
@Override
void eat() {
System.out.println("Cat is eating.");
}
}
public class Main {
public static void main(String[] args) {
Cat myCat = new Cat();
myCat.eat(); // Calls the overridden method in Cat class
myCat.sleep(); // Inherited from Animal class
}
}
In this example, the Cat
class overrides the eat()
method inherited from the Animal
class with its own implementation.