inheritance in java
class Arithmetic {
}
class Adder extends Arithmetic {
public int add(int a, int b)
{
return a+b;
}
}
inheritance in java
class Arithmetic {
}
class Adder extends Arithmetic {
public int add(int a, int b)
{
return a+b;
}
}
Inheritance in java
// Hierarchical inheritance in java
class Animal
{
void eating()
{
System.out.println("animal eating");
}
}
class Lion extends Animal
{
void roar()
{
System.out.println("lion roaring");
}
}
public class Dog extends Animal
{
void bark()
{
System.out.println("dog barking");
}
public static void main(String[] args)
{
Animal obj1 = new Animal();
obj1.eating();
Lion obj2 = new Lion();
obj2.eating();
obj2.roar();
Dog obj3 = new Dog();
obj3.eating();
obj3.bark();
}
}
inheritance in java
// super class
class Animal
{
void eat()
{
System.out.println("Animal is eating.");
}
}
// subclass
class Lion extends Animal
{
public static void main(String[] args)
{
Lion obj = new Lion();
obj.eat();
}
}
Inheritance in java
// Multiple inheritance in java
interface M
{
public void helloWorld();
}
interface N
{
public void helloWorld();
}
// implementing interfaces
public class MultipleInheritanceExample implements M, N
{
public void helloWorld()
{
System.out.println("helloWorld() method");
}
public static void main(String[] args)
{
MultipleInheritanceExample obj = new MultipleInheritanceExample();
obj.helloWorld();
}
}
Inheritance in java
// Multilevel inheritance in java
class Animal
{
void eating()
{
System.out.println("animal eating");
}
}
class Lion extends Animal
{
void roar()
{
System.out.println("lion roaring");
}
}
public class Cub extends Lion
{
void born()
{
System.out.println("cub drinking milk");
}
public static void main(String[] args)
{
Animal obj1 = new Animal();
obj1.eating();
Lion obj2 = new Lion();
obj2.eating();
obj2.roar();
Cub obj3 = new Cub();
obj3.eating();
obj3.roar();
obj3.born();
}
}
Inheritance in java
// single inheritance example
import java.util.*;
class Animal
{
void eat()
{
System.out.println("Animal is eating.");
}
}
class Lion extends Animal
{
void roar()
{
System.out.println("lion is roaring.");
}
public static void main(String[] args)
{
Lion obj = new Lion();
obj.eat();
obj.roar();
}
}
Inheritance in java
// Hybrid inheritance in java
class A
{
public void display()
{
System.out.println("class A display method");
}
}
interface B
{
public void print();
}
interface C
{
public void print();
}
public class HybridInheritanceDemo extends A implements B, C
{
public void print()
{
System.out.println("implementing print method");
}
public void show()
{
System.out.println("show method of class HybridInheritanceDemo");
}
public static void main(String[] args)
{
HybridInheritanceDemo obj = new HybridInheritanceDemo();
obj.show();
obj.print();
}
}
inheritance in java
Inheritance in Java is a mechanism in which one object acquires
all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system).
inheritance in java
Builds relations between classes, main purpose:
create a TEST BASE CLAS and use it in other classes.
Inheritance allows a class to inherit properties
(objects, variables, methods) from another source (class or interface).
Allows code reusability and easy to maintain.
SUPER CLASS (also known as parent or base class):
is the class where the fields are being inherited from.
SUB CLASS (also known as the child or derived class):
is the class inheriting the properties
INHERITANCE EXAMPLE
In my framework I have a TestBase class where I store
all my reusable code and methods. My test execution classes,
and elements classes will extend the TestBase in order to reuse the code.
My framework follow POM and some pages have similar actions,
so I can easily use those similar actions and fields
by inheriting them from the ready classes.
Example: Base Page Class and Test Base Class.
These 2 class are being inherited from so many different classes.
For Example; In Pages Package, Base Page Class is being extended
by all the class by Base Package. So that Constructor
can be automatically be called in the sub classes.
That way you will be able to locate the elements
by using same driver.
Test Base Class can also be inheritance.
One driver, TestNG framework one before method to
setup browser and reuse it every single test class it.
By inheriting them to other test class. These 2 class are
abstract class and meant to be inherited to other classes.
We are not creating any object in these 2 class.
These class is super class. Comes from Selenium library WebDriver,
takes Screenshots, javascriptexecuter these are interface.
List and Set also interface. You cannot create object in interface.
They are only being reference. These are also example for abstraction.
Inheritance in Java
Java Inheritance (Subclass and Superclass)
In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:
subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us