overriding and hiding methods and variables in Java in inheritance

Source: Internet
Author: User

We know that in Java, subclasses can inherit the parent class, and if the subclass declares a method that has the same name as the parent class, we all know that if you rewrite it, there are actually two cases in which the method and variable are overwritten and hidden when inheriting.

Hide : Child hides the parent's variables and methods, then the child cannot access the parent's hidden variables or methods, but, b translates into a, you can access a hidden variable or method

Overwrite : Child overrides the parent's variable or method, then child cannot access the variable or method that the parent is overridden, and the parent's overridden variable or method is also inaccessible after the child is converted to parent

First look at the rules for overriding and hiding methods and variables in Java when they inherit

1. Instance and static variables of the parent class can be hidden by a variable of the same name in the quilt class

2. Static method of the parent class the quilt class with the same name static method hidden

3. Instance methods of the parent class overridden by an instance variable of the same name in the quilt class

There are a few more things to note.

1. You cannot use a static method of a subclass to hide An instance method that is equally marked in the parent class (that is, the return value name parameter is the same)

2. You cannot overwrite a static method that is equally marked in a parent class with an instance method of a subclass

3. Craved note that the variable will only be hidden and will not be overwritten , whether he is an instance variable or a static variable, and that a static variable of the subclass can hide The instance variable of the parent class, and the instance variable of the subclass can be hidden Static variables of the parent class

Public class Pratices {

Public static void Main (string[] args){
new Child (). Say ();//1
    }
}


class Parent{
String message = "parent";
void Say (){
System.out.println (message);
    }
}

class Child extends Parent{
String message = "Child";
}

What is the output of the 1 position?

is the parent.

So the summary is as follows:

1. Instance methods with the same name are overwritten , static methods with the same name are hidden , and the GetName instance method of the child class overrides the GetName instance method of the parent, Chind Getkind method Hides the Getkind method of the parent class

2. The difference between hiding and overriding is that when a subclass object is converted to a parent class object, it is able to access the hidden variables and methods of the parent class, and cannot access the method that the parent class is overridden

3. If you need to access an instance variable that the parent class is hidden , plus super, such as accessing the name of the parent class, write Super.name.


This article is from "Doro's blog" blog, declined reprint!

overriding and hiding methods and variables in Java in inheritance

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.