Selenium - Core Java OOPs Concept - Inheritance (Reusability)

2:09 AM 1 Comments

Advantages of re-usability:

1. Save time and effort
2. Maintenance of code is easy means if you want to modify the code. We will change the code in one place.









OOPs Concept Inheritance: 

1. The objective of inheritance is re-usability.

2. The process of acquiring the methods and variables from Super-class to Sub-Class is called Inheritance.

Class members are methods and variables.

Two types of class members -> static and nonstatic

Static Members are accessed using Class Name:
public static void display()
{

}

Non-Static Members are accessed using Object:
public void display()
{

}

Only object level class members are inherited to subclass.


3. Inheritance is achieved between two classes using the keyword called 'extends'. From now on-wards 'extends' means inheritance.

4. Single Inheritance is between two classes is achieved using the extends keyword

Class A
{
public int i=20;

public void display()
{
}

}

Class B extends ClassA
{

}



5. Multilevel Inheritance : mean the subclass(child) can access superclass(Father) and Superclass SuperClass (GrandFather) also.

  Class A   
  {


 }


 Class B extends Class A
 {


 }



 Class C extends Class B
 {


}

6. Multiple Inheritance:

   In java we cannot extend more than one class at a time.
   Multiple classes cannot be inherited at the same time.
   So, multiple inheritance is not supported in java.


   Class C extends Class B,Class A  //Not Valid in java

   Diamond Problem in java.


7. By Default a Class extends Object Class. So we can access the object class Members. Object is a class available in the package java.lang.

8. Only non-static variables and methods are inherited to the subclass.  Static variables and Methods are at the class level not at the object  level. So cannot be inherited to the subclass.

9. Global and Local Variables

public class Variables {


int a = 20; // Global variable

public static void main(String[] args) {

int a =10;
System.out.println(a); // local variable

Variables v = new Variables();
System.out.println(v.a);


}


}

Unknown

This site creates a platform to become an expert by gaining knowledge of unknown real-time issues as well.

1 comments:

rmouniak said...


Thanks for sharing this good blog.This is very important and imformative blog for Java .It's very interesting and useful for students
Java Online Training Hyderabad