inheritance object creation in java code example
Example 1: 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();
}
}
Example 2: inheritance in java
it is used to define relationship between two class,
which a child class occurs all the properties and
behaviours of a parent class.
Provides code reusability.
Ex: in my framework I have a TestBase
class which 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.