19 – Java Object Oriented Programming

Selenium Class 19 – Java Object Oriented Programming

Java Object Oriented Programming
i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation

i) Inheritance

  • It is a process of inheriting (reusing) Class members (Variables and methods) from one class to another
  • The Class where the class members are getting inherited is called Super Class / Parent Class / Base Class
  • The Class to which the class members are getting inherited i s called Sub Class / Child Class / Derived Class
  • The Inheritance between parent Class and Child Class is achieved using “extends” keyword

Example: Create Static and Non Static class members, and Call

public class Sample2 {
//Declare Static Variables
static int a=10, b=20;

//Declare Non Static Variables
int c =30, d=40;
//Create a Static Method with returns a value
public static int add(){
int result = a+b;
return result;
}
//Create a Non Static Method with returns a value
public int multiply(){
int result = c*d;
return result;
}
public static void main(String[] args) {
//Call a Non Static Class Members
Sample2 obj = new Sample2();
int val = obj.multiply(); //Calling a Non Static method
System.out.println(val);//1200

int val2= obj.d; //Calling a Non Static Variable
System.out.println(val2);//40

//Call Static Class Members
int val3 = Sample2.add(); //Call a Static Method
System.out.println(val3);//30

int val4 = Sample2.b;//Call Static Variable
System.out.println(val4);//20
}
}

Use Static and Non Static variables in Non Static method

//Declare Static Variables
static int a=10, b=20;

//Declare Non Static Variables
int c =30, d=40;

//Create a Non Static Method with returns a value
public int multiply(){
int result = (cd)b;
return result;
}

//Call the Non Static Method in main program
Sample2 obj = new Sample2();
int val = obj.multiply();
System.out.println(val);//24000

Note 1: We can use Static and Non Static variables in Non Statci Methods
Note 2: We can use Non Static Variables in Non Static Methods only

  • We can’t use Non Static variables in Static Methods

//Reuse a Class Non Static class members from another call without Inheritance

//Class 1:
public class Class1 {
int a =30, b=40;

public int add(){
int result = a+b;
return result;
}
}
//Class 2:
public class Class2 {

public static void main(String[] args) {
Class1 obj = new Class1();
int val =obj.add();
System.out.println(val);//70
int val2 = obj.b;
System.out.println(val2);//40
}
}

//Reuse a Class Non Static class members from another call with Inheritance

public class Class2 extends Class1 {

public static void main(String[] args) {
Class2 obj = new Class2();
int val =obj.add();
System.out.println(val);//70
int val2 = obj.b;
System.out.println(val2);//40

}
}

//Calling Class members from one class to another by creating object using source class name

package abcd;

public class Class1 {
int a =30, b=40;

public int add(){
int result = a+b;
return result;
}
public static void main(String[] args) {
Class2 xyz = new Class2();
System.out.println(xyz.sub());//50
}
}

package abcd;

public class Class2 {
public int sub(){
int x=100, y=50;
int result = x-y;
return result;
}
public static void main(String[] args) {
Class1 obj = new Class1();
System.out.println(obj.add());//70

}
}

Java Tutorial
Java Object Oriented Programming

Types of Inheritance

i) Single Inheritance
Class2 extends Class1

ii) Multi Level Inheritance
Class2 extends Class1
Class3 extends Class2

….

iii) Multiple Inheritance (* Java doesn’t support multiple inheritance)
Class2 extends Class1
Class2 extends Class7

Example: Reusing class members from different classes

Class 1:

public class Class1 {
int a =10, b=20;
public int add(){
int result = a+b;
return result;
}
public static void main(String[] args) {

}
}

Class 2:
public class Class2 extends Class1 {
int a =100, b=200;
public int add(){
int result = a+b;
return result;
}
public static void main(String[] args) {
Class2 obj = new Class2();
System.out.println(obj.add());//300

Class1 obj2 = new Class1();
System.out.println(obj2.add());//30
}
}

Multi Level Inheritance

Class1:

package abcd;

public class Class1 {
int a =10, b=20;
public int add(){
int result = a+b;
return result;
}
public static void main(String[] args) {
Class1 obj = new Class1();
System.out.println(obj.add());//30
}
}

Class 2:
public class Class2 extends Class1 {
int a =100, b=200;
public int add2(){
int result = a+b;
return result;
}
public static void main(String[] args) {
Class2 obj2 = new Class2();

System.out.println(obj2.add2());//300

System.out.println(obj2.add());//30

}
}

public class Class3 extends Class2{
int a =1, b=2;
public int add3(){
int result = a+b;
return result;
}
public static void main(String[] args) {
Class3 obj3 = new Class3();

System.out.println(obj3.add());//30
System.out.println(obj3.add2());//300
System.out.println(obj3.add3());//3

}
}

//Two classes in same project but in two different packages

//Class 1 in Package 1:
package package1;

public class Class1 {
int a =10, b=20;
public int add(){
int result = a+b;
return result;
}
public static void main(String[] args) {
}
}

//Class 2 in Package 2:

package package2;

import package1.Class1;

public class Class2 extends Class1 {

public static void main(String[] args) {
Class2 obj = new Class2();
System.out.println(obj.add());//30

}
}

//Two Classes in two different Projects

Project 1:

package package1;

public class Class1 {

int a =10, b=20;
public int add(){
int result = a+b;
return result;
}
}

Project 2:

package package2;

import package1.Class1;

public class Class2 extends Class1{

public static void main(String[] args) {
Class2 obj = new Class2();
System.out.println(obj.add());
}
}