Java-Rewritten Learning

Source: Internet
Author: User
Tags instance method

This article turns from: http://www.cnblogs.com/happyframework/p/3332243.html, thank you very much.

The rewrite rules in Java are more flexible, as follows:

    1. All instance methods except the private adornment can be overridden, and no explicit declaration is required.
    2. Overridden methods to explicitly override this concept, use @Override to annotate.
    3. Overridden methods can modify access modifiers and return types as long as they are compatible with the methods of the parent class (higher access levels and more specific return types).
    4. You can use final to mark a method as non-overridable.
    5. In the construction method, use super (XXX, XXX) to call the parent class construction method, using Super.method (XXX, XXX) in the regular instance method to call the parent class method.
    6. Java does not support overwrite (new).
public class Test {/*** @param args*/public static void Main (string[] args) {Animal Animal = new Animal (); Animal dog = new Dog (), Animal.say ();d Og.say (); Animal.eat (Animal);d og.eat (dog); System.out.println (Animal.info ()); System.out.println (Dog.info ());}} Class Animal {private String name = "Animal";p rotected Void Say () {System.out.println ("Animal" + "" + THIS.name);} public void Eat (Animal food) {System.out.println ("Animal eat" + food);} Public Object info () {return "Animal";} @Overridepublic String toString () {return "Animal";}} Class Dog extends Animal {private String name = "Dog"; @Overridepublic final void Say () {System.out.println ("dog" + "" + t His.name);} @Overridepublic final void Eat (Animal food) {super.eat (food); System.out.println ("Dog eated");} @Overridepublic final String info () {return "Dog";} @Overridepublic final String toString () {return "Dog";}}

  

Java-Rewritten Learning

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.