Three major features in Java

Source: Internet
Author: User

We all know that there are three major features in Java, that is inheritance, encapsulation and polymorphism. So I'm going to talk about these features today.

In the old way, ask yourself why these traits exist. First of all, encapsulation is the use of permission modifiers to implement the hiding of attributes, while providing some common get/set methods to access the data. How does this achieve the effect of encapsulation? We can add some logical control statements inside the get or set method. For example, when I set my age, I limit the number of ages passed to 120. The code implementation can write like this.

 public  void  setage (int  " { if  (age > 120 this . Age = 120; }}  public  int   Getage () { if  ( this . Gender = "female" ) {System.out.println ( " What is the age of a girl you can see?        ");     return  -1;  return  this  .age;}  

This is only a simple example, the actual situation can be optimized. For example, limit age to less than 0. So we can see that the encapsulation of the data has the following advantages, the precise control of the members and hidden implementation details, but also the freedom to modify the internal implementation of the method.

Like a son inheriting from Lao Tzu's wealth, subclasses can also inherit the properties and methods of the parent class, but some of them cannot be inherited, and this goes back to the question of accessing the modifier in the previous view. Only a single inheritance is allowed in Java, that is, there is only one laozi. How is inheritance implemented? Why should there be inheritance? We use the extends keyword to identify the inherited parent class. and inheritance is transitive, a inherits B, B inherits C, then C is the parent of a. The parent class is also called a superclass, and a subclass is also called a base class. Object is a superclass of all classes. When you call a method in a subclass that has the same name as the parent class, you execute a method of the subclass, and you can display a method that uses super to invoke the parent class.

The advantages of inheritance are obvious, can increase the reusability of code, but we cannot inherit to inherit, but also pay attention to the concept of inheritance and composition, inheritance is the concept of "is a", for example, Cat is a animal. And the combination is "has a" concept, for example, our body has a leg. The above two sentences can design the following structure.

 class   Animal {sleep () {System.print.out (    /span> "Animal Sleeps"  //  means the cat is an animal   Class  Cat extends   animal{sleep () {Syste    M.print.out ( "Cat Sleeps"  class   leg{}  //  class   body{Leg Leg  = new   Leg ();}  

And the last one is polymorphic, the same ask yourself, what is polymorphism? What's the use of polymorphism? As the name implies, polymorphism is a variety of states. It's like the same morning. Sometimes you're full of spirit and sometimes you're depressed, and when we call the same method, we're probably doing it differently. This is what Java says about polymorphism. Then why is it different? Remember what you just said about inheriting the parent class, the subclass inherits the parent class method and can override it, and when we call the overridden method, the result is not the same as the result of the parent class. So, you want to be polymorphic, to have inheritance first. There is also a polymorphic form, which we call "virtual method invocation", which is also called upward transformation, and the simple point is that the reference to the parent class refers to the child class object. Like this, the parent class Animal a points to the Cat entity.

New//  cat sleeps

It is important to note that when using a virtual method call, you cannot invoke a method that is unique to the parent class, which can be wrong at run time, compile-time, compile, and compile only to the left of the equals sign. But in the run will look at the specific object, in the subclass can not find the corresponding method will naturally error.

Can summarize the role of polymorphism, we use polymorphism can achieve the purpose of decoupling and extensibility, we write the function of different modules, when called only need to change the parent class reference in the direction of the line. Simply provide a stable subclass implementation, and the parent class is only responsible for invoking the appropriate interface method on the line. This is the principle of the factory design pattern.

Three major features in Java

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.