Results 1 to 4 of 4
  1. #1
    sujith123 is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

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

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    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

  3. #3
    sujith123 is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    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.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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

  1. Custom Class, Wrapper/Subclass?
    By Moncleared in forum Advanced Java
    Replies: 3
    Last Post: 02-09-2009, 11:08 PM
  2. Super class and Subclass in same source file
    By makbar24 in forum New To Java
    Replies: 17
    Last Post: 09-10-2008, 01:24 PM
  3. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:12 PM
  4. subclass vs inner class
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:31 PM
  5. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •