0
 class AA {  
      void run()
          {
             System.out.print("In AA");
          }  
     }  
 class B extends AA{
          void run()
         {
             System.out.print("In B");
         }  
     public static void main(String z[])
        {  

      //upcasting 
      AA a= new B();
      a.run();
    }  
} 

Upcasting here working fine but now i m trying to call Parent class method like this..

B b=new AA();

It gives compile time error so cast it.

 B b=(B) new AA();

However, I'm not sure why it's not working how I expect. Any thoughts?

learner
  • 365
  • 1
  • 3
  • 16

4 Answers4

2

What

B extends AA

mean, is that B is an AA, but AA is not a B. Let's give an example:

Employee extends Person

This means that an Employee is a Person, but a Person is not necesarily an Employee.

When you do String literal is not properly closed by a double-quote AA a = new B();

the reference type of a will be AA, but the object type will be B. This kind of declaration only allows to call methods declared in AA.

Doing

B b = new AA();

won't work because an AA is not a B, as explained with the example above.

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

a B class IS an instance of AA; however, an AA class IS NOT and instance of B

the Javac compiler has no way of knowing that there exists a class B that extends AA when you declare new AA();

if you're asking behind the theory of this, the suppose you also have another class that derives AA, such as:

public class C extends AA {
    @Override void run() { System.out.println("i do something different");}
}

now, when you call new AA(), how do you know what AA can be casted to? what is called when you call run() ? is it the run that belongs to B ? or C ?

if it were to work polymorphically, you would need to have instantiated both a B and a C class. but wouldn't this be a total waste of space, for every children of AA ?

David T.
  • 22,301
  • 23
  • 71
  • 123
0

I don't know how far you have gotten with polymorphism in Java, but that is a concept you should really understand well because it is at the base of OOP.

So, using some ASCII class diagrams:

AA <- B

B is a subclass of AA. A specialized type of AA, if you want to put it in other words. So you can use a AA ref = new B();.

On the other hand, AA is not an instance of B. You cannot have a specialization of AA be used when the reference is of type B.

Let's look at an example:

Animal <- Cat
       <- Dog
       <- TyrannosaurusRex 

We can all agree that Cat, Dog and TyrannosaurusRex are all animals, so you can use an Animal a type to point to Cat, Dog or TyrannosaurusRex. On the other hand, a TyrannosaurusRex rex can not be any animal, so it is normal that TyrannosaurusRex rex = new Animal() should give an error.

Daniel
  • 179
  • 2
  • 9
0

First you need to study the reference variable

Object str =new Object() // this is Correct.
String newString = (String) str // this is not allowed because str object does not have the reference of String 

But you are casting into String still it not work.

Actually str is reference is Object. 

Clear.

now your problem is solve

B has an reference of AA but 
AA  class has not reference of class B 
JegsVala
  • 1,789
  • 1
  • 19
  • 26