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);
         }
    }
3
  • stackoverflow.com/questions/1970806/coding-to-interfaces Commented May 11, 2012 at 7:44
  • There's a syntax error if you uncomment the second line: you can't declare two times a variable. Commented May 11, 2012 at 7:45
  • @RamyAlZuhouri I think the question is why prefer line1 over line2, so either line1 or line2 is commented out. Commented May 11, 2012 at 7:46

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

try reading this: Polymorphism

Comments

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)

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.