Java basic notes-inheritance, abstract classes, interfaces, polymorphism, internal classes, exceptions

Source: Internet
Author: User

Java basic notes-inheritance, abstract classes, interfaces, polymorphism, internal classes, exceptions
Inheritance: when multiple classes share the same attributes and behaviors, the content is extracted to a separate class. Therefore, multiple classes do not need to define these attributes and behaviors, directly inherit that class. 1) Multiple classes are called subclasses, and individual classes are called parent classes or superclasses. 2) subclasses can inherit non-private attributes and methods of the parent class. 3) use the extends keyword to establish an inheritance relationship between classes. 4) the emergence of inheritance improves code reusability and creates a relationship between classes, it provides a premise for the emergence of polymorphism. 5) it is not possible to inherit the code to implement a certain type of function. It must be a class or a class that has a parent relationship: is a 6) features: java only supports single inheritance, does not support multi-inheritance Java supports multi-layer inheritance (Inheritance System) 7) Super: similar to this usage, this indicates the reference of the class object, and super indicates the reference of the parent class. 8) override: If the subclass contains the same method as the parent class, the override operation will appear, this is also known as override or rewrite. When the override is private, the method cannot be overwritten. The static method can only be overwritten. The method of the latter parent class can be accessed by calling the subclass of super, the permission must be greater than the access permission of the parent class 9) final: final can modify classes, methods, and variables. The final-modified classes cannot be inherited. The final-modified methods cannot be overwritten. The final-modified variables are constants, an internal class can only be assigned once and can only access the final-modified partial variable abstract class: defines methods without method bodies in java, and the specific implementation of this method includes subclass implementation, this method becomes an abstract method. This class is called an abstract class. 1) abstract method: only a function declaration is required, and a method without a function subject is called an abstract method. 2) abstract classes and abstract methods must be modified with abstract keywords. 3) abstract classes can define non-abstract methods for sub-classes to call. 4) abstract classes cannot be instantiated, that is, they cannot create objects through new. 5) abstract classes can be instantiated by subclasses. subclasses must overwrite all abstract methods to create objects. Otherwise, subclasses are also abstract class interfaces. (1) The member modifiers in interfaces are fixed (all members are public) member variable: public static final member function: public abstract 2) features: the interface is the rule interface exposed to the outside. It is the Function Extension interface of the program to reduce the Coupling Degree of the program. The interface can be used to implement the relationship between multiple implementation classes and interfaces, in addition, classes can implement multiple interface interfaces and interfaces, and there can be inheritance relationship polymorphism between them: there are multiple forms of a transaction, and the reference of the parent type points to the subclass object 1). embodiment: references in the parent class or interface point to or accept subclass objects. 2) Role: Improves program scalability and maintainability. 3) premise: inheritance or overwrite operations are required. 4) feature: when compiling a member function, check the left-side function. when running the function, check the right-side member variables. When compiling and running the function, check the left-side static member functions (the class to which the referenced variable belongs, for both compilation and running, refer to the internal class on the left: Define a class in another class, and the class inside is called the internal class (built-in class, nested class) 1) access features: internal classes can directly access members in external classes, including private members, external classes, and internal class objects must be created. 2) internal class location: Member location: the internal class that can be modified by the private static modifier can only access static member local variables in the external class: You can directly access members in the external class or access local variables in the local class, but it must be final. 3) Anonymous internal class: the premise is to create an external class or a subclass of the external class or interface of the content: internal classes can inherit from or implement an external class or interface Exception: the Exception system (Throwable): Error: Usually a major problem occurs, and no code is written to handle Exception: in some running situations, you can use the following common methods: getMessage () to obtain the exception information, return the string toString () to obtain the exception class name and exception information, and return the string printStackTrace () returns void printStackTrace (PrintStreams) to get the exception class name and exception information, and locate the exception in the program. Saves the exception information in the log file throws and throw: throws: used to identify the exceptions exposed by the function. The exception class name throw is used to throw exceptions in the function, followed by exception object Exception Handling: 1) try {code to be processed} 2) catch (exception class variable) {Exception Handling Code} 3) finally {code to be executed} is usually used to close the resource if the previous execution class System. exit (0) will not be executed for custom exceptions: 1) custom exceptions inherit Exception or its subclass 2) define Exception information through the constructor 3) throw the exception through throw: RuntimeException and its subclass are thrown in the function. When a function does not have to declare that a method is overwritten, the method that overwrites it must throw the same exception or its subclass. If the parent class throws multiple exceptions, the exception thrown by the overwrite method must be a subset of those exceptions and cannot throw new exceptions.

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.