Inheritance Example in Java

By: Watson Viewed: 158209 times  Printer Friendly Format    


Here is the sample b for a possible implementation of a Bicycle class and demonstrates the concept of Inheritance in JAVA.

public class Bicycle {
	
    // the Bicycle class has three fields
    public int cadence;
    public int gear;
    public int speed;
	
    // the Bicycle class has one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
	
    // the Bicycle class has four methods
    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;
    }
	
}

A class declaration for a MountainBike class that is a subclass of Bicycle might look like this:

public class MountainBike extends Bicycle {
	
    // the MountainBike subclass adds one field
    public int seatHeight;

    // the MountainBike subclass has one constructor
    public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
        super(startCadence, startSpeed, startGear);
        seatHeight = startHeight;
    }	
	
    // the MountainBike subclass adds one method
    public void setHeight(int newValue) {
        seatHeight = newValue;
    }	

}

MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it. Except for the constructor, it is as if you had written a new MountainBike class entirely from scratch, with four fields and five methods. However, you didn't have to do all the work. This would be especially valuable if the methods in the Bicycle class were complex and had taken substantial time to debug.



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. where's your main sir?
View Tutorial          By: armor at 2010-12-08 01:00:45

2. Why do you need main, Sir?
MountainBike and

View Tutorial          By: SM at 2011-02-08 19:43:29

3. where is the main class dear.................
View Tutorial          By: akhand at 2011-03-01 23:58:05

4. This java file. there are So many editors are avai
View Tutorial          By: Moinatik at 2011-04-11 07:56:48

5. we know how declare constructor and methods but we
View Tutorial          By: sreekanth at 2011-05-04 02:49:29

6. You don't have to call the constructors. When you
View Tutorial          By: Watson at 2011-05-04 10:44:23

7. constructors are not to be called. when you make a
View Tutorial          By: niyati at 2011-05-28 07:57:52

8. if the "extend" word is not present does
View Tutorial          By: detteskii at 2011-06-27 09:12:23

9. hey guys the talk waz about main
hey man w

View Tutorial          By: shivam at 2011-07-09 11:45:54

10. hey this code is copied from the oracle website an
View Tutorial          By: xxx at 2011-08-10 13:34:48

11. ya its correct but tell me where is main method???
View Tutorial          By: kashiNATH MOKASHI at 2011-08-20 05:33:19

12. You only need 1 main method per application, not 1
View Tutorial          By: Michael at 2011-09-02 02:37:51

13. I executed this program with the help of below cod
View Tutorial          By: leela at 2011-09-08 12:16:24

14. copy cat from oracle official site
View Tutorial          By: abhi at 2011-11-04 14:23:12

15. Thank's for the code and this program will help to
View Tutorial          By: Ariel Delos Santos at 2012-02-20 02:28:05

16. sir u r an asshole you SUCK AT programming ! go fu
View Tutorial          By: fucker at 2012-02-24 04:11:55

17. Your code will not execute using above main method
View Tutorial          By: Gajanan Patil at 2012-05-09 04:42:29

18. main is not needed for a simple program...

View Tutorial          By: manoj at 2012-05-29 17:46:08

19. still am nt gettin. how the pgm ll execute

View Tutorial          By: kiran k at 2012-06-04 13:09:54

20. The program will work only need to change

View Tutorial          By: LEENA NARULA at 2012-06-15 06:45:06

21. Nice to learn....


Thanks,
View Tutorial          By: Udhayakumar G K at 2012-08-01 12:45:04

22. i cant understand the concept.please mail me the f
View Tutorial          By: nirmal at 2012-12-17 06:53:14

23. I lit'l modified the above program for main class,
View Tutorial          By: Adil khan at 2013-02-12 06:37:17

24. actually we dont want to explain public in int as
View Tutorial          By: sukumar at 2013-03-14 10:01:04

25. thankyou
View Tutorial          By: gomu at 2014-07-08 09:19:13