How do I access a method or variable of the same name in Java?

Source: Internet
Author: User
Tags instance method

/* Class variable: Also called a static variable, which belongs to the class, and the class variable can be accessed through the class name. Instance variable: an instance of a class is an object that can access an instance variable through an object, but cannot access an instance variable through the class name. Static methods: Methods belong to classes, access instance methods through objects, and cannot access instance methods through the class name. Hide: b Hides the variable or method of a, then B cannot access a hidden variable or method, but after converting B to a, you can access a hidden variable or method. Overwrite: b overrides the variable or method of a, then B cannot access a overridden variable or method, and when B is converted to a, the same cannot be accessed by a overridden variable or method. The rules for overriding and hiding methods and variables in Java are as follows: the instance variables and class variables of the parent class can be hidden by the same name variable of the class. The static method of the parent class is hidden by the same name as a static method of the quilt class. Instance methods of the parent class are overridden by the instance method of the quilt class with the same name. You cannot use a static method of a subclass to hide an instance method that is similarly identified in the parent class, or the compiler will make an error. You cannot overwrite a static method that is similarly identified in the parent class with an instance method of the child class, or the compiler will error. The final method (the method with the keyword final) cannot be overwritten. The variable will only be hidden and will not be overwritten, whether she is an instance variable or a static variable. Also, a static variable of a subclass can hide an instance variable of the parent class, and an instance variable of the subclass can hide the static variable of the parent class. */public class Accessmethod {public static void main (string[] args) {//test inheritance is covered by the hidden problem System.out.println ("in the inheritance relationship, The contents of the overridden and hidden variables of the same name are as follows: "); ChildClass child = new ChildClass (); System.out.println ("\ t created the ChildClass object, ChildClass property information is as follows:"); System.out.println ("Name:" + Child.name + "; Age: "+ Child.age +"; var: "+ Child.classvar");//convert ChildClass type to ParentClass object parentclass parent = child; SYSTEM.OUT.PRINTLN ("assigns the created ChildClass object to the ParentClass object, whose property information is as follows:"); System.out.println ("Name:" + Parent.name + "; Age: "+ Parent.age +"; VAR: "+ parent.classvar); System.out.println ("Subclass can access the instance variable that the parent class is hidden by name:" + child.getparentname ()); System.out.println ("Subclass can access the instance variable that the parent class is hidden by age:" + child.getparentage ()); System.out.println ("Subclass can access the parent class is hidden static variable var:" + Child.getparentvar ()); System.out.println ("\n<<----------------<<------------>>------------------>>\n");// Re-test the inheritance of the method cover and hide Problem System.out.println ("in the inheritance relationship, the same name method overrides and hides the execution content as follows:"); Child.getname ();//instance method Child.getclassvar ();// static method Parent.getname ();//instance method Parent.getclassvar ();//static method}}class Parentclass{public static string Classvar = "String class variable of parent class ";p ublic static int age = 50;//class variable, parentclass ages public String name =" ParentClass ";//instance variable, ParentClass name//static method, Gets the class of ParentClass public static String Getclassvar () {///static method that can only be manipulated in a type variable, static variable System.out.println (" ParentClass's Getclassvar () method is called! "); return Classvar;} static method that gets the age of ParentClass public static int getage () {///static method can only be a class variable, static variable System.out.println ("ParentClass getage") Method is called! "); return age;} Instance method, gets the name of the ParentClass public String getName () {///The operation in the instance method can beinstance variable, or it can be a class variable System.out.println ("ParentClass's GetName () method is called! "); return this.name;} Final method, reduce the age of ParentClass by 1public final int getpreviousage () {System.out.println ("ParentClass getpreviousage () Method is called! "); return--age;}} Class ChildClass extends Parentclass{public static string Classvar = "String class variable of subclass";p ublic int age = 25;//instance variable, ChildClass, The ParentClass class is static public String name = "ChildClass";//instance variable, ChildClass name//static method, gets the category public static of the ChildClass String Getclassvar () {///static method can only operate on class variables, static variable System.out.println ("ChildClass Getclassvar () method is called! "); return Classvar;} static method, gets the class public static String Getparentvar () {//through the class name plus "." In the parent class. Accesses a class variable that is hidden by the parent class return parentclass.classvar;} Instance method, gets the name of ChildClass public String getName () {System.out.println ("ChildClass getName () method is called!" "); return this.name;} Instance method, gets the name of the parent class public String Getparentname () {//The instance variable that is hidden through the Super Keyword parent class return super.name;} Instance method, gets the age of the parent class public int getparentage () {return parentclass.age;}} /*1. Instance methods with the same name are overwritten, and static methods with the same name are hidden. The GetName () instance method of the child class overrides the GetName () instance method of the parent class;The Getclassvar () static method hides the Getclassvar () static method of the parent class. 2. The difference between hiding and overriding is that when a subclass object is converted to a parent object, it can access the variables and methods that are hidden by the parent class, and cannot access the variables and methods that the parent class is overridden. The GetName () instance method of the parent cannot be accessed when the child object is converted to the parent class's object parent, and the child object is converted to the parent class object, and the parent can access the Getclassvar () static methods. 3. Subclasses need to use the Super keyword if they need to access an instance variable that is hidden in the parent class. The Getparentname () method of the child class uses Super.name to access the name instance variable of the parent class. 4. Subclass if you need to access a class variable that is hidden in the parent class, you need to add "." to the name of the parent class. To access. The Getparentage () method of the child class uses parentclass.age to access the age static variable of the parent class. */

How do I access a method or variable of the same name in Java?

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.