Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

superClass & subClass 2

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

Ok I understand what they mean, superClass is a class which a subClass is made up of.

A subClass will have all the state attributes & method protocol of its superClass and then extra stuff relative to its own class.

Why is super and sub used in this way...

e.g.

If you went into MacDonalds and asked to super size your Bic Mac meal, you wouldn't get a kids happy meal.

If you had a sub set of something, the sub set wouldn't necessarily contain all of the elements of the set, it's only a subset if all the elements of the subset were in the main set.

As all the elements in a subClass are not in the superClass, it's the other way round, a subClass is not a subset.

So what is the reason for this sub/super terminology.

The only thing I can think of is 'Superceed' , so the superClass goes 'before' the subClass.

Is this a correct analogy?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Having studied some more, this really confuses me...

How come you can assign a reference variable declared as a Superclass to an instance object of a Subclass?

If you tried to use a method from the Subclass protocol it would error as the declaration of the Superclass variable won't find the Subclass method as it isn't in the Superclass protocol, yet it can reference the Subclass object.

I find this very confusing.





"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
How come you can assign a reference variable declared as a Superclass to an instance object of a Subclass?
You cant.
Code:
	SubFoo sf = new SuperFoo ();
	sf.foo ();
}

class SuperFoo {} 
class SubFoo extends SuperFoo { void foo () {}}
and you named why.

don't visit my homepage:
 
Well according to my university tutor you can, so now i'm really confused.

I showed the exact same code syntax, and have been told it is perfectly ok.

In the example user I decalred a variable of a type wich was a superclass and assigned it to an instance of an object which was a subclass. It didn't error and i've been given this explanation as to why...
Code:
 The rules of Java allow that to happen, as objects can be assigned to variables with the 'type' of a superclass.

So who's right , you or them?


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
objects can be assigned to variables with the 'type' of a superclass." ist the opposite case.

List <String> ls = new ArrayList <String> ();

List <- ArrayList

List is the superclass, ArrayList a specialized subclass. ArrayList extends List.

don't visit my homepage:
 
So you're saying subclasses can be assigned to reference vars declared as the superclass. Where as the university is telling me it's the other way round.

Also what's the keyword "Is" ?

I've looked through the Java keyword list and the only two letter keywords I can find are "do" & "if".



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I think your analogy might be off. I think of it like this

I have a class Car. I can make subclasses that extend Car , Ford, Volvo, Ugo, etc. I can extend those too Explorer, CrownVictoria, etc. So a CrownVictoria is a Ford and a Ford is a Car. So both CrownVictoria and Ford have all the methods of Car, CrownVictoria has all methods from Ford, along with whatever was added in its class.

A superclass for a BigMacMeal might be a McDonaldsMeal, not a larger BigMacMeal.

Someone might correct this as an oversimplification, but super classes are more generic versions of any subclass. Object is the most generic class, and almost all (I think there are a couple special cases) other classes are subclasses of Object.

-----------------------------------------
I cannot be bought. Find leasing information at
 
jaxtell,

I understand what your saying, however, my analogy was meant to imply that 'super' of something is not less than, it's more than, in the macdonalds context.

Where as a superclass is less than a subclass, as you say a subclass extends the original class.

So a subclass is equal to the superclass and then some.

Does that make more sense?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I've thought of another analogy, which might make more sense.

Superman is not just a man he's more than a man.

Though in OO progamming Superman is a subclass of man!

Superman can do everything a that an object of class man can do and then some.

You don't call him Subman, that would imply he's an ape!


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
ls isn't a keyword, but freely named attribute or variable (a list of strings).

I can't answer what you've been told at university, but you can ask the compiler to compile something containg:

Code:
    SuperFoo sf = new SubFoo ();
    sf.foo ();
}

class SuperFoo {}
class SubFoo extends SuperFoo { void foo () {}}

note: I turned the first line around.

Your idea of superman is indeed misleading. Superclass and subclass don't specify which is more mighty. It's more about who's been first around, who depends on whom?

Let's call him Batman, and Batman is a man, is derived from a man. Batman is a man, but not every man is (a) Batman.



don't visit my homepage:
 
ls isn't a keyword, but freely named attribute or variable (a list of strings).
gotya, I saw it as a keyword or a class type as it had a capital leter to start.

Because of the way I'm being taught all variable names should start with a lowercase letter and then each sucessive word capitalised.

This helps distinguish between classes and variables as Classes should always be named starting with an uppercase letter.

Not necessarily syntax required by the language, just the way we are being taught. Good practice I guess.

So the conclusion is as I thought, super/sub are used in reverse to the way we use them in normal english / mathematics.

Superclass 'supercedes' Subclass.

Many thanks for all you input.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
crikey my eyes must be going, i saw it as a capital 'I' not an 'l'.

my bad , please accept my apologies.

That's what happens when you study too much, your eyes go all blurry!

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top