Java Basics Trap (iv)

Source: Internet
Author: User
Tags access properties throw exception

This article is published in my blog.

Today we talk about Java inheritance and reflection related issues, we first look at the following code, can be compiled through no, why specifically say why?

public class test{public        static void Main (string[] args) {person            p = new Man ();            P.talk ();        }    }        Class person{public        void Talk () throw nullpointerexception{            System.out.println ("person is Talking");        }    }        class Man extends person{        protected void Talk () throw exception{            System.out.println ("Man is Talking"); c13/>}    }

  

If you pay more attention to this question, it is obvious that there is a problem, and this is a question about how to override the parent class in succession.

We know that to implement override you must return a type, a method name, and a parameter list that matches the override, but there are some rules that need attention, such as access level, exception throw. The above code cannot be compiled, there are 2 problems: First, the subclass overrides the parent class method when the access level must be greater than or equal to the parent class access level, and the second is that the subclass overrides the parent class method when the exception range must be less than or equal to the parent class exception. The above code slightly modified under the can, the protected changed to public, abnormal eception changed to nullpointerexception so that it conforms to the compilation passed! Of course, you can change the code slightly (in reverse) as follows:

class person{    protectedvoidthrows  exception{        System.out.println ( "Person is talking");}    } class extends person{    publicvoidthrows  nullpointerexception{        System.out.println ("man is Talking");}    }

The access level of the child class's talk method is greater than the access level of the parent talk method, and the subclass's Talk method's exception nullpointerexception is less than equal to the parent talk method's exception exception.

Can you change the name member in the demo class below, and what can I do?

class demo{    private String name = "Hello";          Public int Age ;          Public String GetName () {        return  name;    }      Public int Getage () {        return age ;    }    }

This question everybody knows actually is asks the reflection, in the Java inside class all members including public, private in front of reflection all can be invalid, is not think this reflection is too powerful ah! We can implement the modified field by using the following methods: 1. Syntax of the class, 2. Pass the GetClass () method of the Class object, and 3. The forname () method of the Classes object.

Look at the Code name field is a private field, then we look at using the GetField () method, view the next JDK help document can be concluded that it just returned to the public member and cannot return to private, how to get the private then we can use Getdeclaredfield ( ) method But keep in mind that to modify its access properties with Setaccessible (TRUE) is an error, now look at the code below to modify the private and public fields:

     Public Static voidMain (string[] args)throwsException {Demo d=NewDemo (); Class<?> cl = Demo.class; //This can be returned privately, GetField (name) is just a public memberField Namefield = Cl.getdeclaredfield ("name"); //do not forget to modify its access propertiesNamefield.setaccessible (true); Namefield.set (d,"World"); Field Agefield= Cl.getfield ("Age"); Agefield.set (d,30);        System.out.println (D.getname ());    System.out.println (D.getage ()); }

We execute the code, we can see the results of the output is what we expected.

World 30

Come here first this time. Keep a record of every bit of drip!

Java Basics Trap (iv)

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.