Inheritance, is a very important concept of OOP and Java, so we are divided into several chapters to discuss it.
What does this chapter discuss about inheritance? Why do I need inheritance?
Inheritance: Is the is-a relationship, that is, a subclass belongs to the parent class.
For example:
Package Com.ray.ch01;public class Person {private long id;private string Name;private string Sex;public long GetId () {Retu RN ID;} public void SetId (long id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} @Overridepublic String toString () {return "ID:" + getId () + "; Name:" + getName () + "; Sex:" + getsex ();}}
Package Com.ray.ch01;public class Singer extends person {public void sing () {}}
Package Com.ray.ch01;public class Boxer extends person {public void fight () {}}
We will answer below what is inheritance? And to describe the rules of inheritance.
As you can see from the code above, singer and boxer inherit the person class, which, in this case, is called the parent class (the base class), and singer and boxer are called subclasses (export classes). If an inheritance relationship exists for two classes, the subclass automatically inherits the methods and variables of the parent class, and the methods and variables of the parent class can be called in the subclass. In Java, only single inheritance is allowed, that is, a class can only be displayed to inherit from a parent class at most. But a class can be inherited by multiple classes, which means that a class can have more than one subclass.
1) subclass inherits member variables of parent class
When a subclass inherits a class, the member variables in the parent class can be used, but not all member variables of the parent class are fully inherited. The specific principles are as follows:
(1) The ability to inherit public and protected member variables of the parent class, and cannot inherit the private member variable of the parent class;
(2) for the package access member variable of the parent class, if the child class and the parent class are under the same package, the subclass can inherit; otherwise, the subclass cannot be inherited;
(3) For parent class member variables that can be inherited by subclasses, if a member variable of the same name appears in the subclass, a shadowing occurs, that is, the member variable of the child class masks the member variable of the parent class with that name. If you want to access a member variable of the same name in the parent class in a subclass, you need to use the Super keyword to refer to it.
2) Subclass inherits the method of the parent class
Similarly, subclasses do not fully inherit all the methods of the parent class.
(1) The ability to inherit public and protected member methods of the parent class, and the private member method of the parent class cannot be inherited;
(2) for the parent class of the package access member method, if the child class and the parent class under the same package, the subclass can inherit, otherwise, the subclass can not inherit;
(3) For a parent class member method that a subclass can inherit, if a member method of the same name appears in the subclass, it is called overwrite, that is, the member method of the child class overrides the Member method of the parent class with that name. If you want to access a member method of the same name in the parent class in a subclass, you need to use the Super keyword to refer to it.
Note: Hiding and overwriting are different. Shadowing is for member variables and static methods, and overrides are for common methods.
Why do I need inheritance?
(1) Reuse code
(2) Abstraction of a common agreement.
Let's give another example:
Package Com.ray.ch01;public class Animal {private String name;private long id;private String location;public void Eat () {} public void Sleep () {}}
Package Com.ray.ch01;public class Dog extends Animal {}
Package Com.ray.ch01;public class Bird extends Animal {}
Since both dog and bird are animal, it inherits the properties and methods of animal, so that the code reduces duplication and abstracts the common agreement between dog and Bird.
In the above description, we refer to the overlay method.
As explained here, for Eat () This method, dog and bird are definitely different ways of eating, so for the above code we need to make some changes.
Package Com.ray.ch01;public class Bird extends Animal {@Overridepublic void Eat () {//TODO auto-generated method Stubsuper . Eat ();}}
Package Com.ray.ch01;public class Dog extends Animal {@Overridepublic void Eat () {//TODO auto-generated method Stubsuper. Eat ();}}
Although there is a "eat" between dog and bird, there is a different way to eat, so you need to overwrite the Eat method and rewrite the way you eat it.
Summary: This section briefly discusses what inheritance is and why it needs to be inherited, and we will continue to discuss other aspects of inheritance in the following chapters.
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 (1)