Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

hierarchy inheritance gfg java

// Java program to illustrate the
// concept of inheritance
  
// base class
class Bicycle {
    // the Bicycle class has two fields
    public int gear;
    public int speed;
  
    // the Bicycle class has one constructor
    public Bicycle(int gear, int speed)
    {
        this.gear = gear;
        this.speed = speed;
    }
  
    // the Bicycle class has three methods
    public void applyBrake(int decrement)
    {
        speed -= decrement;
    }
  
    public void speedUp(int increment)
    {
        speed += increment;
    }
  
    // toString() method to print info of Bicycle
    public String toString()
    {
        return ("No of gears are " + gear + "
"
                + "speed of bicycle is " + speed);
    }
}
  
// derived class
class MountainBike extends Bicycle {
  
    // the MountainBike subclass adds one more field
    public int seatHeight;
  
    // the MountainBike subclass has one constructor
    public MountainBike(int gear, int speed,
                        int startHeight)
    {
        // invoking base-class(Bicycle) constructor
        super(gear, speed);
        seatHeight = startHeight;
    }
  
    // the MountainBike subclass adds one more method
    public void setHeight(int newValue)
    {
        seatHeight = newValue;
    }
  
    // overriding toString() method
    // of Bicycle to print more info
    @Override public String toString()
    {
        return (super.toString() + "
seat height is "
                + seatHeight);
    }
}
  
// driver class
public class Test {
    public static void main(String args[])
    {
  
        MountainBike mb = new MountainBike(3, 100, 25);
        System.out.println(mb.toString());
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: combine .proto in android studio project 
Java :: java.lang.number is interface or abstract class 
Java :: join table in where clause criteria in java hibernate 
Java :: give text color and font size in android string 
Java :: sphere 
Java :: int a[ ]={4,8,3,2}; a[0] = 23; a[3]= a[1]; a[2]=12; for(int i=0; i<a.length; i++) System.out.println(a[i]); 
Java :: java virtual override 
Java :: @Controller 
Java :: gc algorithms java 8 
Java :: google pass api integration in java 
Java :: java radom float 
Java :: spring boot endpoint getting list from the body 
Java :: classloader in static method 
Java :: spigot scoreboard objective 
Java :: problemi arrayList javafx 
Java :: how to set id to TextView programmatically java android 
Java :: java boolean even number 
Java :: before first method in jdbc 
Java :: place.getlatlng() returning null 
Java :: tenth digit is odd in c 
Java :: kill no entity was found minecraft 
Java :: Obtaining all data in a table with Hibernate 
Java :: Java program to find largest of three numbers using nested if 
Java :: java casting method 
Java :: what is the process of mvvm in android 
Java :: {1 2 3 4 5 } 
Java :: java Prefix Sum of Matrix (Or 2D Array) 
Java :: java initialise array in return 
Java :: fibonacci java 
Java :: android studio cannot resolve @nullable 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =