Home >Java >javaTutorial >Java's three major features: encapsulation, inheritance, and polymorphism
We know that the three major characteristics of object-oriented are encapsulation, inheritance and polymorphism. However, sometimes we are always confused about these concepts. These concepts are organized below to lay a solid foundation for future abstraction-oriented programming.
The concept of encapsulation is still easy to understand. If you can define classes, then I believe you have fully grasped the concept of encapsulation. The classes defined below are the encapsulation of data.
The advantage of inheritance is code reuse. Inherited subclasses automatically have all properties and methods in the parent class. So inheriting existing classes means reusing the methods and fields of these classes. On this basis, subclasses can also add some new methods and fields to meet new needs. This is a core technology in Java programming.
So how to judge whether inheritance is needed? The "is-a" relationship is an obvious feature of inheritance. The meaning of this sentence can be interpreted as: The reason why Student inherits Person is because Student is Person. If there was no such relationship, there would be no need for inheritance. This is also the definition of the Liskov Substitution Principle, subtypes must be able to replace their parent types.
When defining a subclass by extending a superclass, you only need to point out the differences between the subclass and the superclass. Therefore, when designing a class, general methods should be placed in the super class, and methods with special purposes should be placed in subclasses. This approach of placing general functions in the super class is Very common in object-oriented programming.
Dynamic binding has a very important feature: the program can be extended without modifying the existing code. So polymorphism is the basis of the open and closed principle.
The following is a specific example of inheritance and polymorphic implementation:
abstract is the abstract keyword. (It's better to inherit from an abstract class than from a concrete class).
public abstract class Person { // 抽象类 private String name; // 私有变量 public String getName() { // Getter方法 return name; } public void setName(String name) { //Setter方法 this.name = name; } public Person(String name) { // 构造函数,用于初始化name super(); this.name = name; } public abstract String getDesc(); // 抽象类中的抽象方法。 只有声明,没有具体实现。 public String toString(){ // toString方法覆盖了Object类的toString方法 return name + getDesc(); } }
extends is the inherited keyword. Student inherits Person, so Student has the name attribute.
public class Student extends Person { // 继承类 private String major; // 新增加的数据 public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public Student(String name,String major) { // 构造函数用于初始化 super(name); // 调用超类构造函数 this.major = major; } @Override public String getDesc() { // 必须实现超类中的抽象方法 // TODO Auto-generated method stub return " : a student, major is " + major; }
public class Employee extends Person{
private double salary;
public double getSalary() {
return salary;
}public void setSalary(double salary) {
this.salary = salary;
}
public Employee(String name, double salary) {
super(name);
this.salary = salary;
}
@Override
public String getDesc() { // TODO Auto-generated method stub
return " :a employee, salary is " + salary;
}
}
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub
Person[] p=new Person[2];
p[0]=new Student("Mark", "IT");
p[1]=new Employee("Jerry", 2000); for (Person person : p) {
System.out.println(person.getName()+person.getDesc());
}
}
}
Running results
It can also be seen from this example that there is no inheritance without encapsulation, and Without inheritance, there would be no so-called polymorphism.
To learn more about java tutorials, please visit:
Java TutorialThe above is the detailed content of Java's three major features: encapsulation, inheritance, and polymorphism. For more information, please follow other related articles on the PHP Chinese website!