Java Overlay and hide

Source: Internet
Author: User

Hiding refers to a subclass that hides the properties or methods of the parent class, and when the child class is cast to the parent class, the properties and methods of the parent class are called, and overrides refer to the parent class reference to the subclass object, which invokes the specific method of the subclass.

(1) Variables can only be hidden (both static and non-static) and cannot be overwritten

(2) A static variable of the parent class can be hidden with the child class, or the static variable of the parent class can be hidden with a non-static variable of the subclass, or the final variable in the parent class can be hidden with a non-final variable (final);

(3) static method can only be hidden and cannot be overwritten;

(4) Non-static methods can be overwritten;

(5) cannot use the static method of the subclass to hide the non-static method in the parent class, otherwise the compilation will error;

(6) cannot use the subclass's non-static method to overwrite the parent class static method, otherwise the compilation will error;

(7) cannot override the final method (final) in the parent class;

(8) Abstract methods must be overridden in a specific class;

Instance:

Superclass class:

Package COM.YILONG.TEST.SCJP;

public class Superclass {

public static int I = 1;

public int j = 2;

public final int k = 3;

public static void Method1 () {

System. out. println ("Superclass Method1");

}

public void Method2 () {

System. out. println ("Superclass Method2");

}

Public final void Method3 () {

System. out. println ("Superclass Method3");

}

}

Subclass class:

Package COM.YILONG.TEST.SCJP;

public class subclass extends superclass {

public static int I = 2;//can hide the variable I of the parent class, whether it is static or not

public static int J = 1;

public final int k = 4;//can hide the variable k of the parent class, whether final or not

public static void Method1 () {

System. out. println ("Subclass Method1");

}

public void Method2 () {

System. out. println ("Subclass Method2");

}

Cannot override the final method from superclass

/*public final void method3 () {

System.out.println ("Subclass Method3");

}*/

public static void Main (string[] args) {

Superclass sc = new subclass ();

System. out. println ("i =" + sc.i);

System. out. println ("j =" + SC.J);

System. out. println ("k =" + SC.K);

SC.method1();//static method can only be hidden

SC.METHOD2 ();

Subclass SUBC = new Subclass ();

System. out. println ("i =" + SUBC. I);

System. out. println ("j =" + SUBC. J);

System. out. println ("k =" + SUBC.K);

SUBC. method1 ();

SUBC.METHOD2 ();

}

}

Printing results:

i = 1
j = 2
K = 3
Superclass Method1
Subclass METHOD2
i = 2
j = 1
K = 4
Subclass Method1
Subclass METHOD2

Supplemental: When subclasses override a method of a parent class, you cannot throw exceptions that differ from the parent class method. means that if a method of the parent class throws an exception, the subclass overrides the method without throwing an exception, but if the method in the parent class does not throw an exception and the subclass overrides the method throws an exception, it compiles the error; If a subclass throws an exception that is thrown in a method of the parent class when overridden by an exception thrown in the parent's method, it is also compiled with an error, such as Interruptedexception is thrown in the parent class method, and the subclass overrides the method when the exception is thrown.

Java Overlay and hide

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.