-1
public class A {
    public void m1() {
        System.out.println("A m1");
    }
}

public class B extends A {

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

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

}

public class Result {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    /*A a = new A();
    a.m1();

    B b = new B();
    b.m1();
    b.m2();
    */
    A ab = new B();
    ab.m1();
}

}

In the above code, I have a Class A and Class B that extends Class A. In class C I am creating the object of class B and it's been assigned to class A. when I try to call ab.m1() it calling the method in class B.but when I try to call ab.m2() i get compile time error. I am not understanding why class B method is called during ab.m1(). can someone help me in understanding the concept much better.thanks in advance.

  • `.m2()` does not exists on references to class `A` only on instances of `B` that are assigned to a reference to `B`. So `ab` is an instance of `A` which does not have the method `.m2()`. Your IDE should show you this very clearly. If you typed `ab.` it should only show you methods from `A`. `B` is an `A` but not the other way around. `A` has no knowledge of `B` or what it provides. –  Jun 25 '18 at 17:58
  • @feelingunwelcome This is not a duplicate of that question. The one you linked to has a terribly broad title and some specific questions in the body. It should not be used as a dupe target. – Bill the Lizard Jun 25 '18 at 18:00
  • Both the questions is about inheritance but my problem is different from what is being asked in that question. – Keerthi Prasad B V Jun 25 '18 at 18:02
  • 2
    ["*A pox on the ISA relationship. It’s been misleading and damaging for decades. Inheritance is not ISA. Inheritance is the redeclaration of functions and variables in a sub-scope. No more. No less.*" -- Robert Cecil Martin](http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/#comment-436689) – Turing85 Jun 25 '18 at 18:03
  • Are you asking why `B`'s `m1()` method is called instead of `A`'s `m1()`? If so, since `ab` is an instance of class `B`, its `m1()` method takes precedence and overrides all of its parent class' `m1()` methods. In the same way all objects have a `toString()` method because the `Object` class has a `toString()` method, but any implementation in the lower classes overrides it. – Nordii Jun 25 '18 at 18:09
  • `[teach-me]` You're asking the compiler to find method `m2` *via eference* to class `A`. Compiler tells you that instances of class A don't have `m2`. And compiler is right. There are at least SOME instances of `A` that don't have `m2` - those that are not instances of `B`. You think that compiler remembers that `ab` was assigned an instance of `B`, but it's not the case. All it knows is that you have "some sort" of `A`: could be `A`, could be some unknown `C`. The only thing to go on is what's available in `A` itself. Oh, and yes - the inheritance is most often the wrong tool for the job. –  Jun 25 '18 at 18:12
  • @Nordii will it check m1() of class A? If an is an instance of class B then it should call m2() also – Keerthi Prasad B V Jun 25 '18 at 18:30

2 Answers2

1
A ab = new B();

You create a reference of type A and point it to an object of type B. This is valid and legal because B extends A, so an object of type B IS-A type A. That's why, when you call ab.m1(); you're calling the method defined in B. Your object type is B, so it overrides the method defined in A.

However, you can't call ab.m2() because the reference is type A, and knows nothing about new methods defined in B that weren't included in A. The reference only has access to the things defined in A (and anything that it might inherit from its super types, like Object).

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
-1
B -->A// B is-a A
A ab = new B()//object of B is created and reference variable is type of Super type as A is super class of B

since ab.m1() is type of A class and can be accessible through super variable. ab.m2() is type of B class and can not be accessed using super type reference, thats where down casting comes into picture.

try:

B a = (B)ab;// down casting
a.m1();
a.m2();

now you call both method of B class.

chand mohd
  • 2,363
  • 1
  • 14
  • 27
  • Thank you. But I am not worried about down casting, I am looking how the object creation and reference type work so that respective methods being called. – Keerthi Prasad B V Jun 25 '18 at 18:25
  • @KeerthiPrasad look at the Inheritance B extend A that mean B have all the member of its super class except private and final. `A ab = new B()` means Object of B is created and reference `ab` is type of super class i,e A which point to B object. using `ab` we can not call any sub class method or variable becoz super class is not aware of anything in sub class. – chand mohd Jun 25 '18 at 18:39