Understanding java-1.5 Inheritance from the beginning (2)

Source: Internet
Author: User

Then, in the previous section, we'll continue to introduce inheritance.

1. The order of lookup methods when subclasses invoke methods:

Let's continue with the animal code for the above section for example:

Package Com.ray.ch01;public class Animal {private String name;private long id;private String location;public void Eat () {S Ystem.out.println ("Animal is Eating");} public void Sleep () {}}

Package Com.ray.ch01;public class Bird extends Animal {@Overridepublic void Eat () {System.out.println (' Bird is eating ');} public static void Main (string[] args) {Animal bird = new Bird (); Bird.eat ();}}


Output:

Bird is eating


Package Com.ray.ch01;public class Dog extends Animal {public static void main (string[] args) {Animal dog = new Dog ();d OG.E at ();}}


Output:

Animal is eating


Let's look at the class diagram first:


Based on the class diagram, it is clearer to call the order of the methods:

1) Now implement the class to find, that is, the class after new to find, if not, turn 2

2) to the parent class to find, has been such a circular upward layer to find, so far, or error.


2. Key points of succession.

(1) Subclasses are extend from the parent class.

(2) Subclasses inherit the methods and instance variables of the parent class public and protected, but do not inherit private.

(3) Inherited methods can be overridden, but instance variables are not available.

(4) Verify the rationality of inheritance by is-a test.

(5) The is-a relationship is unidirectional, and the bird is an animal, but the animal is not necessarily a bird.

(6) When the method is overwritten, call the overridden method first.

(7) If X is the parent of Y and Y is the parent of z, then X is also the parent of Z.


3.is-a and Is-like-a

"is a" and "like a"


Let's look at the class diagram above and compare the class diagram of the previous animal, and the subclass of this class diagram obviously has a few other things.

The animal's class diagram We think is a complete replacement, there is no other method in the subclass, is the ideal way of inheritance, but in the real world, often need to add their own unique methods in the new classification, this time can only say "like a", rather than the "is a", of course, this is also the way of inheritance, It's just not the absolute replacement, but it's more adapted to the real needs.


Summary: In this chapter we discuss the lookup order of calling subclass methods and the inherited relationships.


This chapter is here, thank you.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Understanding java-1.5 Inheritance from the beginning (2)

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.