[Java entry notes] three features of object-oriented: inheritance and java

Source: Internet
Author: User
Tags what inheritance

[Java entry notes] three features of object-oriented: inheritance and java
Understanding what inheritance is

First, we know that there are three main characteristics of objects:

Encapsulation: solves data security issues inheritance: solves code reuse issues polymorphism: solves program expansion problems in the previous blog, we learned about encapsulation, now, let's look at what inheritance is. In real life, we can understand encapsulation as the son's inheritance of his father's property. Inheritance in object-oriented programming is a process in which an object obtains attributes and methods from another object.

Inheritance is the process of creating new classes using existing classes,The existing class is called the base class (parent class), and the new class is called the derived class (subclass). The derived class can have non-private attributes and methods of all the base classes.

For example, we can have a class "person", and we want to create a new class "student". Students are also "people, with some common property behaviors of the "person" class, we can inherit the "student" class from the "person" class ", there are also "Primary School Students", "Middle School Students", and "College Students". They all belong to students and we can make them inherit from the "student" class.

In inheritance, the highest level should be the most universal and most suitable for the general situation. The next layer should be more specific than the previous layer, in inheritance, subclasses can automatically share Member attributes and member methods in the base class.

How to Use inheritance

In Java, to inherit, you must useExtendsKeyword. The inherited syntax format is as follows:

[Access modifier] class subclass name extends parent class name {member list} such as public Student extends Person {......}

 

Example:

Based on 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'm eating ");}}

 

For "people", people all have a name, all have ages, can go to bed, can also eat.

If I need to have a student class at this time, the students also belong to people, and the students also have the characteristics of others, then we can make the students inherit from people.

Public class Student extends Person {public String sid; // Student ID public String cls; // class public void study () {System. out. println ("I'm learning ");}}

 

For students, they have their own student IDs, their own classes, and learning functions.

Public class Test {public static void main (String [] args) {Student stu = new Student (); stu. name = "James"; stu. age = 20; // we can set name and age for Student objects. Because S graphs inherit from Person, they also have attributes and methods of the Person class. Stu. sleep (); stu. eat (); // we can also use the Person class method. Stu. sid = "S20150101001"; // Of course, we can also use the attributes and Methods stu of Student. cls = "1001"; stu. study (); Person p = new Person (); p. study (); // error. The attributes and methods of the parent class cannot be used. }}

 

 

Of course, if we still need college students, we can define college students so that college students can inherit from their own students so that they can have attributes and methods of the student and "person" classes.

It should be noted that the inheritance in Java isSingle inheritanceThat is to say, only one parent class can be inherited. If Student inherits the Person class, it cannot directly inherit other classes.

Constructor in inheritance

Note that the constructor in the parent class cannot be inherited.

At the same time, we need to note that during the object construction process, we will first build a parent class, and then build a subclass. This is a better understanding, just like in real life, only when I have a father before I have a son.

For example:

Class ParentClass {// define the parent class public ParentClass () {// constructor System. out. println ("this is the constructor of the parent class. ") ;}} Class ChildClass extends ParentClass {// subclass inherits from the parent class public ChildClass () {// constructor System. out. println (" this is the constructor of the subclass. ") ;}} Public class ConstructorTest {// This class is used to hold the main method public static void main (String [] args) {ChildClass cc = new ChildClass (); // instantiate a subclass object }}

Execution output:

This is the constructor of the parent class. This is the subclass construction method.

Therefore:

  • When instantiating a subclass object, you must first execute the constructor of the parent class, and then execute the constructor of the subclass.
  • If the parent class has a parent class, the constructor of the top parent class is called first, and then all the parent class constructor of all inheritance relationships are executed one by one.
  • If the constructor of the parent class fails to be executed, the subclass object cannot be instantiated.
Super keyword

In Java, the super keyword can be used to operate the parent class.

Super has two main purposes:

The first use is:In the subclass constructor, the super keyword can explicitly call the constructor of the parent class to pass the parameter to it. Its syntax is: super (parameter ); note that this statement must be the first statement in the constructor. The second purpose is:If the child class has members with the same name as the parent class, the Child class uses the members of the Child class by default. However, the super keyword can be used to explicitly call the members of the parent class. Syntax: super. Note that the member name in the parent class is not private. Inheritance and inheritance functions in Java

Inheritance is unidirectional. Child classes can access members in the parent class, but the parent class cannot access Child class members.

In Java, only single inheritance is allowed. A subclass can have only one parent class.

 

Function

Introduce inheritance to realize code reuse;

Introduce inheritance to implement incremental program design.

It can reduce code and data duplication redundancy and reduce inter-module interfaces and interfaces by enhancing consistency, thus enhancing program maintainability;

It can clearly reflect the hierarchical relationship between classes.

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.