What is inheritance? java code example
Example 1: inheritance in java
class Animal
{
void eat()
{
System.out.println("Animal is eating.");
}
}
class Lion extends Animal
{
public static void main(String[] args)
{
Lion obj = new Lion();
obj.eat();
}
}
Example 2: Inheritance in java
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();
}
}