








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









public class Test { public static void main(String[] args) { Student stu = new Student(); stu.setName("John Smith"); stu.setStuNum("12345"); System.out.println("Student Name: " + stu.getName()); System.out.println("Student Number: " + stu.getStuNum()); } }
class Bicycle { int speed; int gear; Bicycle(int s, int g){ speed=s; gear=g; } void changeGear(int newValue){ gear = newValue; } void speedUp(int increment) { speed = speed + increment; } void applyBrakes() { speed = 0; } void printStates() { System.out.println(" speed:"+speed+" gear:"+gear); } }
public class Test { public static void main(String args[]) { MountainBike m=new MountainBike(24,3,5); m.printStates(); System.out.println(" seat height=" + m.getHeight()); m.changeGear(2); m.speedUp(54); m.setHeight(34); m.printStates(); System.out.println(" seat height=" + m.getHeight()); } }
Practice Now override printStates() method in child class Create objects of parent class & child class in main() Call methods for parent class in the following sequence printStates() speedUp() changeGear() applyBrakes() printStates() Call methods for child class in the following sequence
class MountainBike extends Bicycle{ int seatHeight; MountainBike(int seat,int s, int g){ super(s,g); seatHeight=seat; } public void setHeight(int h){ seatHeight=h;} public int getHeight(){ return seatHeight;} void printStates() { System.out.println("Sub Class: speed:"+speed+" gear:"+gear + " Seat height:" + seatHeight); } }