simple inheritance program in 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
interface M
{
public void helloWorld();
}
interface N
{
public void helloWorld();
}
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();
}
}
Example 3: 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.