3
public class A {
    protected int x = 10;

    A() {
        System.out.println("Constructor A" ) ;
    }

    public void test() {
        System.out.println(" A " );
    }

    public void Aex() {
        System.out.println(" Aex " ) ;
    }
    public void testg() {
        System.out.println("Hello");
    }
}

public class B extends A {
    B() {
        System.out.println("Constructor B" ) ;
    }

    B(int num){
        this.x = num ;
        //System.out.println(this.x);
    }

    public void test() {
        System.out.println(" B " );
    }

    public int getx() {
        return this.x;
    }


}

public class C {
    public static void main( String [] args ) {
        A a = new B();
        //a.test();
        a.Aex();
        //a.testg();
        B b = new B(5);
        a = b;
        a.test();

        System.out.println( a.getx() ) ;
    }
}

Class B inherits from class A and both have a method called test(). I can create a base class reference to a derived class object , so I create a reference A a = new B(); . When I call a.test() , the test() method of B is called which I understand , which helps me to prevent conditional statements in my code. But suppose B has another method called void getx() , and I want to call that method, I cannot call it with a base class reference , since the base class has no method called void getx() . So how is polymorphism useful in such a situation ? The only way to call getx is to create a reference of derived class and call getx().

BackSlash
  • 21,927
  • 22
  • 96
  • 136
huskywolf
  • 505
  • 1
  • 4
  • 12

5 Answers5

4

Rather than classes A and B let's use classes Animal and Bird for an example.

Say your Animal class has methods eat and sleep (things that all animals do) and your Bird class also has a fly method (which only birds can do). If you have a reference to an Animal object it would make no sense to be able to call the fly method on this variable because you don't know what kind of animal it is, it could be a Slug or an Wombat.

codebox
  • 19,927
  • 9
  • 63
  • 81
  • Then what is the use of polymorphism , except for the fact that it can reach the subclasses overriding method. Apart from that is there any use ? Or when is it appropriate to create a base class reference to a derived class object ?Like Animal a = new Bird() . – huskywolf Aug 28 '15 at 14:23
  • 1
    The point is that you can provide different implementations for the shared methods - you don't have to know/care how all the different animals go about eating/sleeping you just say eat()/sleep() and know that the object will get the job done whatever its underlying type. – codebox Aug 28 '15 at 14:25
  • 1
    You would use a base class reference when you want your BIrd to be treated like any other animal (you just need it to eat/sleep), and use a Bird class reference when you need it to do something that only a bird can do – codebox Aug 28 '15 at 14:28
  • Okay it makes sense , so it helps me use overriding methods without caring about the Derived classes and hence it removes conditional statements . If I already know the type of object that I want to create then I dont have to create a base class reference to it , I can just create an object like Bird tweety = new Bird() ? . – huskywolf Aug 28 '15 at 14:31
  • Yes, but your code will be more flexible if you keep your variable types as general as possible, if you write a method that just uses `eat()` and you make the parameter type 'Bird' it means you can't use that method for any other type of animal, even though they all have an eat() method – codebox Aug 28 '15 at 14:40
3

The whole point is not to be aware of the existence of getx() method and not to call it with a reference to A.

If you really have to call it, you can cast it to the desired type:

B b = (B) a;
b.getx();

But in that case, you should reconsider your design and either change the design to avoid casting, or use a reference to B in the first place.

Imagine that you are inviting friends to a dinner. At this level of abstraction, you would just tell them to come:

for (Friend friend : friends) {
   friend.comeToDinner();
}

You would not tell them to exit their home, sit into the car, drive it, stop on red traffic light, continue driving on green light... stop at your home, exit the car. You tell them what to do, not how to do it (some will come by car, some by public transportation, etc).

Each Friend instance will use its own state and implementation to execute the comeToDinner method.

Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110
1

You already stated how it could be useful, you can call methods on a reference to base class which may actually have a value of an inherited class, this makes your code extensible.

and I want to call that method, I cannot call it with a base class reference , since the base class has no method called void getx() .

This is a contradiction, why do you want to call a method of a class that does not exist?

codebox has an excellent example in his answer of why it doesn't make sense to call the non-implemented method.

James Wierzba
  • 16,176
  • 14
  • 79
  • 120
1

What you call a deficiency is actually the key advantage of polymorphism: you can reach the subclass's overriding method without knowing anything about it or its own methods. That's what the phrase "programming against the interface" is meant to capture.

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

It seems you are missing an important piece of OO magic: the method getx() can be declared as abstract in A and you will not have to implement it there. This will allow you to call it on the base class (which will become abstract itself) and at the same time you don't need to have an implementation of the method there.

Jacob Eckel
  • 1,633
  • 1
  • 13
  • 22