Three major features of Java

Source: Internet
Author: User
Tags multiple inheritance in java

Ava three main features:
Packaging:
Abstraction: The first step in dealing with a variety of problems, combining specific problems with solutions to problems, is the process of abstraction.
Java Common access control modifiers:
Public: Accessible everywhere, not restricted by package and parent-child relationships
Default: can only be accessed within the same package, not restricted by parent-child class relationships
Protected: A subclass of different packages and access in the same package class, which is an inherited access.
Private: Only the inside of your own class can access, other parts of the package, regardless of inside and outside, parent-child class can not access
Final: Modifier Property: Constant, value cannot be modified
Either assign a value at the time of the Declaration, or assign a value in the constructor method, and two opportunities can be selected only once.
Modification method: The final method subclass cannot override it

Modifier class: The class cannot have subclasses
Static: Decorated property: A static property is common to all instances of a class and can be accessed in the form of a class name. property, and for numeric variables, the system will give the initial value, not forcing the initial value to be assigned.
Method: can be accessed by using the "Class name. Property" method, which is also common to all instances of the class
Code block: Load has the highest priority, even earlier than the load of the main method.
For variables that are modified by final static, there are two opportunities for assignment, one at the time of Declaration, and the other in static code blocks, where two opportunities are available and can only be selected one time.

Inherited:
The relationship between parent and child classes is allowed, and a subclass has only one direct parent class (inheritance in Java is a single inheritance between classes).
When the memory forms the information binding of the class, there are not multiple intersections, and the structure is much clearer. Java itself is less efficient and can only reach one-fifth of the C language. The perfect requirement for the Java language to abandon efficiency, in exchange for learning and use of the simple.
Masking of attributes: Father f = new Son (); The property accesses the parent class, and the method accesses the child class.
Overloading of methods: there are multiple representations of a method inside a class, such as a different parameter table, overloading of the constructor method
Method overrides: Occurs between parent and child classes, the subclass method has a new implementation method for the parent class method, but the two method definitions are partially identical.

The purpose of inheritance: to implement code reuse. Straighten out the structural relationships between types of objects.
When building a parent class, it is required that his subclass and parent class must have a theoretical and definite inheritance relationship.
How to Reuse code:
Inheritance reuse, completely the non-private part of the parent class as the content of the subclass, if necessary, subclasses just rewrite the parent class with the same name method.
Composite multiplexing: the implementation of a method of selecting an object in a type is implemented as a method of the class, and these two classes are also known as the theoretical parent and the theoretical subclass. The extends keyword does not appear in the program to describe the two class relationships.

The process of abstracting a parent class through a concrete instance is called generalization
The process of confirming a subclass through a parent class is called a special type.
In the design phase of the relationship between the classes of code, there is a theoretical requirement that there is no identical method between the parent and child classes. However, the syntax allows subclasses to override the parent class's methods. Requires that only its own methods and properties are unique to the subclass, and that the other parts are inherited from the parent class with the same parts as the parent and other sibling
About Abstract Classes:
Abstract classes are used to describe high-level object classes in nature, and one cannot instantiate an abstract class, such as an animal. An object of an abstract class can only be instantiated with a subclass of an animal class.
Abstract modifier abstractions can be used to modify classes, and to modify methods.
A non-abstract method can be defined in an abstract class, but if there is an abstract method in a class, the class must be defined as an abstract class. There are construction methods in the abstract class, but there is no abstract construction method. The existence of a construction method exists solely for the purpose of inheriting the relationship.
In contrast to this is the interface, all the methods in the interface requirements are defined as public abstract methods, because these methods represent standards, the standard information must be open, transparent, such as the rules in sports competitions.
The contrast between abstract classes and interfaces: interfaces and abstract classes are essentially two concepts, but interfaces can be thought of as a special class with a higher level of abstraction than an abstract class. Because the information of the interface is only known immutable value and abstract method, so the information is bound in a single way, from the implementation can satisfy the inheritance but also does not appear the mesh information binding effect, does not increase the number of nodes at the time of access. Therefore, the interface allows multiple inheritance.
For the content of the class, do not allow multiple inheritance, a subclass can only have a direct parent class, the complexity of information binding and Java itself is limited by the operating principle, so that it can not be as efficient as the C language to run, if the binding information is more complex, the data access process more complex, affecting the operational efficiency.
Abstract classes are classes, interfaces are interfaces, and they are at a more abstract level, making them similar in nature. (Interfaces can be viewed as a special abstract class)
Polymorphism: An interface of various implementations, can be understood as a syntactic permission, the performance of a variety of forms. The following code expressions can be thought of as a concrete manifestation of Java polymorphism:
IA ia = new Testia (); Where IA is the defined interface, Testia is the implementation class for the interface
Father f = new son () where Father is the parent class and son is a subclass
Animal a = new dog () where Animal is an abstract class, and dog is a subclass of the Animal class.
Method overrides in parent-child class
Method overloading in a class

The polymorphism in Java is basically implemented by the interface, and the polymorphism between the abstract class and the subclass is only a small part, the interface is the highest level of abstraction in Java, its internal definition is purely abstract, the definition of the property is to expose static constants, pure abstract methods and constant attributes constitute a standard, all of its implementation classes, Including abstract classes are subject to the rules defined in the interface.
The best way to communicate in a Java class is to pass a value or object in the form of an interface, such as the relationship between a lamp and a lamp. Whatever type of object is used as an argument, as long as it is an implementation class of an interface object, it can be a reasonable parameter.
The contents of the two modules are communicated in an interface way, so that the two modules can be developed in a relatively independent manner and do not depend on the implemented information of any one module. form the loosely coupled state required in the code design.
For a polymorphic unnecessary explicit definition, only need to understand its expression and meaning, the code fully realize the interface mode of communication is basically zero, but the requirements to achieve as far as possible.
Interfaces exist on the surface to implement a polymorphic syntax, which actually implements multiple inheritance in Java, a class can inherit one class at a time, and implement another or more interfaces to obtain multiple types of information at the same time (including properties and methods).
Other Edge knowledge points:
Garbage collection mechanism for Java:
Virtual machines provide programmers with a unified programming interface (API) that programmers only need to program as defined in the API. The biggest difference between C and C + + is that there is no need to consider memory recycling in Java, and virtual opportunities to recycle garbage in a time-delayed way to maximize the use of memory space.
There are several methods of garbage collection in Java, but each of these methods is simply a reminder of what is being recycled, not the virtual machine. such as the System.GC () method, is a common way of system-provided garbage collection, but when the value of the object is null, The GC () method does not immediately release the memory space, only a null value reminder to the virtual machine that the object's contents can be recycled.
Programmers have no control over the garbage collection point of Java virtual machines and can only fulfill the responsibility of prompting, but in most cases, the automatic operation of the garbage collection area of the virtual machine can meet the needs of the system running.
In the process of running the program, you can report information related to the virtual machine stack overflow, such errors are related to the data structure during the operation of the virtual machine, the human cannot guarantee that the error does not occur, in most cases, after throwing a virtual machine exception, simply restart the virtual machine or recompile the corresponding Java code can be , there are only a few cases where you need to restart your PC and other related components. ----------------------------






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

Packaging:
First, attributes can be used to describe the characteristics of the same class of things, and methods describe the operations that a class of things may do. Encapsulation is the inclusion of commonalities (including attributes and methods) belonging to the same category in a class for ease of use.
1. Concept: encapsulation, also known as information hiding, refers to the use of abstract data types to encapsulate data and data-based operations together to form an indivisible independent entity, the data is protected within the abstract data type, as much as possible to hide internal details, leaving only a few external interfaces to make the external connection. The rest of the system is communicated and interacted with this abstract data type only through authorized operations that are wrapped outside of the data. That is, the user does not need to know the implementation details of an object's internal methods, but can access the object based on the external interface (object name and parameters) provided by the object.
2. Benefits: (1) achieve a professional division of labor. Once the code that implements a particular function is encapsulated into a separate entity, the programmer can invoke it when needed, thus achieving a professional division of labor. (2) Hide the information and implement the details. By controlling access permissions, you can hide information that you do not want the client programmer to see, such as a customer's bank password that needs to be kept secret and can only develop permissions for that customer.

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. Understanding: The relationship between the subclass and the parent class is not a parent-child relationship in daily life, but a special and generalized relationship between the subclass and the parent class, which is a is-a relationship, and the subclass is a more detailed classification of the parent class. As Class dog extends animal, it can be understood as dog is a animal. Note that when designing inheritance, to allow a class to inherit, the parent class needs to be properly open to access, following the Richter substitution principle, which is to open the extension to the modified closure, that is, the open-close principle.
4. 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.

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. (For an abstract thing, the inner individual can find its own behavior to execute.) )
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. Subclasses appear as parent classes need to be transformed (upcast), where the upward transformation is automatically implemented by the JVM and is secure, but the downward transition (downcast) is unsafe and requires casting. When a subclass appears as a parent, its own properties and methods cannot be used.

Use Java to make 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;

}

}

Encapsulates the 2 numbers and the results of the operation that will be performed.

Class Operationa extends Operationattridute

{

Public double GetResult (double a,double b)

{

Double result=0;

Result=a+b;

return result;

}

}

Addition class: Inherit the Operationattridute class and overwrite 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 overrides its GetResult method

Class Operationm extends Operationattridute

{

Public double GetResult (double a,double b)

{

Double result=0;

Result=a*b;

return result;

}

}

Multiplication classes: Inheriting Operationattridute classes and overriding their GetResult methods

Class Operationd extends Operationattridute

{

Public double GetResult (double a,double b)

{

Double result=0;

if (b==0)

{

Throw new RuntimeException ("Dividend cannot be 0");

}

result=a/b;

return result;

}

}

Division class: Inherit the Operationattridute class and overwrite its GetResult method, divide to judge the 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 Arithmetic method by passing in the 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, the results of the calculator are implemented by means of the input operation symbol and the way the parent class is returned by polymorphism.


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.