Java object-oriented and feature

Source: Internet
Author: User
Tags modifiers naming convention throw exception

Object-oriented:

Concept:

Object-oriented is a kind of programming idea, the design of computer program is essentially to draw out the characteristics of some things in reality to describe the process of computer events, in this abstract process, we encapsulate specific things into a whole to describe, so that the object described has some systematic, The behavior and characteristics of the whole, we call this design idea object-oriented. From the reality of the existence of things, to construct software systems, and in the structure of the system as far as possible the use of human natural thinking, object-oriented is the most simple communication between human and computer language ideas.

Three types of object-oriented features:

Package :1. Hide the properties and implementation details of an object, providing public access only

Principles of Encapsulation:

1. Hide content that does not need to be provided externally

2. Hide the implementation details and provide a way to access them publicly

the way of encapsulation

To separate a function or attribute from a separate method or class.

Set access Permissions

1. Set access permissions to limit the functionality or properties that can be accessed by specific visitors, ensuring the security and functionality of the program.

2.Java defines specific access modifiers for members in classes and classes

Class: Public (Common), default (defaults, does not write default is it)

Members in class: Public, protected, default, private

/*

The access modifier for a class can be either non-writable (the default), or it can write public (common), a Java file can have only one class that is publicly decorated, and the class name that is modified by the public must be consistent with the file name (including the case-consistent), or compile error, in order to facilitate reading, The naming convention for the class name is the first letter of the word, with the class name of multiple words capitalized on the first letter of each word

*/

Inheritance : inheritance is the derivation of new classes from existing classes, and new classes can absorb data from existing classes

Properties and behaviors, and can extend new functionality, or to extract the functionality and attributes of a common thing

Taken out to define a parent class, the subclass that later has a more diversified implementation inherits from the parent class.

the role of inheritance

1. The parent class has the method subclass can inherit directly, does not have to write again, improves the code the reusability

2. To have a relationship between classes and classes, a relationship is a polymorphic implementation

Only single inheritance is supported in 3.Java, multi-inheritance is not supported, because multiple inheritance has security implications (when multiple parent classes have the same function, subclasses are not sure which one to run), Java supports multiple layers of inheritance, that is, the parent class can inherit other classes, and Java uses another mechanism to solve the limitations of single inheritance, that is, multiple implementations

Characteristics of inheritance

1. The inheritance relationship is transitive. If Class C Inherits Class B and Class B inherits Class A (multi-layer inheritance), then Class C has properties and methods inherited from Class B, as well as properties and methods inherited from Class A, and can have its own newly defined properties and methods. Inherited properties and methods, although implicit, are still properties and methods of Class C. Inheritance is the most effective means of constructing, building and expanding new classes on the basis of some more general classes.

2. Inheritance simplifies people's understanding and description of things, and can clearly reflect the hierarchical relationship among related classes.

3. Inheritance provides the software reuse function. If Class B inherits from Class A, then building Class B requires only a few more features (data members and member methods) that are different from the base class (Class A). This method can reduce the redundancy of code and data, and greatly increase the reusability of the program.

4. Inheritance greatly increases the maintainability of the program by increasing consistency to reduce the interface and interface between modules.

5. Provide multiple inheritance mechanisms. Theoretically, a class can be a special class of more than one generic class, and it can inherit properties and methods from multiple generic classes, which is multiple inheritance. For security and reliability reasons, Java supports single-inheritance only, and multiple inheritance by using an interface mechanism.

//build a Class Aclassa{//member variables where num is an argumentintNum=0;//member method, where I is the type parameter PublicAinti) {//input int type file output belowSystem.out.println ("Aaaaaaaaaaaaaaaaaaa"); System.out.println ("I=" +i);//assign the input I to the member variable numnum=i;}}//b inherit aclassBextendsa{intNum=0;//member Method B. PublicB () {//the method that inherits Class A. Because B inherits a, a must inherit the characteristics of a. So the input int value 10 is implemented by the method.Super(10); System.out.println ("BBBBBBBBBBBBBBBBBBBB");//num Here is Class B.System.out.println ("num=" +num);//If you want to display num for Class A, you need to use the following super.num. Note here that NUM needs to be a member variable in Class A .System.out.println ("super.num=" +Super. num);}}//build test Class C Public classc{//Main Public Static voidMain (String aa[]) {//new An object B ()NewB ();}}

Polymorphic
definition : means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object. The technique of implementing polymorphism is called dynamic binding, which is the actual type of the referenced object that is judged during execution, and its corresponding method is called according to its actual type.
Two different forms of expression : overloading and re -

overloading and overriding

overloading : Overloading is a means for a class to handle different types of data in a uniform manner. Multiple functions with the same name exist at the same time, with different parameter types. Method overloading is the ability to create multiple methods in a class that have the same name, but with different parameters and different definitions, which determine which method to use by the number of different arguments and parameter types passed to them when calling the method. The method name is the same, but the parameter type and number can be different.

Rules for overloading:

1, must have a different list of parameters;

2, can have not scold return type, as long as the parameter list is different;

3, can have different access modifiers;

4, can throw different anomalies;

Override: The method in the Kawai class has the same method name, return type, and parameter table as a method in the parent class, and the new method overrides the original method. the access adornment permission for a subclass function cannot be less than the parent class. in the Java language, the Java.lang.Object class is the most fundamental base class (or parent class, superclass) of all classes, and if one of our newly defined classes does not explicitly specify which base class to inherit from, then Java defaults to that it inherits from the object class.

Rules for overriding methods:

1. The parameter list must be exactly the same as the overridden method, otherwise it cannot be called overridden instead of overloaded.

2, the returned type must always be the same as the return type of the overridden method, otherwise it cannot be called overriding but overloaded.

3. The access modifier must be greater than the access modifier (public>protected>default>private) of the overridden method

4. The overriding method must not throw a new check exception or a more general type of check exception than the overridden method declares. For example:

A method of the parent class states that a check exception ioexception, in overriding this method is not able to throw exception, can only throw IOException subclass exception, can throw non-check exception.

the difference between overrides and overloads is that:

Overriding polymorphism works, and calling overloaded methods can greatly reduce the amount of code input, and the same method name can have different functions or return values as long as it passes different parameters inside.

With good rewriting and overloading, you can design a clear and concise class that can be said to be overridden and overloaded in a way that is very unusual in the process of writing code.

Java object-oriented and feature

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.