Home > Java > javaTutorial > A brief analysis of inheritance and composition in Java

A brief analysis of inheritance and composition in Java

高洛峰
Release: 2017-01-21 16:37:31
Original
1269 people have browsed it

Preface

Java is an object-oriented language. Everyone who has studied Java knows that encapsulation, inheritance, and polymorphism are the three characteristics of object-oriented. Everyone will have more or less the impression when they first learn inheritance: inheritance can help me achieve class reuse. Therefore, many developers will naturally use class inheritance when they need to reuse some code, because this is how it is written in the book (this is how the teacher teaches). However, this is actually wrong. Extensive long-term use of inheritance will bring high maintenance costs to the code.

Actually, I had never heard of the term combination when I first learned Java, and the teacher never explained it. I always thought that I had missed some knowledge points on my own. In fact, it was not the case. Combination is Let’s define the noun as a thinking thing for the time being. I believe readers have come across it, but they don’t know that it has this name.

In fact, the so-called combination is to create a new class to call the class that has been created and debugged, then this new class can be called a combination

For example I create a People

public class People {
 private String name;
 private int age;
  
 public void setName(String name){
 this.name = name;
 }
 public String getName(){
 return this.name;
 }
 public int getAge(){
 return this.age;
 }
 public void setAge(int age){
 this.age = age;
 }
}
Copy after login


Then I want to use this class now. I can add some new features to this class. At this time we can Create a new class and create People objects in this class.

For example, I now create a class called Student

class Student {
 People people = new People();
}
Copy after login

Then I can add some attributes to this class, such as defining an identity as student. Here I No more discussion.

Let’s take a look at inheritance. In fact, inheritance and combination have the same purpose. Let’s first look at inheritance.

There is a keyword called extends in java, which can help us inherit. The inherited class is called the parent class, or it can also be called the base class or super class, and the inheritor We call it a subclass or a derived class, etc.

Here we define a class

public class Student extends People{
 //doSomething
}
Copy after login

In this way, the class inherits the properties of the parent class All member methods and member variables, but note that fields or methods declared as private permissions will not be inherited.

In order to prove this, we write a method in the student class

public String re(){
 return this.name;
}
Copy after login

The compiler will report an error "People.name is not visible", from It can be seen here that fields or methods declared as private cannot be inherited. If you want to inherit it, you can change private to protected. In this case, we can successfully inherit the name field.

In this way we initialize all the fields in People, just add this code block to the code

{
 this.age = 10;
 this.name = "zhangsan";
}
Copy after login

Next, declare it in the main function This student

Student student = new Student();
System.out.println(student.getAge());
Copy after login

Now we are surprised to find that this writing method is feasible. Although we have not declared any fields and methods in the subclass, we can still call getAge(); and it can It prints out 10

smoothly. This is because we have not overloaded any method in the subclass, so the getAge of the parent class is called at this time, so we can easily access the private class of the parent class. declared fields.

After reading this, I believe everyone has a preliminary understanding of inheritance. So in the inheritance mechanism, how are the constructors of each class called? The answer is to call them in sequence from the parent class to the child class. .

While demonstrating, I first declare three classes Temp1, Temp2, and Temp3. At the same time, Temp3 inherits from Temp2, and Temp2 inherits from Temp1. In this case, we will use the constructor method of each class Put a print statement

//Temp1
System.out.println("i'm temp1");
//Temp2
System.out.println("i'm temp2");
//Temp3
System.out.println("i'm temp3");
Copy after login

For convenience, I just wrote it here, but everyone must pay attention to these being placed in the construction method of each class.

We create a Temp3 object in the main function

public class Mian{
 public static void main(String[] args){
 Temp3 temp3 = new Temp3();
 }
}
Copy after login

We look at the console and print out

i'm temp1
i'm temp2
i'm temp3
Copy after login

It can be seen that the object of temp3 must be created first, the extends keyword is found, and then goes up the inheritance chain to find temp2, and the extends keyword is found, and then finds temp1, and then calls the constructor of temp1, and then Call them one by one.

Summary

The above is the entire content of this article. I hope the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

For more articles related to a brief analysis of inheritance and combination in Java, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template