Java object-oriented (inheritance)

Source: Internet
Author: User

Inherits the abstraction of a batch of classes, reduces the amount of code, facilitates the modification of the method, and manages the class.

Subclasses cannot inherit from parent class: ① constructor method ② private property and method ③ Use default modifier not under the same package

Inheritance initialization: Parent class Property, parent class, child class property, subclass constructor method

Inheritance: ① overload ② override ③ Interface ④ parent class object reference to Child class object

Rewrite rule: ① method name ② parameter list same ③ return value type is same or its subclass ④ access adornment permission cannot be strict with parent class

Private (This class) >default (same package) >protected (same package and sub-category) >public (All)


The Super/this keyword cannot be applied to the static decoration method

Package com.huidao.day10;

/* Question:

Realize the host and pet play function

Play a Frisbee game with your dog.

Play swimming games with penguins.

------Inheritance

Analysis:

How to add a Frisbee to dog Catchingflydisc ()

Add Swimming method to Penguin swimming ()

Add the play (Pet Pet) method to the owner */

public class Test03 {

public static void Main (string[] args) {

Master master = new Master ();

Pet dog = new Dog ();

Master.play (dog);

Pet penguin = new Penguin ();

Master.play (Penguin);

}

}


Class master{

Pet Pet;

public void Play (Pet pet) {

Pet.shout ();

if (pet instanceof Dog) {

Dog dog = (dog) pet;

Dog.catchingflydisc ();

} else if (pet instanceof Penguin) {

Penguin Penguin = (Penguin) pet;

Penguin.swimming ();

}

}

}


Class pet{

public void Shout () {}

}


Class Dog extends pet{

Overridden methods

public void shout () {

System.out.println ("Barking Dog");

}

Common methods

public void Catchingflydisc () {

System.out.println ("Pick up the Frisbee");

}

}


Class Penguin extends pet{

public void shout () {

System.out.println ("Penguin bark");

}

public void swimming () {

System.out.println ("swimming");

}

}


Java object-oriented (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.