1

Since this object(stated in title) can invoke overridden methods in child class, why it can't invoke other methods of child class? I need answer as detailed as possible like memory organization, internal logic in JVM etc.

below code will give you clear understanding of my question.

class A
{
  int x=10;
  public  A()
  {
    System.out.println("Constructor of class A called!!!");
  }

  public void sayGreetings()
  {
    System.out.println("accept hye from class A");
  }
}

class C extends A
{
  int x=30;//why this is not accessed by stated object.
  public C()
  {
    System.out.println("Constructor of Class C caled!!!");
  }
  public void sayGreetings()
  {
    System.out.println("accept hye from class C");
  }
  public void ssa()
  {
    System.out.println("Sat Sri Akal ji from class C");
  }
}

public class ParentClassTypeObject 
{
  public static void main(String[] args)
  {
    C cObj=new C();
    cObj.sayGreetings();
    cObj.ssa();
    A aCObj=new C();//this is let say stated object,main object
    aCObj.sayGreetings();/*here we invoked method will be child class's 
                         overriden method.*/
    //aCObj.ssa(); //why this line gives error

    System.out.println("x="+aCObj.x);
  }
}
Bivek
  • 1,380
  • 10
  • 24
Ranjit Sran
  • 11
  • 1
  • 1
  • 3

8 Answers8

4

Because the interface you have to the object is the one you chose when you wrote:

A aCObj = new C();

If you want access to the C properties via the aCObj variable, declare it as a C.

By making it an A, you make it possible to write this later:

aCObj = new A();

So since the variable can point to an A, or a C, the compiler restricts you to accessing the methods defined by the interface exposed by the A type.

You still access C's definition of those methods, because that's one of the main points of OOP (polymorphism).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • but you didn't give answer to my question why overridem methods are accessible but not other methods of child class. ineed answer in detail .that how compiler selects overriden method in child class when we make call aCObj.sayGreetings() .why not class A 's method(sayGreetings() was called. – Ranjit Sran Mar 13 '13 at 18:23
4

Reference Variable points to the object which is of same type or the Sub set of same type.

Please consider Parent and Child are two classes Where Parent is Super Class and Child inherits the Parent Class. The below image will give you a detailed explanation. enter image description here

In the above picture, The Parent class Reference variable will search for the Parent Class Object in Child Object.It will find it , as it is there.So will give the output.And if you have the Same method in Child Class(Method Overriding) It will execute the child class overrided method.

But for the Child Class reference Variable ,It can not find out the child class object in Parent Class Object.So here,It's Not possible.

enter image description here

Hope This clear your Confusion.

3

If you compile the code you will get compile time error(not runtime error). The reason behind this is that

A aCObj=new C();

aCObj.sayGreetings();/* The compiler knows that aCobj is a reference of type A while compiling. Since compiler thinks aCobj is of type A and sayGreetings() method is present in class A so no error while calling this method */

aCObj.ssa(); /* As I mentioned above that compiler have no knowledge about run time. At run time aCobj will point to the object of type class C, but while compiling the compiler only knows that aCobj is of class A type and since class A have no such method called ssa(), you will get compile time error. */

One simple rule for object : Left side of assignment operator checking at compile time. Right side of assignment operator at run time. Consider this statement:

Parent obj =new Child(); obj.method1(); obj.method2();

Whatever method u want to call using obj reference of Parent type, those method should present in Parent class because during compile time the compiler will strictly check for those methods presence in Parent class even though it may be present in Child class.

2

The compiler decides IF you can call a method based on the type of the reference variable.So if the reference variable is of class A you can only call methods of class A.

But also the compiler decides WHICH method to call based on the actual type of the object and not the type of the reference variable starting a bottom up check on the inheritance tree.(it starts from the subclasses all the way up)

So in this case when you say aCObj.sayGreetings(); the compiler firstly checks the reference type of aCObj which is A.Class A has the sayGreetings() method so its ok.But the actual object is a C.So the compiler starts from subclass (C) to find whether this method is implemented all the way to the superclass (A).The method `sayGreetings() is overriden at C class. So it calls the C class sayGreetings() method(of the subclass).

On the other hand the ssa() method is of class C and since the reference variable is of class A the compiler gives an error when you try aCObj.ssa();

It's just polymorphism.Since an A class reference variable can be either A or C object the compiler restricts the access only to the methods that are common which are the methods of the superclass A.Next it checks whether this method is implemented at the class of the actual object (C).if it is not it moves up to the superclass (A) and calls the method of the superclass.But if it is implemented it calls the method of the subclass (C)

nahtim
  • 96
  • 5
0

Because aCObj is declared as type A so only methods declared in type A are accessible. The compiler cannot guarantee it is also of type C.

E.g.

You may also have

public class B extends A {
    public void sayGreetings() {
        ...
    }
}

This does not have the ssa method, but could still be assigned to an object declared as type A

cowls
  • 24,013
  • 8
  • 48
  • 78
  • but you didn't give answer to my question why overridem methods are accessible but not other methods of child class. – Ranjit Sran Mar 13 '13 at 18:17
  • but aCObj is making access to child class's method(overriden code is executed on call), not to it's own class method, why??it should have called it's own class method not child class's overriden method – Ranjit Sran Mar 13 '13 at 18:32
  • It is because the method is declared by A. When you execute the method it will always execute the method on the underlying implementation – cowls Mar 13 '13 at 18:35
  • yeah i agree that aCObj is of type A,as you are saying...so this object on invoking sayGreetings() should call A class's Method...agree?? but in actual it calls C class's saygreetings() method.are you getting me now??? – Ranjit Sran Mar 13 '13 at 19:27
0

The Object is of type A not C so you cannot access the instance variable i think if you make it public then you can.

3kings
  • 838
  • 2
  • 13
  • 28
0

Case 1. The reference variable of a parent class can point to an object of its child class.. Case 2. The reference variable of a parent class that is pointing to an object of its child class can be typecasted to an object of its child class.

In case 1: reference variable of a parent class can only call the methods that are defined within the parent class and also it can call the methods of child class that are overriding the methods of parent class.But cannot call the methods that are exclusively only in child class.

In case 2: reference variable of a parent class can call the methods of its child class also.

This is due to the principle of Polymorphism.

Amit Bhandari
  • 541
  • 6
  • 8
0

here is the detailed explanation of your stated query:

The answer is the intersection of "polymorphism" and "static typing". Because Java is statically typed at compile time you get certain guarantees from the compiler but you are forced to follow rules in exchange or the code won't compile. Here, the relevant guarantee is that every instance of a subtype (e.g. Child) can be used as an instance of its supertype (e.g. Parent). For instance, you are guaranteed that when you access employee.getEmployeeDetails or employee.name the method or field is defined on any non-null object that could be assigned to a variable employee of type Parent. To make this guarantee, the compiler considers only that static type (basically, the type of the variable reference, Parent) when deciding what you can access. So you cannot access any members that are defined on the runtime type of the object, Child.

The answer has been taken from the following link: Why do we assign a parent reference to the child object in Java?

Android1005
  • 155
  • 3
  • 14