examples of inheritance java
Example 1: 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();
}
}
Example 2: how to make one java class inherit from another
public class Bicycle {
public int cadence;
public int gear;
public int speed;
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}