Java Object-oriented programming (II.)

Source: Internet
Author: User
Tags modifiers

It summarizes the three main features of object-oriented in Java. Now talk about the abstract class, interface, inner class and other features in Java.

I. Abstract class
Public abstract class Shape {public int shapeid = 0;public abstract double Getarea ();}
    1. In Java, you agree that a class, interface, or member method has an abstract property, but does not agree that member fields or constructor methods have abstract properties.

    2. Class modifiers contain abstract properties, called abstract classes, and interfaces always have abstract properties.

    3. Assume that the modifier of the member method contains an abstract. Then the member method has an abstract property, called an abstract method. Abstract methods can only be defined in abstract classes or interfaces. Abstract classes generally have at least one abstract method, so generally speaking. Classes that contain abstract methods are called abstract classes.
    4. Abstract methods do not contain method bodies. The format is as follows:
      [Method modifier Word list] Returns the Type method name (table of participation);
    5. An abstract class can have a construction method. However, instances cannot be generated directly, and instances must be generated through non-abstract subclasses.

    6. Assuming that the subclass of an abstract class is not an abstract class, it is required that all abstract methods of the parent class (the abstract class) must be overwritten in the definition of the child class.

Two. Interface Java does not agree that a subclass has multiple direct parent classes, but agrees that a class implements multiple interfaces. The format of the interface definition is:[interface modifier word list] Interface interface name [extends interface list] {interface body}
Public interface Shape {public static final double PI = 3.14159;public abstract double Getarea ();}
    1. external interfaces, like external classes, generally do not have protected and private properties
    2. The interface itself has an abstract property. Therefore, abstract modifiers are not necessary.

    3. There is no construction method inside the interface body, the instance object cannot be generated directly through the interface
    4. All member fields of an interface have public, static, and final properties; All member methods of an interface have public and abstract properties
    5. If the class implementing the interface is not an abstract class. You need to define all the member methods in the class body that overwrite the interface.
Three. Inner class real name inner class
    1. The encapsulation modifiers of the real-name inner class add protected and private.

      Can be selected according to the situation.

    2. For real-name inner classes that do not have a static property, it must have a final property at the same time if its member field has a static property, and a real-name inner class that does not have a static property cannot contain a method with a static property.

    3. How to create a real-name inner class instance object
      * Instance object of static real name inner class:
      New external class name. real name internal class names (list of references)
      *
      instance objects for real-name inner classes that do not have static properties:
      Outer class expression. New real name internal class names (list of references)
Anonymous inner class anonymous inner class does not have a class name, cannot have abstract and static properties, and cannot derive subclasses. The definition format for anonymous inner classes is:new Parent type name (list) {class Body}
A sample is easier to understand:
Abstract class ClassA {int data;public ClassA (int i) {data = i;} public abstract void Method (); public class ClassB {public static void main (string[] args) {ClassA a = new ClassA (3) {public void method () {SYSTEM.OUT.PR INTLN (data);}}; A.method ();}}
Four. Final modifier
    1. A class with a final attribute cannot derive subclasses
    2. Assume that the member field has a final attribute. cannot be overridden by the member fields of the class, and cannot be changed after the assignment. Assuming that the member domain has the final and static properties at the same time, it can only assign values when defined, assuming that the member field has only a final attribute. Does not have a static property. It is only possible to assign values in the definition or in the constructor method.
    3. Assume that a member method of a class has a final property. It cannot be overridden by a member method of the child class of the current class.
Five. Static modifiers in addition to inner classes, classes generally do not have static properties. The reasons and characteristics of static internal classes need to be further studied.
Six. Miscabstract, static, final is three frequently used modifiers.

Only static and final two modifiers can be combined in these three modifiers.

Java Object-oriented programming (II.)

Related Article

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.