It is an object oriented programming concept where a class can derive properties and behavior of another class.
In java : extends keyword would signify inheritance is there.
import java.util.io*
//parent class or super class
class School{
private String name;
School(){
name = "DPS";
}// Default constructor
void printSchoolName(){
System.out.println("School name:" + name);
}
}
class Student{
private String name;
Student(String name){
this.name = name;
}
void printStudentName(){
System.out.println("Student name:" + name);
}
public class Main{
public static void main(String[] args){
School school = new School();;
Student student = new Student(name = "Raj");
student.printStudentName();
school.printSchoolName();
}
}
Now we modify the student class to inherit from school in next part of example
Now changing the student class to inherit from school class.
Student has inherited the behavior from school.
//subclass or child class
class Student extends School{// note the inheritance
private String name;
Student(String name){
this.name = name;
}
void printStudentName(){
System.out.println("Student name:" + name);
}
public class Main{
public static void main(String[] args){
School school = new School();;
Student student = new Student(name = "Raj");
student.printStudentName();
student.printSchoolName();// This would now work with student.printSchoolName();
}
}
If a class is extending another class.
It has access to everything that is accessible in that class.
(We wont have access to private?)