Results 1 to 4 of 4
Thread: Super class refering to subclass
- 10-04-2009, 03:38 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
Super class refering to subclass
A Super class variable can reference a sub class Object.
It is the type of the variable not the type of the object which decides which member of a class has to be invoked.
What is the use of the above statements??? I mean what is the use of doing so???
why is it not applicable to OverRidden Methods (type of object decides which overridden method to be invoked, not the type of variable ).
I am confused with both these concepts, help me.
- 10-04-2009, 05:25 PM #2
It is very useful when you want to collect like things together with each item's class having different functionality and states of being:
Java Code:public class Fruit //could have been an abstract class or interface. { public void eat() { System.out.println("I'm not sure what i ate."); } }Java Code:public class Apple extends Fruit { public void eat() { System.out.println("Yum, I ate an apple."); } }Java Code:public class Orange extends Fruit { public void eat() { System.out.println("Yum, I ate an orange."); } }Java Code:public class Invisible extends Fruit { public void eat() { super.eat(); System.out.println("But i'm sure it tasted good!"); } }Java Code:... ArrayList<Fruit> fruitsAPlenty = new ArrayList<Fruit>(); fruitsAPlenty.add(new Orange()); fruitsAPlenty.add(new Apple()); fruitsAPlenty.add(new Invisible()); for(Fruit fruit : fruitsAPlenty) { fruit.eat(); } ...My Hobby Project: LegacyClone
- 10-05-2009, 04:37 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 2
- Rep Power
- 0
Thanks for the reply, but I could not understand your example. Can you please give a theoretical explanation for my question.
Esp. what is the advantage of A Super class variable can reference a sub class Object.
- 10-05-2009, 09:00 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Google and read about polymorphism.
There's also a golden programming rule which says "Code to the interface rather than to the implementation". If you use Super type variables (or interfaces) in code, then that code will work for any subclasses of that super type. If you use sub type variables(or implementations) then the code can only be used with that more specific type or subclasses of that type. So you limit the functionality of your code by using more specific types when it is possible to use more abstract types.
Similar Threads
-
Custom Class, Wrapper/Subclass?
By Moncleared in forum Advanced JavaReplies: 3Last Post: 02-09-2009, 11:08 PM -
Super class and Subclass in same source file
By makbar24 in forum New To JavaReplies: 17Last Post: 09-10-2008, 01:24 PM -
Class Reflection: Finding super class names
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:12 PM -
subclass vs inner class
By bugger in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:31 PM -
which class is superclass and subclass?
By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-11-2007, 08:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks