Similarities and differences between JAVA "reference type" and "Object Type" in "Inheritance"

Source: Internet
Author: User
Tags java reference

Introduced

The similarities and differences between the JAVA reference type and the object type in inheritance. This problem has been sorted out by itself N times. He was also asked N times by a rookie. So, simply tidy up here. For everyone to share.

    • In an inheritance relationship. General member variables are based on reference types
    • In an inheritance relationship. Static member variables are based on reference types
    • In an inheritance relationship, the general method is based on the object type
    • In an inheritance relationship, a static method is based on a reference type

Attention

Static member variables, static methods are based on the class, this article for testing observation. So. Static member variables and static methods are referenced using objects.


Super Class:

Package Shuai.study.inherit;public class Superclass {public String commonstring = "Superclass Common string";p ublic Stati C String staticstring = "Superclass static String";p ublic int othercommonint = 0;public Static int otherstaticint = 10;pub LIC void Commonmethod () {System.out.println ("Superclass Common Method:" + commonstring);} public static void Staticmethod () {System.out.println ("Superclass static Method:" + staticstring);} public void Othercommonmethod () {System.out.println ("superclass Other Common Method:" + othercommonint);} public static void Otherstaticmethod () {System.out.println ("superclass other static Method:" + Otherstaticint);}}

Suber Class:
Package Shuai.study.inherit;public class Suberclass extends superclass {public String commonstring = "Suberclass Common St Ring ";p ublic static string staticstring =" Suberclass static string "; @Overridepublic void Commonmethod () { System.out.println ("Suberclass Common Method:" + commonstring);} public static void Staticmethod () {System.out.println ("Suberclass static Method:" + staticstring);}}

Test Class:

Package Shuai.study.inherit;public class Test {public static void main (string[] args) {Superclass superclass = new SUBERCL ();//Common member variable is according to quote TYPESYSTEM.OUT.PRINTLN (superclass.commonstring);//Static member VA Riable is according to quote type//generally we invoke static member as superclass.staticstring, because static method &A mp Variable is based on classSystem.out.println (superclass.staticstring);//Common method is according to object TYPESUPERCL Ass.commonmethod ();//Static method is according to quote type//generally we invoke Static method as Superclass.staticmet Hod (), because static method & variable is based on Classsuperclass.staticmethod (); System.out.println ("=================================================="); Suberclass Suberclass = new Suberclass ();//INHERIT member variable from Super CLASSSYSTEM.OUT.PRINTLN ( Suberclass.othercommonint); System.out.println (suberclass.otherstaticint);//Inherit method from Super Classsuberclass.othercoMmonmethod (); Suberclass.otherstaticmethod ();//self Methodsuberclass.commonmethod (); Suberclass.staticmethod ();}}

Output:

Superclass Common stringsuperclass static stringsuberclass Common method:suberclass Common stringsuperclass static Metho D:superclass Static String==================================================010superclass Other Common Method: 0SuperClass other static Method:10suberclass Common method:suberclass Common stringsuberclass Static method:suberclass Static String


Similarities and differences between JAVA "reference type" and "Object Type" in "Inheritance"

Related Article

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.