Overload in Java (method override)

Source: Internet
Author: User

Method Overwrite (overload) is very similar to the overload of the method, and it also has important applications in Java inheritance.

The writing program may encounter the following situation, the method already implemented in the parent class may not be accurate enough to satisfy the subclass

's needs. For example, in the preceding Animal class, thebreath method is too simple, for fish animals to breathe with gills,

And for mammals, breathing with the lungs, how to do it,Java provides a way to overwrite is to solve this problem.

In the following program, you first define a parent class Animal, and then define the Animal 3 sub-class Tiger,Fish

and Dog, 3 methods were provided in the parent class eat,breath,sleep, in two subcategories of Tiger and re-defined in Fish

The breath method and did nothing in the Dog class. In Overloaddemo , you create a Fish object, a

Tiger object and a Dog object, respectively, calling the breath method.

classAnimal {String type;//typeString name;intAge ;//name//Ageintweight;voideat () {//Weight//How to eatSystem. out. println ("animals love to eat");}voidBreath () {System. out. println ("Animal Breath");}voidsleep () {//Breathing Methods//Sleeping MethodsSystem. out. println ("The animals are sleeping .");}}//Tiger class inherits animal classclassTiger extends Animal {String tigertype;//Tiger SpeciesString from;//define Tiger Unique variables//Tiger's Own methodvoidTigerrun () {//how the tiger runsSystem. out. println ("The tiger is running .");}voidBreath () {//inherited breathing MethodsSystem. out. println ("the tiger breathes with the lungs.");}}//fish inherits Animal classclassFish extends animal{string fishtype;//Fish's own way.voidswim () {System. out. println ("the fish is swimming .");}voidBreath () {System. out. println ("the fish breathes with their gills.");}}classDog extends animal{} Public classoverloaddemo{ Public Static voidMain (string[] args) {//declaring three different objectsTiger tiger=NewTiger (); Fish Fish=NewFish ();D og Dog=NewDog ();//both call the breath methodTiger.breath (); Fish.breath ();d Og.breath ();}}

The result of the program operation is:

The tiger breathes with the lungs.

The fish breathes with their gills.

Animal Breath

After the method is overwritten, if it needs to be called again, it can be implemented using the Super keyword, such as below.

classAnimal {String type; String name;intAge ;intweight;voideat () {System. out. println ("animals love to eat");}voidBreath () {System. out. println ("Animal Breath");}}classTiger extends Animal {String tigertype; String from;voidBreath () {//call the breath method of the parent class with the Super keywordSuper.breath ();//call the animal's breathing methodSystem. out. println ("the tiger breathes with the lungs.");}} Public classsuperdemo{ Public Static voidMain (String args[]) {Tiger Tiger=NewTiger (); Tiger.breath ();}}

In the subclass Tiger of Animal, in the Breath method, use the statement Super.breath (); Call the breath of the parent class

Method. The running results of the program are as follows:

Animal Breath

The tiger breathes with the lungs.

The Super keyword has the following two uses:

Call the parent class constructor in the subclass constructor.

Call the method of the parent class in the subclass.

Overload in Java (method override)

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.