0

I want to know about the advantage in between the creating an object for sub class but assigning class A ref instead of assigning Class B ref. which is shown in line1,line2 below code

 class A
    {
         int b=10;
     }
    class B extends A
    { 
         int b=12;

        }
    class Test
    {
         public static void main(String[] args)
         {
               A a=new B(); //line1 
               //B a=new B();//line2
            System.our.println(a.b);
         }
    }
gnat
  • 6,213
  • 108
  • 53
  • 73
user1335578
  • 2,587
  • 2
  • 18
  • 15

4 Answers4

1

If you're not going to need any methods specific to B, in other words, you're strictly going to use it as an A, it's an advantage for readability to state so.

But the main advantage comes to light when you use the general type in a method declaration:

public String resolveName(A a) { ... }

If you used B here for no good reason, then you would unnecessarily cripple your method. It could have worked for any A, but it works only for B.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

try reading this: Polymorphism

Alex Stybaev
  • 4,623
  • 3
  • 30
  • 44
0

In your short example there's no real advantage,. But in general your question is about what polymorphism is good for?

And the answer goes back to the need of OOP methodology. In short it gives you the ability to encapsulate and abstract the implementation from the usage, and can help you I the future to replace the implementation without a need to change the usage.

For more details see http://en.wikipedia.org/wiki/Polymorphism_(computer_science)

Guy Korland
  • 9,139
  • 14
  • 59
  • 106
0

In that example, I don't think you can talk about an advantage (or disadvantage), because your two lines of code each do something very different.

If the type of variable a is A, then a.b refers to the field b with the value 10.

But if you change the type of a to B then a.b refers to a totally different field, the one declared inside B, with the value 12.

If instead b was a method, then the one declared in B would "override" the one declared in A, which is a very different situation. e.g.

class A
{
     public int b() { return 10; };
}
class B extends A
{ 
     public int b() { return 12; }
}

And the calling code would be:

A a=new B(); //line1 
//B a=new B();//line2
System.our.println(a.b());

That is, we call the method b to get the return value. Now it makes no difference to the behaviour whether the type of a is A or B. What matters is the type of object that a refers to, which is always B and hence always has the behaviour defined in B, so it will print 12 regardless.

The advantage of this is that you can have a collection of objects (e.g. various kinds of vehicle) and they can be a mixture of cars, boats, trains etc. Each has its own implementation of the start method, so you can treat them all the same way.

Although generally it's clearer to define such methods in an interface, rather than a class. There is no general way to start a vehicle, so no reason to have a base class that has a meaningless implementation of start. Instead you just use an interface to declare the "shape" of the method without giving it any implementation.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284