File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is the use of calling overidden sub class methods using superclass reference ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the use of calling overidden sub class methods using superclass reference ?" Watch "What is the use of calling overidden sub class methods using superclass reference ?" New topic
Author

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

Jayant Joshi
Greenhorn

Joined: Feb 03, 2012
Posts: 18
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.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11301
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jayant Joshi
Greenhorn

Joined: Feb 03, 2012
Posts: 18
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3527
    
  15
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.


Joanne
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11301
    
  16

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

Joined: Feb 03, 2012
Posts: 18
Yes its very clear now. Thanks you both Joanne Neal and Fred for nice explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the use of calling overidden sub class methods using superclass reference ?