Three major Java features

Source: Internet
Author: User

Java has three major features: encapsulation, inheritance, and polymorphism.

Encapsulation:
First, attributes can be used to describe the features of a class of things. methods can describe the operations that a class of things can perform. Encapsulation is to classify the commonalities (including attributes and methods) of the same class into a class for convenient use.
1. concept: encapsulation, also known as information hiding, refers to encapsulating data and data-based operations together using abstract data types to form an inseparable and independent entity, the data is protected inside the abstract data type, so as to hide the internal details as much as possible, and only some external interfaces are kept to associate with the external. The other part of the system communicates and interacts with the abstract data type only by means of Authorized operations wrapped outside the data. That is to say, you do not need to know the implementation details of the object's internal methods, but you can access the object based on the external interfaces (object names and parameters) provided by the object.
2. Benefits: (1) professional division of labor is achieved. After the code that can implement a specific function is encapsulated into an independent entity, Each programmer can call it as needed, thus achieving a professional division of labor. (2) hide information and implement details. By controlling access permissions, you can hide information that you do not want the client programmer to see. For example, if the password of a customer's bank needs to be kept confidential, you can only develop permissions for the customer.

Inheritance:
It is the acceptance of the common attributes and methods of the personality, and the addition of the unique attributes and methods of the personality.
1. Concept: A class inherits from another class, And the inherited class is called a subclass. The inherited class is the parent class.
2. purpose: to reuse code.
3. understanding: the relationship between child classes and parent classes is not the parent-child relationship in daily life. Child classes and parent classes are a special and general relationship, which is the relationship between is-, subclass is a more detailed category of the parent class. For example, class dog extends animal can be understood as dog is a animal. note that when designing inheritance, to make a class inherit, the parent class should be appropriately open to access and follow the Rys replacement principle, that is, to disable extension opening to the modification, that is, the open-closed principle.
4. Result: After inheritance, the subclass automatically owns the attributes and methods of the parent class, but note that the private attributes and constructor of the parent class cannot be inherited.
In addition, sub-classes can write their own unique attributes and methods to achieve function expansion. sub-classes can also rewrite the methods of the parent class, that is, method rewriting.

Polymorphism:
The concept of polymorphism is developed based on encapsulation and inheritance.
Polymorphism is to implement a unified behavior at the abstract level. At the individual (specific) level, this unified behavior will be caused by individual (specific) and implements its own feature behavior. (For an abstract thing, an individual can find its own behavior to execute it .)
1. concept: the same thing calls the same method, and the parameters are the same, but the behavior is different.
2. Understanding: Child classes appear as parent classes, but they are also implemented in their own way when doing things. Sub-classes appear as parent classes and need to be transformed upwards (upcast). The upward transformation is automatically implemented by JVM and is secure, but downcast is insecure, forced conversion is required. When a subclass appears as a parent class, its own attributes and methods cannot be used.

Use Java as a simple calculator;

Import java. Io .*;

Class operationattridute

{

Private double numbera = 0;

Private double numberb = 0;

Private double result = 0;

Public double setnumbera (double I)

{

Return numbera = I;

}

Public double getnumbera ()

{

Return numbera;

}

Public double setnumberb (double J)

{

Return numberb = J;

}

Public double getnumberb ()

{

Return numberb;

}

Public double setresult (Double Z)

{

Return result = z;

}

Public double getresult (double A, double B)

{

Return result;

}

}

// Encapsulate the two numbers and results to be computed.

Class operationa extends operationattridute

{

Public double getresult (double A, double B)

{

Double result = 0;

Result = A + B;

Return result;

}

}

// Addition class: inherits the operationattridute class and overwrites its getresult method.

Class operations extends operationattridute

{

Public double getresult (double A, double B)

{

Double result = 0;

Result = A-B;

Return result;

}

}

// Subtraction class: inherits the operationattridute class and overwrites its getresult method.

Class operationm extends operationattridute

{

Public double getresult (double A, double B)

{

Double result = 0;

Result = A * B;

Return result;

}

}

// Multiplication class: inherits the operationattridute class and overwrites its getresult method.

Class operationd extends operationattridute

{

Public double getresult (double A, double B)

{

Double result = 0;

If (B = 0)

{

Throw new runtimeexception ("the divisor cannot be 0 ");

}

Result = A/B;

Return result;

}

}

// Division class: inherits the operationattridute class and overwrites its getresult method. Division must judge the number of divisor and throw an exception.

Class operationdo

{

Public static object expression_r (char R)

{

Operationattridute OA = new operationattridute ();

Switch (r)

{

Case '+ ':

OA = new operationa ();

Break;

Case '-':

OA = new operations ();

Break;

Case '*':

OA = new operationm ();

Break;

Case '/':

OA = new operationd ();

Break;

}

Return OA;

}

}

// Operation class: Call the operation method by passing in Parameters

Class account

{

Public static void main (string [] ARGs) throws exception

{

Char r = '*';

Operationattridute OA = (operationattridute) operationdo. expression_r (R );

Double A = Oa. setnumbera (9 );

Double B = Oa. setnumberb (2 );

System. Out. println (OA. getresult (a, B ));

}

}

// Main program

In this way, you only need to enter the operator number and return the parent class through polymorphism to implement the result of the calculator.

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.