Inheritance in java
The process of getting the property of
one class into another class is called
Inheritance
The process of deriving a new class
from old class is called
Inheritance
The old class is called base class or
parent super class
New class is called child class, derived .
sub class
Parent class : the class which is
inherited the another class is called
Parent class or base class
Child class: the class which is inherits
the property of another lcass is called
Child class or sub class or derived class
How we can inherits one class into
another
Derived class extends base class
Example:
Class subtraction extends addition
Here addition is base class and
subtraction is child class
Ex;
Class addition
{
Void add()
{
Int x,y=20,z=10;
X=y+z;
System.out.println(“addition: “ +x);
}
Class subtraction extends addition
{
Void sub()
{
Int x, y=20,z=30;
s.o.p(“sub=”+x);
}
Public static void main(String[] args)
{
Subtraction a1=new subtraction();
A1.add();
A1.sub();
}
Example for single inheritanace :
package Chrome_Browse;
public class single {
void add()
{
int x,y=20,z=30;
x=y+z;
System.out.println("addition of two
vlaues is : "+x);
}
}
class sub1 extends single
{
void sub()
{
int x, y=30,z=40;
x=z-y;
System.out.println("subtraction of
two values is : "+x);
}
Class 2:
package Chrome_Browse;
public class Calling {
public static void main(String[]
args) {
// TODO Auto-generated method
stub
sub1 a1=new sub1();
a1.add();
a1.sub();
}
}
In single inheritance we have one base
class and one derived class
Multi level inheritance :
In this inheritance first class is inherited
with second class and second class is
inheritace wioth
Third class and third class is inherited
with fourth class and so on------
Opening chrome browser
Navigate to amazon page
Max the window
Get the title and url of the page
Capture all the dropdown elements
from the list
Capture the screen shot
Close the browser
Use multi level inheritance
Class1:
package multi_level;
import org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriv
er;
public class T1 {
WebDriver d;
String
baseurl="https://www.amazon.in/";
public void Opening_Browser()
{
System.setProperty("webdriver.chrome.
driver","D://chromedriver.exe");
d=new ChromeDriver();
d.get(baseurl);
d.manage().window().maximize();
}
public void title()
{
System.out.println("amazon page
title:"+d.getTitle());
}
Class 2:
package multi_level;
import java.util.List;
import org.openqa.selenium.By;
import
org.openqa.selenium.WebElement;
public class T2 extends T1{
public void url()
{
System.out.println("page
url:"+d.getCurrentUrl());
}
public void dropdown()
{
WebElement
drop=d.findElement(By.id("searchDropd
ownBox"));
List<WebElement>drop1=drop.findElemen
ts(By.tagName("option"));
System.out.println(drop1.size());
for(int i=0;i<drop1.size();i++)
{
System.out.println(drop1.get(i).getTe
xt());
}
}
Class 3:
package multi_level;
import java.io.File;
import java.io.IOException;
import
org.apache.commons.io.FileUtils;
import
org.openqa.selenium.OutputType;
import
org.openqa.selenium.TakesScreenshot;
public class T3 extends T2{
public void Screenshot() throws
IOException
{
File
src=((TakesScreenshot)d).getScreensho
tAs(OutputType.FILE);
FileUtils.copyFile(src, new
File("D://d1.png"));
}
public void close()
{
d.close();
}
}
Class 4:
package multi_level;
import java.io.IOException;
public class T4 {
public static void main(String[]
args) throws IOException {
// TODO Auto-generated method
stub
T3 a=new T3();
a.Opening_Browser();
a.title();
a.url();
a.dropdown();
a.Screenshot();
a.close();
}
Hierarchical inheritanace :
Single class is inherited by two or more
class
Class 1
Class 2 extends class1
Class 3 extends class 1
Class 4 extends class 1
Example :
Class 1:
package hierarchial_inheritance;
public class t11 {
public void add()
{
int x, y=10,z=20;
x=z+y;
System.out.println("addition:"+x);
}
}
Class 2:
package hierarchial_inheritance;
public class t12 extends t11{
public void sub()
{
int x=10,y=20,z;
z=y-x;
System.out.println(z);
}
Class 3”
package hierarchial_inheritance;
public class t13 extends t11{
public void mul()
{
int x=10,y=20,z;
z=x*y;
System.out.println(z);
}
public void div()
{
int x=10,y=5,z;
z=x/y;
System.out.println(z);
}
public static void main(String[]
args)
{
t13 a=new t13();
a.add();
a.mul();
a.div();
t12 a1=new t12();
a1.sub();
}
}
Combination of any two inheritances
is called hybrid inheritance