Three major features of Java

Source: Internet
Author: User

The three main features of the Java language are: encapsulation, inheritance, polymorphism

Package:  

Attributes can be used to describe the characteristics of the same class of things, and the behavior describes the operations that a class of things can do, encapsulation is to classify the common things (including attributes and behaviors) into a class, so as to be convenient to use. For example, people this stuff, can be encapsulated in the following way:

People

     Age ( Property one)   
height (attribute two) /strong>
gender (attribute three)

doing (one of the behaviors)
Walk (Act II)
Talking (Act III)
For example

public class  husband {  

/*   

* Encapsulation of attributes

* A person's name, gender, age, wife are the private attributes of this person     

*/

private String name;

private String sex;

private int age;

private Wife Wife;

 /*  

* Setter (), getter () is the interface that the object develops externally  

*/

public String getName () {

return name;

}

public void SetName (String name) {

this.name = name;

}

Public String Getsex () {

return sex;

}

public void Setsex (String sex) {

this.sex = sex;

}

public int getage () {

return age;

}

public void Setage (int.) {

this.age = age;

}

public void Setwife (Wife Wife) {

this.wife = wife;

}

}

Inherited:

It is the acceptance of individuality to the properties and methods of generality, and the attribute and method of individuality characteristic.

 1. Concept: A class inherits another class, that is, the inherited class is a subclass, and the inherited class is the parent class.
2. Purpose: To implement code reuse.
3. Result: After inheritance, subclasses automatically have the properties and methods of the parent class, but it is particularly important to note that the parent class's private properties and construction methods cannot be inherited.
In addition, subclasses can write their own unique properties and methods, the purpose is to implement the extension of the function, subclasses can also replicate the parent class method is the method of rewriting.
Inheritance: After the subclass inherits the parent class, the methods and properties of the parent class are passed to the subclass, and the subclass can
use them, and subclasses can have their own properties and methods,method Overrides: The same method appears in the subclass as the parent class declaration .declarations are the same: return type, method name, parameters are all the sameaccess modifier: Overridden method in a subclass the access modifier is greater than or equal to the overridden method 

public class Chinacat extends Cat{/*chinacat is a subclass, Cat is the parent class, the extends keyword, and eat is the method that calls the parent class */

@Override
public void Eat () {
System.out.println ("mouse");
}

}

Example: Sub-category page

Parent Class Page

Polymorphic:

The concept of polymorphism is developed on the basis of encapsulation and inheritance.
Polymorphism is the implementation of a unified behavior on an abstract level, at the level of the individual (concrete), this unified behavior will be due to the individual (specific) characteristics of the characteristics of the implementation of their own behavior.
1. Concept: The same thing, call its same method, the parameters are also the same, but behave differently.
2. Understanding: Subclasses appear as parent, but do things in their own way.

Three major features of 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.