Selenium/Java-OOPs-Inheritance

In last posts, we learnt about Abstraction and Encapsulation concepts of OOPs. In this post, we will go through the concept of Inheritance.

Inheritance allows object of one class to acquire properties and methods of another class in addition to new fields and methods. It is defined as parent-child relationship as child also inherits the properties of parent. Here, parent is a main class and child is a sub class which will inherit the properties of main class.

[Source: www.codepointing.blogspot.com]


In Inheritance, two keywords are mainly used which are extends and super. extends keyword is used to extend sub class to main class while super is used to inherit the properties, methods and constructor of the parent class but child class can not access the private variable of parent class in any way !!

Inheritance provides superb code reusability and useful for method overriding to achieve dynamic runtime polymorphism. (which will see in next post soon !)

There are various types of Inheritance possible in Java.


  • Single Inheritance: when one class extends only one class, it is defined as single inheritance.
Example:

// parent class

class A{

String name="Raj";

void method1(){

 system.out.println("name is: "+name);

}

// child class using the properties of the parent class

class B extends class A{

int id=1;

void method2(){

system.out.println("name is: "+name);  // reusing properties of the parent class

system.out.println("id is: "+id);



public static void main(String args[]){

B b= new B();

b.method2();

}
}

  • Multilevel Inheritance: When child class also act as a parent class for another class, it is called as multilevel Inheritance.
Example:

// parent class

class A{

String name="Raj";

void method1(){

 system.out.println("name is: "+name);

}

// child class using the properties of the parent class

class B extends class A{

int id=1;

void method2(){

system.out.println("name is: "+name);  // reusing properties of the parent class

system.out.println("id is: "+id);

}

// child class using the properties of the parent class

class C extends class B{

int age=25;

void method3(){

system.out.println("name is: "+name);  // reusing properties of the parent class

system.out.println("id is: "+id);  // reusing properties of the parent class

system.out.println("age is: "+age); 


public static void main(String args[]){

C c= new C();

c.method3();

}
}

  • Hierarchical Inheritance: When two child class extends one common parent class, it is defined as a Hierarchical Inheritance. 

[Source: www.scanftree.com]

Try to Hierarchical Inheritance just like we did in above two examples !


Besides these three inheritances, there are other two inheritances which are hybrid and multiple inheritances. Multiple Inheritance is achieved when one child class inherits properties of two parent classes.

Multiple Inheritance is not supported in Java. !

Suppose one class C extends both class A and class B which has one method with same name. If class C inherits method of both class A and B and when we call it from class C, there will be ambiguity about which method to be called. 

As compile time error are easy to resolved compare to runtime errors , Java will throw compile time error if you extend two classes. however you can achieve multiple inheritance with the help of interfaces. !

 Try below code !!

// parent class

class A{

String name="Raj";

void method1(){

 system.out.println("name is: "+name);

}

// another class with same method name


class B {

int id=1;

void method1(){

system.out.println("id is: "+id);

}

// child class using the properties of the parent class

class C extends class A,B{



public static void main(String args[]){

C c= new C();

c.method1(); // which method1 will invoke ?!
}
}

  • Hybrid Inheritance: It is a combination of both single and multiple Inheritance. 

This is all about Inheritances in Java. we will see last important OOP concept polymorphism in next post !!

Cheers !










































Comments

  1. Please provide sample output also if there is any code included..

    ReplyDelete
    Replies
    1. You will understand more if you run the code on your own and observe results. You can even try to manipulate things and learn more :)

      Delete

Post a Comment

More posts on selenium...

Selenium/Java-OOPs-Encapsulation

Selenium/Java-OOPs-Abstraction

Selenium: Automate Web Application Login

Selenium/Java- CoC

Selenium: Waits

Selenium: All about XPath Locator in WebDriver

Selenium: Capturing the screenshot

Selenium: BDD- The Disruptive Framework

Selenium: Frameworks