super class, class
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Why don't you just try it out?
posted 14 years ago
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
Posts: 11
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
sarah sturgeon
Greenhorn
Posts: 11
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 14 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Fred explained really well, but you can also think of it as a tree :
.................. /-------MountainBicycle-----...
Bicycle----- /--------RacingBicycle-------...
.................. /--------MotorBicycle--------...
.................. /-------MountainBicycle-----...
Bicycle----- /--------RacingBicycle-------...
.................. /--------MotorBicycle--------...
sarah sturgeon
Greenhorn
Posts: 11
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Jean and Fred for detailed reply. I finally got it and I really appreciate your help and patience.
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 14 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
| Screaming fools! It's nothing more than a tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |











