Inheritance

Inheritance is a concept which allows to create an "is-a relationship" in between two classes. It's similar to partent-child relationship. As example think of a student. Every Student is a Person. So Student inherits every behaviour and attribute which a person have. (this depends on access specifier. As example if the access specifier is private then those fields or methods cannot be inherited by its sub classes.)


Code: public class TestInheritance{
public static void main(String []args){
Person p1 = new Person("John",20);
p1.eat();
p1.sleep();
Student s1= new Student("Merry",16);
s1.eat();
s1.sleep();
s1.study();
}
}

class Person{
String name; int age;
Person(String name,int age){
this.name=name;
this.age=age;
System.out.println(this.name+":"+"I'm a Person");
}
void eat(){
System.out.println(this.name+":"+"Eating...");
}
void sleep(){
System.out.println(this.name+":"+"Sleeping...");
}
}

class Student extends Person{
Student(String name,int age){
super(name,age);
System.out.println(this.name+":"+"I'm a Student");
}
void study(){
System.out.println(this.name+":"+"Studying..");
}
}


When the above code is executed we will be able to get the following output


Output:
John:I'm a Person
John:Eating...
John:Sleeping...
Merry:I'm a Person
Merry:I'm a Student
Merry:Eating...
Merry:Sleeping...
Merry:Studying..


Code Explanation:
I roughly numbered the lines to show how it's going to execute the above code
01-->public class TestInheritance{
02-->public static void main(String []args){
03-->Person p1 = new Person("John",20); //making a Person object called p1
12-->p1.eat(); //calling eat method which is implemented inside the person class using p1 object
16-->p1.sleep(); //calling eat method which is implemented inside the person class using p1 object 20-->Student s1= new Student("Merry",16);// making a Student object called s1
31-->s1.eat(); /*Though a method called eat is not implemented in student class, since student is a Person it inherits eat method from its super class*/
35-->s1.sleep(); /*As mentioned above sleep method is also inherits from person class*/
39-->s1.study(); /*rather than eating and sleeping student also can study (Student class is the specialized class while Person class is the generalized class )*/
 43-->}

44-->}
04-->class Person{ //Super class (Parent class /Generalized class) 
05-->String name;
06-->int age;
7,24-->Person(String name,int age){
8,25-->this.name=name;
9,26-->this.age=age;
10,27-->System.out.println(this.name+":"+"I'm a Person");
11,28-->}
13,31-->void eat(){
14,33-->System.out.println(this.name+":"+"Eating...");
15,34-->}
17,36-->void sleep(){
18,37-->System.out.println(this.name+":"+"Sleeping...");
19,38-->}
 }

21-->class Student extends Person{ //sub class(child class/Specialized class)
22-->Student(String name,int age){
23-->super(name,age); /*using the super keyword we can call the constructor in Person class which expect string and an int parameter. (super is a keyword which we used to invoke super class constructors and also to access super class members. It is a must to call the super class constructor in the first line of the subclass constructor. if we havent mentioned it then it calls the default constructor which accept no parameters.)*/
29-->System.out.println(this.name+":"+"I'm a Student");
30-->}
40-->void study(){
41-->System.out.println(this.name+":"+"Studying..");
42-->}
 }


Comments

Popular posts from this blog

Flow Charts

ධර්මය, ආක්‍රමණය හා යුද්ධය

ආගමක් නැති මනුස්සයෙක්