Note that when people/books/web articles refer to a class having one super-class...they mean one DIRECT superclass - i.e. you can only say "extends <class>".
In some languages (C++ comes to mind), you could say
class MyClass extends ClassA, ClassB, ClassC
thus MyClass has three direct super-classes.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Aug 27, 2011
Thanks for your replies guys.
I appreciate your help , as for the trying out my scenario, I have no idea how to try it out as to day is my third day learning Java.
" Note that when people/books/web articles refer to a class having one super-class...they mean one DIRECT superclass"class "
"MyClass extends ClassA, ClassB, ClassC"
"thus MyClass has three direct super-classes".
Sorry Fred I am lost here If there can be only one direct super class , how come MyClass has three direct super classes ?
My second question When you use the word extend what does it mean here ?
Is it possible to create a subclass out of another subclass?
in my example, I created one class mountain by-cycle 1 from subclass mountain by-cycle which has by-cycle as super class. In this scenario, will both by-cycle and mountain by-cycle be considered super class for mountain by-cycle1 class ?
sorry for being repetitive , I am just trying to understand.
I was trying to illustrate the difference between Java and C++. Both let you extend classes. this line:
is LEGAL in C++. It will compile. C++ allows for 'multiple inheritance'. One class may extend any number of other classes.
However, that line is NOT LEGAL in java. Java does NOT allow multiple inheritance. One class may ONLY extend up to one other class.
the keyword 'extend' is how you tell (in our case) Java that your MountainBike class is a sub-class of Bicycle. You would write
This is what is meant by class A being a sub-class of Class B.
Joined: Aug 27, 2011
One class extends to only one class
So if we have by-cycle as a class, mountain by-cycle class extends to by-cycle class, we can not create a class mountain cycle 1 and extends it to mountain by-cycle class because it will extending mountain by-cycle 1 class to two classes: mountain by-cycle and cycle
sarah sturgeon wrote:So if we have by-cycle as a class, mountain by-cycle class extends to by-cycle class, we can not create a class mountain cycle 1 and extends it to mountain by-cycle class because it will extending mountain by-cycle 1 class to two classes: mountain by-cycle and cycle
Let's say we have the Bicycle class:
Now, we create the MountainBicycle class:
So we can say that MountainBicycle is a sub-class of Bicycle, or that Bicycle is a super-class of MountainBicycle. Now we want a FancyMountainBicycle class. It certainly CAN extend MountainBicycle:
because we are only DIRECTLY extending ONE class. The following would NOT be legal:
So, you can only explicitly (maybe that is better than directly) extend ONE class. The class that you are extending may also have extended some other class, and that one may have extended some other class...etc.
If you create your own class and don't say "extends <whatever>", java effectively sticks in "extends Object" for you. EVERY class is somehow derived from the Object class. There may be 30 other classes in between, but each class can only have one immediate superclass.
Ok here is one example from book Head First Java that explains inheritance really well.
Ok I will take your bicylce super class as an example.
Super class is the class that everything begins from(you can look at it as a mother class or parents) the super class can have sub classes(kids) but those sub classes can have their own sub classes(more kids) but in programming languages Java is China(each class can have only one kid) and C/C++ is America(everyone can have shitload of kids)
So it means that you cant say :
because you will get a penalty(compiler wont compile your code) because Java has single inheritance(everyone can have just one kid.
But what you can do in java is this:
now bicycle had a kid mountain bicycle
I hope this cleared up the problem.
Now one more thing you sghould do when creating inheritance trees is weather your subclass is more specific type of your super class with IS-A test.
For example we want to create the Bicylce inheritance tree so we start of with a Bicycle code like this
now we want to create bigger tree I mean what is a tree without a single branch?
Lets say we want to make a Nakamura(bike brand) subclass the first thing we should do is an IS-A test.To do IS-A test we have to ask a question.In this case question would be : IS-A Nakamura more specific type of a Bike?
If the answer to your question is TRUE then you can go on with your subclass
Here is few IS-A Tests to get you started,all you need to do is give true or false answers to them,on the end is the list of TRUE answers:
Eminem IS-A Hip Hop Artist?
Chocolate IS-A Lamp?
Mouse IS-A Computer Gear/Computer Part?
JavaRanch IS-A Forum?
Table IS-A BodyPart?