[Java Getting Started note] Object-oriented three major features: inheritance

Source: Internet
Author: User

Understand what inheritance is

First of all, we know that there are three main characteristics of the face object:

Encapsulation: Resolves data security issue inheritance: Solves code reuse problem polymorphism: solved the extension of the program in the previous blog, we learned about the package, and now I look at what is inheritance. In real life, we can interpret the package as a son's inheritance of his father's possessions. Inheritance in object-oriented programming is the process by which an object obtains properties and methods from another object.

Inheritance is the process of creating a new class from an existing class, which is called the base class (The parent Class), the new class that is created is called the derived class (subclass), and the derived class can have non-private properties and methods for all base classes.

For example, we can have a class of "people", and we want to create a new class of "students", students are also belong to the "people", there are some common attribute behavior of "people", we can be "students" this class inherited from the "people", but also "pupils", "middle school students", "college students", they belong to the students, We can let them inherit from the "Student" class.

In succession, the highest level should be the most universal, the most consistent, the next layer, should be more specific than the previous layer, in the inheritance of the neutron class can automatically share the base class member properties and member methods.

How to use Inheritance

In Java, to inherit, you need to use the extends keyword, which inherits the syntax in the following format:

class extends Parent class Name {    member list} as public extends  person{...    }

Example:

In the example above, we first define a person class

 Public class person{    public  String name;      Public int Age ;      Public void sleep () {        System.out.println ("I'm Sleeping");    }      Public void eat () {        System.out.println ("I am Eating");}    }

For "People", people have a name, all have age, can sleep, also can eat.

And I learn if you need a student class at this time, students are also belong to people, students are also the characteristics of people, at this time we can make students inherit from people.

 Public class extends person{    public String sid ;  // School Number     Public String CLS;  // class     Public void Study () {        System.out.println ("I am Learning");}    }

For students, students have their own school number, have their own classes, as well as learning and other functions.

 Public classtest{ Public Static voidMain (string[] args) {Student stu=NewStudent (); Stu.name= "Zhang San"; Stu.age= 20;//we can set name and age for an object of type student, because the S graph inherits from person, so we also have the properties and methods of the person class. Stu.sleep ();  Stu.eat (); //We can also use the method of the person class. Stu.sid= "S20150101001";//Of course, we can also use student's own properties and methodsStu.cls = "1001";        Stu.study (); Person P=NewPerson ();  P.study (); //error, the parent class is not allowed to use the properties and methods of the child class.     }}

Of course, if you still need to have college students, we can define college students, so that students inherit from students, college students can have students and "people" Class properties and methods.

It should be noted that inheritance in Java is a single inheritance , that is, only one parent can be inherited, and if student inherits the person class, it can no longer directly inherit other classes.

How to construct in inheritance

It is important to note that the construction methods in the parent class cannot be inherited.

At the same time, we need to note that in the construction of the object, we will first build the parent class, in the construction of subclasses, this better understanding, just like in real life, only the first father, and then have a son.

For example:

classParentClass {//defining the Parent class   PublicParentClass () {//Construction MethodSystem.out.println ("This is how the parent class is constructed. "); }}classChildClassextendsParentClass {//subclass inherits from parent class   PublicChildClass () {//Construction MethodSystem.out.println ("This is a method of constructing a subclass. "); }} Public classconstructortest {//This class is used to accommodate the Main method   Public Static voidMain (string[] args) {childclass cc=NewChildClass ();//instantiating a subclass object  }}

Execution output:

This is how the parent class is constructed. This is the construction method of the subclass.

So:

    • When instantiating an object of a subclass, you must first execute the constructor of the parent class, and then execute the constructor of the child class.
    • If the parent class has a parent class that has a higher ancestor, the constructor of the highest parent class is called first, and then all the parent class constructor methods of all inheritance relationships are executed in turn.
    • If the constructor execution of the parent class fails, then the object of the subclass cannot be instantiated.
Super keyword

In Java, you can manipulate a parent class with the Super keyword.

There are two main uses of super:

The first uses are:In the constructor of a subclass, the Super keyword can explicitly invoke the constructor of the parent class to pass the argument to it, with the syntax: Super (parameter), and note that the statement must be the first statement in the constructed method.the second uses are:If a subclass has a member with the same name as the parent class, the members in the subclass are used by default in the subclass, but we can explicitly invoke members in the parent class with the Super keyword.  Syntax: Super. The member name needs to be noted: the member in the parent class is not private. Characteristics of inheritance and inheritance in Java

Inheritance is unidirectional, and subclasses can access members of the parent class, but the parent class cannot access the members of the child class.

Only single inheritance is allowed in Java, and a subclass can have only one parent class.

Role

The introduction of inheritance, the implementation of code reuse;

The introduction of inheritance, the implementation of incremental programming.

It can reduce the repetitive redundancy of code and data, and enhance the maintainability of the program by reducing the interface and interface between modules by enhancing the consistency;

Can clearly reflect the hierarchical relationship between classes and classes.

[Java Getting Started note] Object-oriented three major features: 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.