+ 1

Why do we refer an object with it's super class/interface?

public class/abstract/interface A{} public class B extends/implements A{} public class C { A a=new B; } Why are we catching the object of class B in a reference of it's super class/interface? Can the reasons be listed in points? With examples? Thank you.

16th Mar 2017, 5:19 AM
Navin Koyee Rai
Navin Koyee Rai - avatar
9 Antworten
+ 2
You don't have to, but it is exactly how you get the benefits of polymorphism. But you need more than 1 class descended from A to see this. But to make it clear lets extend your example with realistic names: public class/abstract/interface Shape{void draw(){}} public class Circle extends/implements Shape{void draw(){}} public class Square extends/implements Shape{void draw(){}} public class C { Shape a[] = new Shape[2]; a[0] = new Circle; a[1] = new Square ; for(int i = 0; i < 2; i++) a.foo(); //This will call draw() in Circle and Square } This means we can handle collections of related objects as instances of their super-class. We don't have to worry about the implementation details of the sub-classes, as long as they implemented the necessary interfaces or overridden the virtual functions correctly (and the compiler will help you catch errors here...). It is by far the most useful concept in OOP. Encapsulation, data hiding, etc are undeniably useful in itself, but polymorphism is what gives OOP its real power!
16th Mar 2017, 1:06 PM
Ettienne Gilbert
Ettienne Gilbert - avatar
+ 2
Apologies @Navin for taking so long to respond. I don't really think there are some hidden benefits to using super class references, except for polymorphism. And, since polymorphism is so fundamental to OOP, maybe lots of people just get into the habit of doing it that way. But there are also use-cases where it complicates things. For example, let's expand our sample code (and another apology, I see there was an error in the code previously) : public class/abstract/interface Shape{void draw(){}} public class Circle extends/implements Shape{ void draw(){} int radius(){return r;} } public class Square extends/implements Shape{void draw(){}} public class C { Shape a[] = new Shape[2]; a[0] = new Circle; a[1] = new Square ; for(int i = 0; i < 2; i++) a[i].foo(); //This will call draw() in Circle + Square //Get circle radius... int z = a[0].radius(); //Error; a is Shape, not Circle } In this case you need both: Shape a[] = new Shape[2]; Circle c = new Circle(); a[0] = c; //Legal, Circle is a Shape a[1] = new Square ; Then you use c.radius() instead of a[0].radius(); If you do not have a reference to a Circle, you have to 'down-cast' the Shape ref, and that requires RTTI (Run-Time Type Info). It is possible, but now you have to track what each Shape is. Sometimes you end up needing ref's to both the Superclass and Subclass, so I don't think there is a golden rule.
20th Mar 2017, 12:47 PM
Ettienne Gilbert
Ettienne Gilbert - avatar
+ 1
Thank you @Ettienne Gilbert for taking the time and answering. But since I am self learner so feel my understandings of it may not be in order. So, even though I have used myself this concept many times and do know many use cases of it, I feel like there may be something more or important as to why it is done like that. Especially I get intrigued when there is possibly no reason for object to be caught in the super class reference yet good programmers doing so.
16th Mar 2017, 1:49 PM
Navin Koyee Rai
Navin Koyee Rai - avatar
+ 1
Thank you again for so dilligently answering. I will look upon it.
20th Mar 2017, 2:40 PM
Navin Koyee Rai
Navin Koyee Rai - avatar
+ 1
@Sruthi, you can of course refer to an object by it's interface! Copy & paste the following and see for yourself: public interface Shape{ public void draw(); } public class Circle implements Shape{ public void draw(){System.out.println("..Circle..");} } public class Square implements Shape{ public void draw(){System.out.println("..Square..");} } public class Program { public static void main(String[] args) { Shape a[] = new Shape[2]; a[0] = new Circle(); a[1] = new Square(); for(int i = 0; i < 2; i++) a[i].draw(); } } Note that we are using the interface (i.e. the Shape array) reference a[i] to call the draw method in the actual objects. There would be little point to interfaces if this was not possible.
21st Mar 2017, 12:55 PM
Ettienne Gilbert
Ettienne Gilbert - avatar
0
Interface cannot refer the object but it contain the constructors. Its parent class will be accessed by the super(); keyword.
20th Mar 2017, 4:13 PM
B.sruthireddy
B.sruthireddy - avatar
0
Interface cannot refer the object but it contain the constructors. Its parent class will be accessed by the super(); keyword.
20th Mar 2017, 4:13 PM
B.sruthireddy
B.sruthireddy - avatar
0
Sorry butI didn't quite get what sruti said!
21st Mar 2017, 6:30 AM
Navin Koyee Rai
Navin Koyee Rai - avatar
0
I think sruti got typo there. May be in hurry :-)
21st Mar 2017, 1:08 PM
Navin Koyee Rai
Navin Koyee Rai - avatar