Skip to main content

Casting the Objects

class A
  {
      // body of class A
  }

class B extends A
   {
      // body of class B
   }


We can cast the subclass object to a super class reference variable. i.e. Super class reference variable can hold the subclass object...

A a = new B();

But Subclass reference variable can not hold the super class object. WHY?

B b = new A();                     // Compile time error

Comments

  1. because Class B is having the data member and methods of class A along with its own data member and methods. class A cannot initialize variables of class B(B b = new A(); ).

    ReplyDelete
    Replies
    1. B b = new A();
      Here, we are calling the constructor of class A. So obviously it will initialize all the instance variables of class A. But the question is why we can't assign the object reference of class A to the reference variable of class B?

      Delete
  2. When a subclass extends a superclass, it can also have additional attributes and methods not existing in the superclass.As the variable type determines what methods you can use "on" it, a variable of a subclass type can hence expose more operations available than the superclass has implemented. That's why a variable of a subclass type can't reference an instance of a
    superclass.

    ReplyDelete
    Replies
    1. Ya, You are right.
      We use the reference variable of a class when we want to access the members of any class. So if we assign the reference of object of class A to reference variable of class A, it will be assumed that it will be having access to all the members of class B. But we are invoking the constructor of class A only, which will not have the members of class B.

      Therefore, the subclass reference variable can't hold the super-class object reference.

      Delete
  3. Good Post! Thank you so much for sharing the nice post, it was so good to read and useful to improve
    Java Training in Electronic City

    ReplyDelete

Post a Comment

Popular posts from this blog

ETE Practice Set-1

                                                                 Mock Test-1 -----------------------------------------------------------------------------------------------------------------------------   1 (a) Write a program that reads 10 integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0.                                                         ...

Practice Set: Unit-4

                                                                                                                Practice Set: Unit 4 1. What are the uses of super keyword in Java? Explain with a suitable example. 2. What are the various uses of final keyword in java? An abstract method can not be declared final. Why? 3. Differentiate between Abstract class and Interface. What do you mean by Functional interface and Lambda Expression?  ...

OLA-UBER Problem

Create two interfaces OLA and UBER both having a method public static Cab getCab() which returns the Cab object where Cab is a nested class inside OLA and UBER. Cab must have attributes cab_number and driver_name. A user rides 5 times using cabs of OLA and UBER randomly which are stored in an array. Now implement a method which should display the name of the driver and the company name i.e. OLA/UBER when the valid cab_number is entered by the user. Display "Invalid CAB details" when the cab_number entered by the user is incorrect.