Java Basic Course Learning notes (9)

Source: Internet
Author: User
Tags modifiers

1:final Keywords (master)(1) is the final meaning, can be modified class, method, variable. (2) Features: A: It modifies the class and cannot be inherited. B: It modifies the method and cannot be rewritten. C: The variable it modifies is a constant. (3) Interview Related:A: Local variable A: the base type value cannot be changed B: the reference type address value cannot be changed, but the contents of the object can be changed B: Initialization time A: can only be initialized once. A final modified variable can only be assigned one time. before the construction method is complete。 (non-static constant) B: common values definition of the time. (recommended)Constructs the method. 2: Polymorphism (mastering)(1) the different states that the same object manifests at different times. (2) The precondition of polymorphism: A: There is inheritance or realization of the relationship.  B: There are methods to rewrite.   C: There is a parent or parent interface reference to the child class object.   Polymorphic classification: A: Specific classes of polymorphic class Fu {} class Zi extends Fu {} fu f = new Zi ();   B: Abstract class polymorphic abstract classes Fu {} class Zi extends Fu {} fu f = new Zi (); C: Interface polymorphism interface Fu {} class Zi implements Fu {} fu f = new Zi (); (3) feature of member access in polymorphicA: Member variables compile look left, run look leftB: Construction method the construction of the subclass will have access to the parent class construction by defaultC: Member Method compile look left, run look rightD: Static method compile look left, run look leftWhy? because the member method has overrides. (so run to see right)(4) Benefits of polymorphism:A: Improve code maintainability (inheritance) B: Improve code Extensibility (polymorphic embodiment) (5) The disadvantage of polymorphism: the parent cannot use the child's unique functions. Phenomenon: A child can be used as a parent, and the parent cannot be used as a child. (6) the transformation in polymorphismA: Transition up from child to parent B: down transition from parent to child (7) The case of Confucius pretending to be a father helps us understand polymorphism memory plots in polymorphic inheritance object changes in polymorphic memory plots(8) polymorphic exercises A: Cat and dog case B: teacher and student case 3: Abstract class (Master) Abstract class overviewLooking back at our cat and dog case earlier, we extracted an animal class.       And we've created animal objects in front of us, but that's not true. Why? Because, I say animals, do you know what kind of animal I'm talking about? Only when you see the specific animal, do you know what animal it is. Therefore, the animal itself is not a specific thing, but an abstract thing. Only the real cat, the dog is the specific animal. In the same way, we can infer that different animals should not eat the same things, so we should not give concrete expression in animal classes, but should give a statement. In Java, a method without a method body should be defined as an abstract method, and if there is an abstract method in the class, the class must be defined as an abstract class.   (1) to extract a number of common things into a class, this is the practice of inheritance. But there are many things that are common, in some cases, method declarations, but method bodies.    That is, the method declaration is the same, but each specific object is not the same when the concrete implementation.    Therefore, when we define these common methods, we cannot give the concrete method body.    A method that does not have a concrete method body is an abstract method. In a class, if there is an abstract method, the class must be defined as an abstract class. (2) Characteristics of abstract classesA: Abstract classes and abstract methods must be modified with the keyword abstract B: Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be abstract class C: Abstract classes cannot instantiate D: Subclasses of abstract classes A: is an abstract class. B: is a specific class. This class must override all abstract methods in the abstract class. (3) Characteristics of abstract class Members:A: The member variable has a variable, there is a constant (can be a variable, or it can be a constant) B: The constructor method has a constructor (for subclass access to the initialization of the parent data) C: The Member method has an abstract, non-abstract (can be either abstract or abstract) member method attributes for abstract classes:A: The abstract method enforces what subclasses do. B: The non-abstract method subclass inherits things, improves the code reusability. (4) Abstract class exercise A: Cat and Dog case Exercise B: Teacher case Exercise C: Student case Exercise D: Employee case Exercise (5) some small problems of abstract classA: Abstract class has a construction method, can not be instantiated, then what is the use of the construction method?   Initialization for subclass access to parent data B: What is the use of a class if there is no abstract method but is defined for the abstract class? In order not to create the object (in order not to instantiate the class) C:abstract cannot coexist with which keywords a:final conflict b:private conflict c:static meaningless 4: Interface (Master)(1) Looking back at cat and dog cases, they only provide some basic functions. For example: Cat drill fire ring, dog high jump and other functions, not the animal itself, is trained in the back of the training, this extra function, Java provides an interface representation. (2) features of the interface:A: interface with keyword interface decorated interface interface name {} B: Class implementation interface with implements class name implements interface name {} C: interface cannot instantiate D: interface Implementation Class A: is an abstract class. (but not very significant, because abstract classes cannot be instantiated) B: is a concrete class that must override all abstract methods in the interface. (3) member features of the interface:A: Member variables can only be constant and static (even if you define a non-static variable)         As: defined in the interface: public int num = 10; In fact, his default modifier is: public static final num = 10; default modifier: public static final Recommendation: Manually give default modifiers yourselfB: Construction method No construction methodC: Member Method It can only be abstract. default modifier: Public abstract (even if you do not write public at the time of definition) Recommendation: Manually give default modifiers yourself (4) Classes and classes, classes and interfaces, interfaces and interfacesA: Class and class inheritance relationship, can only single inheritance, multi-layer inheritance B: class and interface implementation relationship, can be single implementation, can also be implemented.  Can also inherit a class at the same time, implement multiple interfaces C: interface and interface inheritance, can be single-inheritance, can also inherit (5) abstract class and interface differences (self-completion)?   A: Member Differences Abstract classes: interfaces: B: Relationship differences: Classes and classes: Classes and interfaces: Interfaces and interfaces: C: Design concepts are different abstract classes: is a, which is defined in the abstract class is a common function. Interface: Like a, an interface that defines an extension function.

Java Basic Course Learning notes (9)

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.