The recent review of the comparison of a place to summarize through the experiment
If a is a parent class, B is declared as a subclass:
A t= new B ();
Assigns a subclass object to the parent class object, which is compiled as a parent class object, but the runtime is a subclass object:
1) is declared as a parent object;
2) has the parent class property, calls the parent class the non-subclass overrides the method;
3) Occupy sub-class memory space;
4) Cannot access variables and methods peculiar to subclasses;
5) When a subclass overrides a method of the parent class, the method of the child class is called.
1 classSubtestextendstest{2 intx, y;3 Public voidPrinthello () {4System.out.println ("hello!!");5 }6 Public voidprinthhe () {7 8 }9 }Ten Public classTest { One intme; A Public voidPrinthello () { -System.out.println ("Hello"); - } the Public voidPrinthi () { -System.out.println ("Hi"); - } - Public Static voidMain (string[] args) { + -Test a=Newsubtest (); + System.out.println (a.me); ASystem.out.println (a.x);//compilation error cannot have subclass attribute atA.printhello ();//at this point the output is subclass "hello!! " - A.printhi (); -A.printhehe ();//compilation error cannot call subclass method - } - -}
About Java up transformation