20 – Java Object Oriented Programming Part-2
Selenium Class 20 – Java Object Oriented Programming Part-2
Java OOPS Continuation:
ii) Polymorphism
Existence of Object Behavior in many forms
We have 2 types of Polymorphism,
1) Compile Time Polymorphism / Method Overloading
2) Run Time Polymorphism / Method Overriding
1) Compile Time Polymorphism / Method Overloading
if two are more methods with same name in the same class, but differ in the following ways,
a) Number of Arguments
b) Type of Arguments
Example:
public class Class1 {
public void add(int a, int b){
System.out.println(a+b);
}
public void add(int a, int b, int c){
System.out.println(a+b+c);
}
public void add (double a, double b){
System.out.println(a+b);
}
public static void main(String[] args) {
Class1 obj = new Class1 ();
obj.add(10.345, 10.567);
obj.add(10, 20);
obj.add(10, 20, 30);
}
}
2) Run Time Polymorphism / Method Overriding
If two or more methods with same name that available in the parent class and child class
Example:
Class1:
package abcd;
public class Class1 {
int a =10, b=20;
public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
Class1 obj = new Class1();
obj.add();//30
}
}
Class2:
package abcd;
public class Class2 extends Class1{
int a=1, b=2;
public void add(){
System.out.println(a+b);
}
public static void main(String[] args) {
Class2 obj2 = new Class2();
obj2.add();//3
}
}
iii) Abstraction
It is a process of hiding the implementation details and showing only functionality to the user.
Two types of Methods
1) Concrete Methods (The Methods which are having body)
Example:
public void add(){
Statements
…………..
…………..
…………..
}
2) Abstract Methods (The Methods which are not having the body)
Example:
public abstract void add();
When we go for Abstract Methods?
- If we know the method name but don’t know the method functionality then we go for Abstract methods.
- Java Class contains 100% concrete methods.
- Abstract Class contains one or more Abstract Methods
Class 1 (having 10 Methods)
10 Methods are Concrete Methods
It is a Java Class
Class 2 (having 10 Methods)
9 Methods are Concrete Methods and One Method is Abstract Method
It is a Java Abstract Class
Class 3 (having 10 Methods)
All 10 Methods are Abstract Methods
It is a Java Abstract Class
Example for Abstract Class:
public abstract class Bikes {
public void handle(){
System.out.println(“Bikes have Handle”);
}
public void seat(){
System.out.println(“Bikes Have Seat”);
}
public abstract void engine();
public abstract void wheels();
public static void main(String[] args) {
//Bikes obj = new Bikes();
}
}
Note: We Cannot create Object in Abstract Class
Inherit Abstract Class (Class Members)
public class HeroHonda extends Bikes{
public void engine() {
System.out.println(“Bikes have Engine”);
}
public void wheels() {
System.out.println(“Bikes have Wheels”);
}
public static void main(String[] args) {
HeroHonda obj = new HeroHonda();
obj.seat();
obj.handle();
obj.engine();
obj.wheels();
}
}
public void handle(){
System.out.println(“Bikes have Handle”);
}
public void seat(){
System.out.println(“Bikes Have Seat”);
}
public abstract void engine();
public abstract void wheels();
public static void main(String[] args) {
HeroHonda obj2 = new HeroHonda();
obj2.handle();
obj2.engine();
}
}
How to call methods in Home Class/Abstract Class?
Create Object using Child Class name (We complete implementation in Child Class), then call methods.
iv) Encapsulation
- It is a process of wrapping code and data into a single unit
Ex: capsule (Mixer of Several medicines)
- Encapsulation is the technique making the fields in a class private and providing access via public methods
- It provides control over the data
- By providing getter and setter methods we can make a class read only or write only
Example:
Class 1:
package abcd;
public class Class1 {
private String name =”Test Automation”;
public String getName(){ //Accessing Private fields vai Public method/s (Reading)
return name; //Accessing name variable value
}
public void setName(String newName){ //Accessing Private field/s and Writing another value via Public method/s
name = newName; //Writing new value to “name” variable
}
public static void main(String[] args) {
Class1 obj = new Class1();
String val = obj.getName();
System.out.println(val);//Test Automation
obj.setName(“Selenium Testing”);
val= obj.getName();
System.out.println(val);//Selenium Testing
}
}
Class 2:
public class Class2 extends Class1{
public static void main(String[] args) {
Class2 abc = new Class2();
String res = abc.getName();
System.out.println(res);//Test Automation
abc.setName(“UFT Testing”);
res = abc.getName();
System.out.println(res);//UFT Testing
}
}