Can a child reference point to parent object?

Can a child reference point to parent object?

The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.

Can we create object of parent class method with child class?

The reference variable of the Parent class is capable to hold its object reference as well as its child object reference.

Can a child class access private methods of parent class?

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class—both fields and methods.

Why can’t we assign parent class object to child class reference?

Why can’t we assign parent class object to child class reference? (Java in General forum at Coderanch) Why can’t we assign parent class object to child class reference? Er. Rohit Sharma This is the basic concept of OOP’s in java.

Why are child and parent classes the same in Java?

This is because the parent reference variable can only access fields that are in the parent class. Thus the type of reference variable decides which version of “value” will be called and not the type of object being instantiated. It is because compiler uses special run-time polymorphism mechanism only for methods.

How to call a child class method from a parent class object?

In this situation your ‘this’ pointer will reference your child class object if you are calling parent method through your child class object. Let me know if still you got stucked somewhere.

How to access child class members in Java?

The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class. It is because compiler uses special run-time polymorphism mechanism only for methods. It is possible to access child data members using parent pointer with typecasting.