Java Foundation 005-Inheritance

Source: Internet
Author: User

Java Foundation 005-Inheritance

Learning Java at the age of 35

1. Overview of inheritance 1.1 (1) when multiple classes share the same attributes and behaviors, extract the content to a separate class, there is no need to define these attributes and actions for multiple classes, as long as the class is followed. 2) Multiple classes can be called subclasses. This class is called a parent class or a superclass. 3) subclass can directly access non-private attributes and behaviors in the parent class. 4) use the extends keyword to generate an inheritance relationship between the class and the class. Class subdemo extends demo {} 5) the emergence of inheritance improves code reusability. 6) The emergence of inheritance creates a relationship between classes and provides the premise of polymorphism. 1.2 inheritance features 1) Java only supports single inheritance, does not support multiple inheritance, and is embodied in another way. A class can have only one parent class, but cannot have multiple parent classes. Class subdemo extends demo {}// okclass subdemo extends demo1, demo2... // error2) JAVA supports multi-level inheritance (Inheritance System) Class A {} Class B extends a {} Class C extends B {} 3) Pay attention to the definition of inheritance: do not inherit the ("is a") Relationship between a class and a class only to obtain a function of another class. xx1 is a type of xx2. 1.3 super keyword 1) the usage of super and this is similar 2) This represents the reference of this Class Object 3) Super represents the identifier of the memory space of the parent class. 4) When a sub-parent class has a member of the same name, you can use super to distinguish it. 5) when the sub-class calls the parent class constructor, you can use the super statement. 1.4 override () 1) if a method of the same class as the parent class appears in the subclass, an overwrite operation is displayed, also known as overwrite or rewrite. 2) Private methods in the parent class cannot be overwritten. 3) In the subclass override method, you can continue to use the overwritten method through the super. function name. 4) override Note: When overwriting, the permission of the subclass method must be greater than or equal to that of the parent method. Static permissions can only be overwritten. 5) covered applications: When the subclass requires the features of the parent class and the feature subject subclass has its own content, you can rewrite the methods in the parent class. In this way, that is, it follows the features of the parent class and defines the unique content of the Child class. 1.5 subclass instantiation process 1) All constructors in the subclass access the constructors of the parent class hollow parameters by default. 2) because the first line of each constructor has a default statement super (); 3) subclass will have data in the parent class, therefore, we must first clarify how the parent class initializes the data. 4) if the parent class does not have a constructor with null parameters, the constructor of the subclass must use the this or super statement to specify the constructor to be accessed. 1.6 final keyword 1) Final can modify classes, methods, and variables. 2) The final modified class cannot be inherited. 3) The final modification method cannot be overwritten. 4) The final variable is a constant. It can be assigned only once. 5) The internal class can only access the local variables modified by final. 1.7 abstract class 1.7.1 abstract class overview 1) abstract definition: abstract is to extract the common and essential content from multiple things. For example, a wolf and a dog are both dogs, and a dog is an abstract concept. 2) abstract class: Java can define a method without a method body. The specific implementation of this method is completed by a subclass. This method is called an abstract method. Classes containing abstract methods are abstract classes. 3) Origin of Abstract METHODS: Multiple objects have the same functions, but the specific functions are different. In the extraction process, only the function definitions are extracted, but the function subjects are not extracted, so there is only a function declaration, and the method without a function subject is called an abstract method. For example, both a wolf and a dog have a way to scream, but the content of the ROAR is different. Therefore, although the abstract inspecies have the ROAR function, the details of the ROAR are not clear. 1.7.2 abstract class features 1) abstract classes and abstract methods must be modified with abstract keywords. 2) an abstract method is defined in an abstract class and has only a method declaration and no method body. Format: modifier abstract Return Value Type Function Name (parameter list); 3) abstract classes cannot be instantiated, that is, they cannot be used to create objects with new. The reason is as follows: abstract classes are extracted from specific things. They are not specific and have no corresponding instances. For example, a dog is an abstract concept, which actually exists as a wolf or a dog. In addition, even if an object is created in an abstract class, calling the abstract method is meaningless. 4) The abstract class is instantiated by its subclass. The subclass must overwrite all abstract methods in the abstract class before creating an object. Otherwise, the subclass is also an abstract class. 1.8 interface 1) Format: interface name {} 2) The member modifier in the interface is fixed. Member constant: public static final member function: Public Abstract: It is found that all members in the interface are public. 3) The emergence of interfaces shows "Multi-inheritance" in another form, that is, "Multi-Implementation ". 1.8.1 interface features 1) interface is an external exposure rule. 2) the interface is the Function Extension of the program. 3) interfaces reduce coupling. 4) interfaces can be used for multiple implementations. 5) there is an implementation relationship between the class and the interface, and the class can inherit a class and implement multiple interfaces at the same time. 6) interfaces and interfaces can have an inheritance relationship. 1.8.2 interface and abstract class

1.9 polymorphism 1) Definition: multiple forms of existence of a certain type of things. For example, cats and dogs in animals. The cat object corresponds to the cat type Cat X = new CAT (). At the same time, the cat is also one of the animals and can also be called an animal. Animal Y = new CAT (); animal is the parent type extracted from the specific things of cats and dogs. The parent type reference points to the subclass object. 2) Program: the reference of the parent class or interface points to or receives its own subclass object. 3) benefits and functions: the existence of polymorphism improves program scalability and maintainability in the future. 4) premise: inheritance or implementation relationships must be covered. 5) characteristic member functions of polymorphism: during Compilation: Check whether the referenced variable belongs to any called member in the class. At runtime: Check whether the class to which the object belongs has called members. Member variable: only the class to which the referenced variable belongs. 1.10 internal class 1) define a class in another class, and that class inside is called internal class (built-in class, nested class ). 2) access features: Internal classes can directly access members in external classes, including private members. To access members of an external class, you must create an object for the internal class. 3) The internal class definition can be modified by the private static member Modifier on the member location. The internal class modified by static can only access static members in the external class. 4) The internal class definition can also directly access members in the external class at a local location. At the same time, you can access local variables in the local directory, but they must be modified by final. 5) The anonymous internal class is the simplified Writing of the internal class. Premise: an internal class can inherit or implement an external class or interface. Format: new external class name or Interface Name () {overwrite the code in the class or interface (you can also customize the content .)} Easy to understand: it is to create an anonymous object of a subclass of an external class or interface with content. 6) when should I use an anonymous internal class? When you use an interface-type parameter and the number of methods in the interface cannot exceed three, you can pass anonymous internal classes as parameters. Enhance readability.

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.