Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmohamadreza azadi
5,167 Pointswhere is our superclass
i little bit confused about superclass(or parent class) and childclass(or subclass) what is that class ? . can anyone explain simplistic for me ?
3 Answers
Dino Šporer
5,430 PointsIf you want to have two classes, a police car and taxi car, you would write two classes with same methods and functions, That means you will be repeating yourself. But you can use inheritance, from super class car, inherit all methods and fields, and add for example field isTexiFree, or isPoliceChasingSomeone, and you would do it in few lines of code, instead of 100 without inheritance.
Dino Šporer
5,430 PointsThat class you often find in Java documentation. It's something like Object documentatio. There you can find all subclasses you been using and they inherited from parent class.
Here is good explanation!
https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
mohamadreza azadi
5,167 Pointsit's good resource but i dont know what is that class i mean i dont understant parent class what is it
Dino Šporer
5,430 Pointspublic 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;
}
} ok, now if you do this :
public class MountainBike extends Bicycle {
// the MountainBike subclass adds one field
public int seatHeight;
}
Mountain bike now will have fields cadence, gear, speed and + seatHeight ! :D
and now, you have to make a constructor. In this constructor you can call contructor from Bicycle with keyword super, something like this :
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear);
plus one variable you added in MountainBike.
seatHeight = startHeight;
}
I hope this is enough for u to understand inheritance!
mohamadreza azadi
5,167 Pointsthanks a million dude
mohamadreza azadi
5,167 Pointsmohamadreza azadi
5,167 Pointswow awsome if you can example with code for me I'm very ashamed.