Java Inheritance Example

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.