Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# super class, class

sarah sturgeon
Greenhorn
Posts: 11
Hi everybody

How is everyone doing ?

I was reading about inheritance with regard to classes. One of the link says a super class can have infinite number of subclasses but all all subclasses will have only one super class.

Let say we a by- cycle as a super class. Let assume mountain bike is one of the subclass of super class by-cycle.

Is it possible to create one class mountain by-cycle with mountain bike as super class ( keeping in mind class mountain by-cycle is already a subclass of super class by-cyle)?

Simply put is it possible to create a subclass from a class which is already a subclass of super class ?

If yes, then what would be considered super class for the subclass mountain by-cycle1 ? will it be Mountain by-cycle or by-cycle?

thanks and have a great day.

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15571
46
Why don't you just try it out?

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
• 1
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.

sarah sturgeon
Greenhorn
Posts: 11

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.

Fred,

" 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.

Thanks

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
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.

sarah sturgeon
Greenhorn
Posts: 11
Thanks Fred

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

Is my understanding correct ?

thanks

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
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

No.

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.

Jean Fontaine
Greenhorn
Posts: 12
Fred explained really well, but you can also think of it as a tree :

.................. /-------MountainBicycle-----...
Bicycle----- /--------RacingBicycle-------...
.................. /--------MotorBicycle--------...

sarah sturgeon
Greenhorn
Posts: 11
Thanks Jean and Fred for detailed reply. I finally got it and I really appreciate your help and patience.

vanio begic
Greenhorn
Posts: 5
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?

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?

Javin Paul
Ranch Hand
Posts: 295

Some time this is reffered more clearly as "multi level inheritance" and "multiple inheritance". Java supports multi-level inheritance but not multiple inheritance.

Class A{

}

Class B extends A{

}

Class C extends B {

}

is an example of multi-level inheritance.

Class C extends B, A{

}

is an example of multiple inheritance , not supported in java.