Packageday07;classfu{intnum = 5; }classZiextendsfu{intNum =7; voidShow () {intNum =9; SYSTEM.OUT.PRINTLN (num); }} Public classExtends { Public Static voidMain (string[] args) {Zi Zi=NewZi (); Zi.show (); }}
9//don't look for it.
Packageday07;classfu{intnum = 5; }classZiextendsfu{intNum =7; voidShow () {intNum =9; System.out.println ( This . Num); }} Public classExtends { Public Static voidMain (string[] args) {Zi Zi=NewZi (); Zi.show (); }}
7
Packageday07;classfu{intnum = 5; }classZiextendsfu{intNum =7; voidShow () {intNum =9; System.out.println (Super . Num); }} Public classExtends { Public Static voidMain (string[] args) {Zi Zi=NewZi (); Zi.show (); }}
5
Super represents the parent class, this represents a reference to this class of objects, and super represents the space that the parent class belongs to
Note: A property with the same name usually does not appear in the subclass parent class, and since the NUM attribute is already defined in the parent class, there is no need for the subclass to define the property
Subclasses cannot override the Show method in the parent class because the types are different
Packageday07;classfu{ Public Static voidShow () {System.out.println ("Run Show1"); } Public voidShow2 () {System.out.println ("HAHA1"); }}classZiextendsfu{ Public Static voidShow () {System.out.println ("Run Show2"); } Public voidShow2 () {System.out.println ("HAHA2"); }} Public classExtends { Public Static voidMain (string[] args) {Fu F=NewZi ();//note the creation of this local object, and the difference between the result, the static method, and the member variable and the common methodf.show (); F.show2 (); }}
Run Show1
Haha2
Contrast:
Packageday07;classfu{ Public Static voidShow () {System.out.println ("Run Show1"); } Public voidShow2 () {System.out.println ("HAHA1"); }}classZiextendsfu{ Public Static voidShow () {System.out.println ("Run Show2"); } Public voidShow2 () {System.out.println ("HAHA2"); }} Public classExtends { Public Static voidMain (string[] args) {Zi Z=NewZi ();//note the creation of this local object, and the difference between the result, the static method, and the member variable and the common methodz.show (); Z.show2 (); }}
Run Show2
Haha2
Note When covering:
1. When the subclass overrides the parent class, ensure that the permission of the overriding method is greater than or equal to the overridden function.
2, coverage method has static Monk's time, static can only cover static, or be static cover
Java Inheritance Example