Inheritance Example in Java
By: Watson
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.
Archived Comments
1. thankyou
View Tutorial By: gomu at 2014-07-08 09:19:13
2. actually we dont want to explain public in int as from second line!!!!!!!!!!!!!!!!!! and also where
View Tutorial By: sukumar at 2013-03-14 10:01:04
3. I lit'l modified the above program for main class, Its executing for me:
public class
View Tutorial By: Adil khan at 2013-02-12 06:37:17
4. i cant understand the concept.please mail me the full code which is ready to execute...
View Tutorial By: nirmal at 2012-12-17 06:53:14
5. Nice to learn....
Thanks,
http://vaalga-valamudan.blogspot.com
View Tutorial By: Udhayakumar G K at 2012-08-01 12:45:04
6. The program will work only need to change
mountainBike m = new mountainBike(1,2,300,4);
View Tutorial By: LEENA NARULA at 2012-06-15 06:45:06
7. still am nt gettin. how the pgm ll execute
without main???
View Tutorial By: kiran k at 2012-06-04 13:09:54
8. main is not needed for a simple program...
constructor gets invoked when the program runs...
View Tutorial By: manoj at 2012-05-29 17:46:08
9. Your code will not execute using above main method it will give following 2 errors while compiling
View Tutorial By: Gajanan Patil at 2012-05-09 04:42:29
10. sir u r an asshole you SUCK AT programming ! go fuck yourself
View Tutorial By: fucker at 2012-02-24 04:11:55
11. Thank's for the code and this program will help to us to understand
View Tutorial By: Ariel Delos Santos at 2012-02-20 02:28:05
12. copy cat from oracle official site
View Tutorial By: abhi at 2011-11-04 14:23:12
13. I executed this program with the help of below code
public class inheritanceBicycle {
View Tutorial By: leela at 2011-09-08 12:16:24
14. You only need 1 main method per application, not 1 per class. There is no requirement to have a main
View Tutorial By: Michael at 2011-09-02 02:37:51
15. ya its correct but tell me where is main method???is it possible to rum without maim??
View Tutorial By: kashiNATH MOKASHI at 2011-08-20 05:33:19
16. hey this code is copied from the oracle website and in that website the main is not written so the m
View Tutorial By: xxx at 2011-08-10 13:34:48
17. hey guys the talk waz about main
hey man where is ur main?
View Tutorial By: shivam at 2011-07-09 11:45:54
18. if the "extend" word is not present does that code still be an inheritance?
View Tutorial By: detteskii at 2011-06-27 09:12:23
19. constructors are not to be called. when you make an object of the class bicycle, you simply pass you
View Tutorial By: niyati at 2011-05-28 07:57:52
20. You don't have to call the constructors. When you create an instance of that class, the constructors
View Tutorial By: Watson at 2011-05-04 10:44:23
21. we know how declare constructor and methods but we need how to call the constructors.
View Tutorial By: sreekanth at 2011-05-04 02:49:29
22. This java file. there are So many editors are available in market so using that editor you can run y
View Tutorial By: Moinatik at 2011-04-11 07:56:48
23. where is the main class dear.................
View Tutorial By: akhand at 2011-03-01 23:58:05
24. Why do you need main, Sir?
MountainBike and Bicycle are public classes, so
you can c
View Tutorial By: SM at 2011-02-08 19:43:29
25. where's your main sir?
View Tutorial By: armor at 2010-12-08 01:00:45
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program