Java object-oriented core skills

Source: Internet
Author: User
Tags instance method

1. Encapsulation

  Encapsulation is one of the three object-oriented features, that is, the state information of the class is hidden inside the class, does not allow external programs to access directly, and through the method provided by the class to implement the operation and access to the hidden information.

Encapsulation Benefits: Hide the implementation details of the class, so that users can only access the data through the method prescribed by the program, you can easily add access control statements, limit unreasonable operation.

Encapsulated steps: Modify the visibility of properties to restrict access to properties, create a pair of assignment methods and value methods for each property, and include access control statements for the property in the assignment method.

Permission controls used by encapsulation: private (private), public (exposed), protected (subclass and same-package class open)

Extension: Static modifier

Static can be used to modify properties, methods, and blocks of code. Static modified variables are owned by this class and do not belong to an object. So all the objects created by this class share a static variable.

Properties and methods that are modified by static are often referred to as class properties and class methods. An instance property and an instance method are not modified by static.

Static BLOCK: The static block is loaded only once when the class is created, until the program ends. Typically used to initialize a property in a program that assigns a value only once from the beginning of the program to the end of the program.

Syntax: static {//code block}

2. Inheritance

  Inheritance is one of the three main features of object-oriented, and it is one of the important means to implement code reuse in Java. Only single inheritance is supported in Java, that is, each class can have only one direct parent class.

In Java, all Java classes inherit the object class directly or indirectly. The object class is the ancestor of all Java classes. When defining a class, this class inherits directly from the object class if the extends keyword is not used.

In Java, subclasses can inherit from the parent class to the following "property":

1. Inherit public and protected decorated properties and methods, regardless of whether the subclass and parent class are in the same package.

2. Inherit the properties and methods of the default permission modifier (friendly) adornment, but the subclass and parent class must be in the same package.

3. You cannot inherit the parent class's constructor method.

4. The properties and methods of the private adornment cannot be inherited.

In addition, methods inherited from the parent class can be overridden (or overwritten) in a subclass, depending on the requirements. Here are a few things to keep in mind when rewriting:

1. The overriding method and the overridden method must have the same method name.

2. The overriding method and the overridden method must have the same argument list.

3. The return value type of the overridden method must be the same as the return value type of the overridden method or its subclass.

4. The override method cannot narrow the access rights of the overridden method.

Super Keyword:

The "super" keyword is often used in the process of programming with inheritance.

Super represents the default reference to the immediate parent class object of the current object. Members in a parent class can be accessed through the Super keyword in a subclass.

When you use the Super keyword to access a parent class member, you need to be aware of the following points:

The 1.super keyword must appear in subclasses (methods and construction methods of subclasses), not elsewhere.

2. You can access the members of the parent class, such as the properties, methods, and construction methods of the parent class.

3. Be aware of the limitations of permissions in the parent class, and you cannot use super to access members of the private adornment in the parent class.

Cases:

1 super.name;                // access the Name property of the immediate parent class 2 super.test ();              // accessing the test method of the direct parent class 3 Super (name);               // access to the corresponding constructor method of the immediate parent class, which can only appear in the constructor method

A few things to note about the construction methods in inheritance:

1. If the child class does not display a constructor method that calls the parent class, the system takes precedence in calling the parameterless construction method in the parent class.

2. When the program is running, the constructor of the parent class is always called first.

Extension: Overloading:

Overloading is a different implementation of a method in the same class, requiring only that the method name and parameter list be different, regardless of the return value type, access modifier.

3. Abstract classes and abstract methods

Abstract classes and abstract methods in C # are basically the same as in Java, and have previously summarized the abstract classes and abstract methods in C #, so this is a simple summary & review.

The characteristics of abstract classes and abstract methods in Java are as follows:

1. Abstract and abstract methods are modified by the abstract keyword.

2. Abstract classes cannot be instantiated. Abstract classes can have no, one or more abstract methods, or they can all be abstract methods.

3. Abstract methods are only declared and no method is implemented. A class by an abstract method must be an abstract class. Subclasses must override all abstract methods, unless the subclass is still an abstract class.

Extension: Final keyword:

Final can be used to decorate classes, methods, and properties, and cannot be decorated with construction methods.

The final decorated class is called the "final class" and cannot be inherited.

The final decorated method cannot be overridden.

Final-modified properties become constants that cannot be modified elsewhere, except when initialized.

4. polymorphic

  C # in the polymorphism and Java is basically the same, because before did a C # polymorphic summary, so this time only to do a simple summary.

Polymorphism is the same implementation interface that uses different instances to perform different operations.

Rules to be aware of when converting subclasses into parent classes:

1. Point a reference to a parent class to a subclass object, called upward transformation, to automatically type conversions.

2. A method called by a parent class reference variable is a method of a child class that overrides or inherits from a parent class, not a method of the parent class.

3. A method that is unique to a subclass cannot be called through a parent class reference variable.

Rules to be aware of when a parent class is converted to a subclass:

1. Converting a parent class to a subclass is called a downward transition and requires casting.

2. In order to avoid one in the cast, you can pass the instanceof keyword.

instanceof Keywords:

The instanceof keyword is used to determine whether an object belongs to a class or to implement an excuse, and the result is true or false. Is the same as the IS keyword in C #.

5. Interface

Interfaces and abstract classes are roughly the same, and abstract classes focus on the reuse of code, and the interface focuses on the extension and maintenance of code. An interface is a specification and a standard that can constrain the behavior of a class.

Interface-oriented programming can reduce the coupling between the code and improve the extensibility and maintainability of the code.

In the development of the system, the main frame uses the interface, the interface constitutes the skeleton of the system, so you can change the implementation of the interface class to realize the replacement system.

Definition of the interface:

1. The naming conventions and classes of the interfaces are the same.

Example: public interface interface Name {

}

2. Constants can be defined in the interface, and variables cannot be defined. The properties in the interface are automatically modified with public static final. The constants in the interface must specify the initial values when they are defined.

3. The methods involved in the interface are abstract methods. The methods in the interface are automatically decorated with public abstract.

4. The interface cannot be instantiated, and there can be no construction method in the interface.

5. An inheritance relationship is implemented between interfaces through extends. An interface can inherit multiple interfaces, but an interface cannot inherit a class.

6. An implementation class for an interface must implement all the methods in the interface, otherwise it must be defined as an abstract class.

A class can have only one direct parent class, but multiple interfaces can be implemented through implements. However, if you implement the interface while inheriting the parent class, the extends keyword must precede the Implements keyword.

The use of interfaces in C # is roughly the same as in Java, with the colon ":" instead of implements when implemented.

Java object-oriented core skills

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.