• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the use of calling overidden sub class methods using superclass reference ?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I searched in previous posts but could not find the satisfying answer so thought of starting a new thread for it. I want to know what is the use of calling subclass overridden methods using superclass reference ? I mean we can directly call them using subclass references. What difference it will make ? For example check the below code.

class SuperReferenceCalling1
{
void method1()
{
System.out.println("Class SRC1");
}
}

class SuperReferenceCalling2 extends SuperReferenceCalling1
{
void method1()
{
System.out.println("Class SRC2");
}
}

class SuperReferenceCalling3 extends SuperReferenceCalling2
{
void method1()
{
System.out.println("Class SRC3");
}
}

public class SuperReferenceCalling {

public static void main(String[] args) {

SuperReferenceCalling1 sr1 = new SuperReferenceCalling1();
SuperReferenceCalling2 sr2 = new SuperReferenceCalling2();
SuperReferenceCalling3 sr3 = new SuperReferenceCalling3();

//PartI
sr1.method1();//Line1
sr2.method1();//Line2
sr3.method1();//Line3

//PartII
sr1=sr2; //Line4
sr1.method1();//Line5

sr1=sr3; //Line7
sr1.method1();//Line8
}
}

Output :
Class SRC1
Class SRC2
Class SRC3
Class SRC2
Class SRC3

Above in main method we can see output of Line2 of PartI is same as Line4+Line5 of PartII i.e. SRC2. But I am not getting in what scenerio calling subclass method using superclass reference could be useful ?? Hope I have made my question clear. Thanks in advance.

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point is not to do what you said...but instead, to use a superclass reference to refer to the actual object.

If I had a collection of Dogs, I don't care if the specific sub-type is a Rottweiler, a Chihuahua, or a Mutt. I want to call each object's bark() method, and it's easier to use a Dog reference for all.
 
Jayant Joshi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred thanks for the reply. I am still not that confident with collections yet. Can you please explain me the same at class level. Let me write a new code with type dog.

package justTesting;

class Dog
{
void bark()
{
System.out.println("dog barks");
}
}

class Rottweiler extends Dog
{
void bark()
{
System.out.println("Rottweiler barks");
}
}

class Chihuahua extends Dog
{
void bark()
{
System.out.println("Chihuahua barks");
}
}

public class DogBarking
{
public static void main(String args [])
{
Dog dg = new Dog();
Rottweiler rt = new Rottweiler();
Chihuahua ch = new Chihuahua();

dg.bark();//line1
rt.bark();//line2
ch.bark();//line3

dg=rt;//line4
dg.bark();//line5

dg=ch;//line6
dg.bark();//line7 }

}
Output:
dog barks
Rottweiler barks
Chihuahua barks
Rottweiler barks
Chihuahua barks

Now if you see the above code I think using line2 is far better than using line4+line5 put together. I know its not only about reducing number of lines of code or about memory. There is more to it in a contexual sense. I know what you are trying to say does make sense but I cant able to pursue it clearly. So can you elaborate it a bit further. Thanks.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code

Jayant Joshi wrote:

Now if you see the above code I think using line2 is far better than using line4+line5 put together. I know its not only about reducing number of lines of code or about memory. There is more to it in a contexual sense. I know what you are trying to say does make sense but I cant able to pursue it clearly. So can you elaborate it a bit further. Thanks.


What happens when you have hundreds of dogs ? Are you going to create a reference variable for every dog ?
What you should be doing is putting them in a collection

You now know that every object in the List is a Dog - what type of dog is irrelevant because you are using a Dog reference variable to access them.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There may even only be one dog...but you'd still want to use the supertype.

Remember that in the 'real world', you will not be writing every line of code. In fact, you may rely on code written by a different company. You don't really know what they are going to return.

Say they write a DogCatcher class. It does its job, and picks up a stray dog off the street. You are writing the DogPound class. You are going to call the DogCatcher.getDog(); method. He may return any kind of dog. You don't want to have to code for getting any of the 157 breeds recognized by the AKC. You want a generic way to handle ANY kind of dog returned by the other class.

This is also the reason for interfaces. You can have your reference variable be of the interface type, and then it doesn't matter what is returned, as long as it implements the interface.

 
Jayant Joshi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes its very clear now. Thanks you both Joanne Neal and Fred for nice explanation.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic