The understanding of Java inheritance

Source: Internet
Author: User

Inheritance Overview:

In object-oriented programming, many objects share some common features, and we extract the common features of these objects to create a new parent class that inherits the private properties and methods of the parent class, so that we avoid writing a lot of repetitive code when defining multiple different objects. Before using inheritance we want to define the definition and role of inheritance. Why do we need to inherit. And how to implement inheritance, what are the benefits of inheritance.

1. What is inheritance? Why use inheritance? The key word for inheritance ?

① inheritance is a relationship that satisfies the is-a between classes, reflecting the special case that a class (subclass) is another class (The parent Class)

② multiple objects have some of the same properties and methods to satisfy the is-a relationship, we can extract the same attributes and methods between objects to reconstruct a class (parent class). Inheritance allows you to get private properties and methods in the parent class and to define the unique properties and behavior of subclasses (properties and methods that are not in the parent class) through the method extension of the instance. This avoids the large amount of code redundancy that occurs when a large number of objects with the same characteristics are created.

③ inherited keywords are: extends

is multiple inheritance supported in 2.Java?

The Java language is a single inheritance multiple implementation (inheriting a direct parent class, implementing multiple Interfaces).

3. Can subclasses inherit the constructor method of the parent class?

Subclasses cannot inherit the constructor method of the parent class, and if there is a display constructor in the parent class, the subclass calls the constructor method in the parent class. (using super () to invoke the display construction method in the parent class)

4. Can subclasses directly access the private properties of a parent class?

Subclasses cannot directly access private properties in the parent class, and subclasses inherit private properties and methods from the parent class, which can access the parent class's own property through methods exposed by the parent class.

5. How do subclasses directly access the public properties of a parent class?

The way of Super. Properties

6. How does a subclass call an instance method of a parent class?

Subclass invokes an instance method in the parent class by overriding the parent class method with an instance method

7. What are the characteristics of the rewrite?

① method Name Same, method return value type same, parameter list same, not in same class

② General method overrides, method body different

③ subclasses can only elevate the access rights of the parent class method When overriding the parent class, and cannot shrink

8. Can the method of making be rewritten?

The constructor method cannot be overridden, only called, and calls the constructor method in the parent class by means of super ()

9. Can the construction method be overloaded?

Construction methods can be overloaded, and the overloads of constructed methods can be used to define different objects

10. Which parent class is inherited by all Java classes?

All classes in Java inherit the object class

Example: Defining a animals (the animal's parent), having the name attribute, eat (eating) and sleep (sleeping) methods, defining Tigers tiger and Rabbit rabbit two subclasses, tigers and rabbits have the same sleeping behavior characteristics "output: in sleep ...." ”,

The tiger's eating behavior is "eats the meat", the rabbit Eats the behavior is "eats the Grass"

/**

* Animal Class (parent Class)

* @author Administrator

*

*/

public class Animal {

private String name;

Public Animal (String name) {

THIS.name = name;

}

/*method*/

public void Eat () {

}

public void sleep () {

System.out.println (name+ "is sleeping ...");

}

Public String GetName () {

return name;

}

/* Construction Method * *

public void SetName (String name) {

THIS.name = name;

}

}

/**

* Tiger Class (subclass)

* @author Administrator

*

*/

public class Tiger extends Animal {

Public Tiger (String name) {

Super (name);

}

/* Method Override */

public void Eat () {

System.out.println (GetName () + "eat meat");

}

}

/**

* Rabbit Class (Subclass)

* @author Administrator

*

*/

public class Rabbit extends Animal {

Public Rabbit (String name) {

Super (name);

}

@Override

public void Eat () {

System.out.println (GetName () + "grazing");

}

}

public class Testanimal {

/**

* Test class

*/

public static void Main (string[] args) {

Rabbit kitty=new Rabbit ("Kitty");

Kitty.eat ();

Kitty.sleep ();

Tiger Xinba=new Tiger ("Xin Ba");

Xinba.eat ();

Xinba.sleep ();

}

}

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.