Inheritance
In object-oriented programming, inheritance is the process by which we can reuse the functionality of an existing class in a new class. In the concept of inheritance, there are two terms: base (parent) class and derived (child) class. When a class is inherited from another class (base class), the derived class acquires all the properties and behaviors of the base class.
Source Code: Inheritance.java
// Superclass (Parent)
class Person {
String name;
int age;
void displayPersonInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Subclass (Child)
class Student extends Person {
String studentId;
void displayStudentInfo() {
// Inherited method
displayPersonInfo();
// Own property
System.out.println("Student ID: " + studentId);
}
}
public class Inheritance {
public static void main(String[] args) {
// Creating a Student object
Student s1 = new Student();
s1.name = "Thary";
s1.age = 18;
s1.studentId = "S103";
// Display student info using inherited and own methods
s1.displayStudentInfo();
}
}Result:
Watch the video:

