File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes super class, class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "super class, class" Watch "super class, class" New topic
Author

super class, class

sarah sturgeon
Greenhorn

Joined: Aug 27, 2011
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

Joined: Aug 16, 2005
Posts: 14338
    
  22

Why don't you just try it out?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

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
sarah sturgeon
Greenhorn

Joined: Aug 27, 2011
Posts: 11
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.

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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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

Joined: Aug 27, 2011
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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

Joined: Apr 12, 2011
Posts: 12
Fred explained really well, but you can also think of it as a tree :


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

sarah sturgeon
Greenhorn

Joined: Aug 27, 2011
Posts: 11
Thanks Jean and Fred for detailed reply. I finally got it and I really appreciate your help and patience.
vanio begic
Greenhorn

Joined: Aug 16, 2011
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?

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?









ANSWERS : 1,3,4 are TRUE
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281


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.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: super class, class