Table of Contents
- 1 Why do we assign a parent reference to the child object in Java?
- 2 Can we assign child reference to parent object in Java?
- 3 Can an object of a child type be assigned to a variable of the parent type in Java?
- 4 When reference variable of parent class refers to the object of the child class then it is known as?
- 5 What is parent class and child class in Java?
- 6 When you assign super class reference variable to sub class object you can achieve which mechanism in Java?
- 7 Can parent class create child class object?
- 8 Can you call a parent class method from child class object?
- 9 Can a parent class reference a child class object in Java?
- 10 What is the reference variable of the parent class?
- 11 Why can’t the child class access the members of the parent class?
Why do we assign a parent reference to the child object in Java?
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 ).
Can we assign child reference to parent object in Java?
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.
What happens if we create an object of the child class but use the reference variable of the parent class?
The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class. This is because the parent reference variable can only access fields that are in the parent class.
Can an object of a child type be assigned to a variable of the parent type in Java?
Yes—an object can be assigned to a reference variable of the parent type.
When reference variable of parent class refers to the object of the child class then it is known as?
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
When reference variable of parent class refers to the object of child class it is known as?
Answer: (c) Dynamic method dispatch. Q65. When reference variable of Parent class refers to the object of Child class, then it is known as. (a) Side casting.
What is parent class and child class in Java?
The class which inherits the properties of other is known as child class (derived class, sub class) and the class whose properties are inherited is known as parent class (base class, superclass class).
When you assign super class reference variable to sub class object you can achieve which mechanism in Java?
First approach (Referencing using Superclass reference): A reference variable of a superclass can be used to a refer any subclass object derived from that superclass. If the methods are present in SuperClass, but overridden by SubClass, it will be the overridden method that will be executed.
Can we assign the parent class object to the subclass as a reference?
No. It makes zero sense to allow that. The reason is because subclasses generally define additional behavior. If you could assign a superclass object to a subclass reference, you would run into problems at runtime when you try to access class members that don’t actually exist.
Can parent class create child class object?
In this line: parent p = new child(); …you’re creating a child object, not a parent object. The variable you’re using to refer to it, p , has the type parent , which means that you can only use parent stuff via that reference, but the object is a child object.
Can you call a parent class method from child class object?
If you override a parent method in its child, child objects will always use the overridden version. But; you can use the keyword super to call the parent method, inside the body of the child method. Use the keyword super within the overridden method in the child class to use the parent class method.
Which of these method of object class is used to obtain class of an object at run time?
Discussion Forum
Que. | Which of these method of Object class is used to obtain class of an object at run time? |
---|---|
b. | void getclass() |
c. | Class getclass() |
d. | None of the mentioned |
Answer:Class getclass() |
Can a parent class reference a child class object in Java?
If a parent reference variable is holding the reference of the child class and we have the “value” variable in both the parent and child class, it will refer to the parent class “value” variable, whether it is holding child class object reference.
What is the reference variable of the parent class?
The reference variable of the Parent class is capable to hold its object reference as well as its child object reference. In Java, methods are virtual by default (See this for details).
How do you know if a variable is parent or child?
+ par.value); Output: If a parent reference variable is holding the reference of the child class and we have the “value” variable in both the parent and child class, it will refer to the parent class “value” variable, whether it is holding child class object reference.
Why can’t the child class access the members of the parent class?
The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class. This is because the parent reference variable can only access fields that are in the parent class.