Java Basics Summary-object-oriented 2

Source: Internet
Author: User
Tags class definition define abstract event listener

1. There are several methods that are related to encapsulation in a class, the method does not call to the unique data, need to be static
2. If all methods of a class are static methods, in order to ensure that they are not created by other objects, you can privatize the construction method of the class.
3. Document comments javadoc-In accordance with the prescribed notes--extract comments saved as HTML (not important according to the provisions of the line)
Design pattern: An effective solution to the problem--is a kind of thought
Java has 23 design ideas--oop Language design pattern Universal
------Inheritance------
1. Overview of Inheritance
Two/More classes constantly on it to extract their common things-the common things encapsulated in the parent class, creating a parent-child relationship--is a relationship with the keyword extends implementation
2. Benefits of Inheritance
Code reuse, making relationships between classes, providing prerequisites for later polymorphism
Characteristics of inheritance in 3.Java:
* * Only single inheritance is supported between classes-for "multiple inheritance" is accomplished through multiple implementations (interface)
Do not directly support the cause of multiple inheritance: if more than one parent class exists the same method, the subclass object calls the method when there is an indeterminate call, so it is not supported, and the interface is just the declaration of the method, the implementation to see the needs of subclasses, so there is no call of uncertainty.
* * There are multiple layers of inheritance-the existence of inheritance system
If you want to use an inheritance system--first to analyze its top class/interface (which is generally the most basic function of the system)--to create an object is generally to choose the object to create the subclass (the reason is both inherited and own unique content)
4. Small details about inheritance
* * Do not blindly use inheritance--only a strong is a relationship exists between classes to consider inheritance
* * Subclasses hold the Super keyword when inheriting-representing the parent class space (in fact, the parent class can be found)
Distinguish a member of a parent-child class with the same name: super; distinguish local variables from this class object: this
For private member variables of the parent class, the subclass inherits but cannot be accessed.
**this and Super functions are similar, but Super does not represent a parent object
* * About parent-child class loading in memory--loading the parent class before loading the subclass
* * Call to Members: first in the subclass, then not found in the parent class.
* * Overrides (Overrides) for parent-child class member methods
Overwrite occurs because the subclass wants to modify the inherited functionality
Overriding attention: The overriding method of the subclass is greater than or equal to the parent class method; static override static/statically overwritten
-----The process of inheriting the instantiation of a neutron class object----
1.JVM reads the class file for that class under the developed path and loads it into memory, and loads its immediate parent class
2. Open space in the heap, assign address
3. And initialize the subclass object in the heap by default
4. Its constructor initialization is then called, but first the constructor of the parent class is called by default.
5. After the initialization of the parent class, the constructor of the subclass will not display the object.
6. When the initialization is complete, the address of the object in the heap is assigned to a reference to the object in the stack, which points to the object
Note: It is generally not specified to call super (), or you can also specify a specific parent constructor initialization, if the corresponding parent class constructor is not found, a compilation failure occurs

---final keyword---modifier
1. Modifier class--cannot be inherited
2. Modifier member Variable--is a constant, can only be assigned once, usually add static decoration
Values are fixed-preferably defined as constants, writing specifications: All uppercase letters and underscores to separate multiple words
3. Modify Member Method---cannot be overwritten
------Abstract class------occur because different subclasses have their own way of implementing methods that inherit from the parent class.
1. Concept: Once a class containing abstract methods (which may not contain abstract methods, but generally do not) must be modified with an abstract, defined as an abstraction class
Abstract method: Only method head, no method body, modified with abstract
2. Features
Abstract class cannot be instantiated--cannot create object with new--call abstract method meaningless
Subclasses to inherit abstract classes must override all abstract methods of the parent class (guaranteed subclasses are generic classes)
3. Abstract type Small details
* Abstract classes can have constructors--for subclass instantiation
* Abstract classes can not define abstract methods-this is not common-AWT inside the adapter class
* Abstract classes cannot coexist with those keywords
This private (method to be overwritten) static (static does not require object) final (overwrite not allowed)
* Differences between abstract classes and general classes
The same point: all are used to describe things, all define members
Different points: The General class has enough information to describe things, abstract classes describe the lack of information; Abstract methods cannot be defined in generic classes, generic classes can be instantiated, abstract classes cannot be instantiated
* Abstract class must be a parent class--yes
------interface-------
1. Concept
External provides a function, other classes want to have this function, directly implement the interface
Define interface: Interface interface name {member variable; abstract method}
Uniform modifier for member variables in interface: public static final
Interface member method: Public abstract
These modifiers can be omitted from the interface, but it is best not to omit them and affect the readability
2. Interface Features
cannot be instantiated, providing a certain function externally
Subclasses can implement multiple interfaces, and multiple inheritance is supported between interfaces
Between subclasses and interfaces, subclasses need this function to implement the interface--subclasses and interfaces directly relate to like a
The relationship between subclasses and interfaces is weak and is primarily used to extend the functionality of classes to reduce the coupling between classes
Eg:usb interface--as long as the USB interface function, you can connect with the USB interface
3. Application of the interface
Define the interface (that is, the rule)--later the program implements the interface (rule)
The relationship between A and B is too tight, difficult to expand and maintain, so in the design of the time to define an interface, convenient for future expansion and maintenance
Summary: 1. Interface definition rules and usage rules 2. Implementation rules
----polymorphic-----
1. Overview
Definition: A class of things with a variety of forms, eg cat, both belong to the cat, also belong to the animal class
Polymorphism occurs because the data was previously encapsulated, but as more and more objects are created, the problem becomes complex, so look for its commonality in these objects (let these objects show the commonality of their parent class-the problem from the subclass object represents itself to the commonality of its parent)
eg. a cat eats something, a dog eats something, a pig eats something.
Its parent class also defines eating, but different sub-classes eat things differently, to rewrite the method, if you want to call the sub-class to eat things, the general practice of creating sub-class objects, through the subclass object reference call method, so that code
Reusability is not good, it is not conducive to the extension of late code
public static void Eat (Cat c) {c.eat ();}
public static void Eat (Dog d) {d.eat ();}
Improved post-code
public static void Eat (Animal a) {a.eat ();} Pass in a subclass object inside to facilitate code extension
2. Polymorphism Benefits
Increase the extensibility of your code
Pre-defined code can use the content of the post-definition code
3. Conditions and drawbacks of polymorphism
Prerequisite: Must have inheritance, override, parent class reference to child class object
Cons: Cannot invoke subclass-specific content (up transformation)
4. Transformation of reference types
Upward Transformation-upcasting
The parent class reference points to the subclass object, which is considered a form of the parent class
Animal a = new Cat (); A at this point only shows the nature of animals the cat must be an animal, belonging to the automatic transformation, when the object reference a can only invoke the method of animal class definition, not access to cat-specific content
Down Transformation-downcasting
Cat C = (int) a;-belongs to the display transformation, at which point C refers to all the contents of the cat, can access the content of the subclass specific
Cat--Upper turn--cat as animal form--down turn--cat goes back to Cat's form
Note: Transitions are always a child-like object in a state of transition
Will----transformation succeed----
1. Upward transformation must be successful
2. A downward transition is not necessarily the case or subclass of the child class passed to the parent class if it is the instance of the class being transformed down
It is usually judged before downward transformation A (reference) instanceof B (class, interface)
That is, whether a actual type is an instance of Class B/interface or an instance of its subclass
----The variation of the various members under polymorphic existence------
Invocation of member variables: compile (if undefined, compile fails) and run time only look at the claim type-because the subclass is already considered a parent class feature, so to access the properties of the parent class
The invocation of the member function (dependent on the object, must be bound dynamically): Compile time to detect whether the declaration type has the method, run time to call the subclass override method (overriding method only occurs on the member function); The runtime refers to the address of the child class object, so it executes the method that is overridden in the subclass ( dependent on the Class);
A call to a static function (which relies on a class and does not depend on a specific object): It is called by the declaration type according to the compilation run
Note: Static methods are actually called directly through the class name.
member variables, static functions look left only, member functions, compile look left, run look left (dependent on specific objects)
Essentially polymorphic: Just for the extensibility of the Code
-----Inner class----
1. Role: Design time in order to better organize the relationship between classes and classes, a class is defined in the internal formation of another class (Outer encapsulation class and inner class)
2. Accessibility features
The inner class holds the external class reference and can access all members of the external class directly
External classes to access the members of the inner class must create an object of the inner class
The inner class must have an external class that belongs to
An inner class is a normal class, but is defined inside a class.
3. When to use: The design of the class, the analysis of things, the discovery of things inside, and the things to access the content of the described things, this is the class inside the definition of the inner class-equivalent to a package
4. Modifier of the inner class--The inner class defines the position of the member in the outer class and can be decorated with the member modifier
private--only works in the original.
default--also has direct access to the inner class in other classes (uncommon-the inner class is usually encapsulated just for this class of service) Outer.Inner in = new Outer (). New Inner ();--Must have an external class of affiliation
static--equivalent to an external class outer.inner in = new Outer.Inner ();
If the inner class is static, the member is static, and the inner class is loaded with the outer class loaded without creating an object of the outer class, calling the static member directly with the class name. If the inner class defines a static member, the inner class must be defined as a static
5. Inner class details Outer.this.num; the inner class accesses the members of the external class directly because the inner class holds the external class's reference to the external class. this--represents a reference to an external class
6. Local inner class: Place the inner class inside the method---for easy organization of the class (not much), you can directly access the values of local variables that are final decorated locally
7. Anonymous inner class--is the shorthand form of the inner class--for convenience
Premise: An inner class must inherit/implement an external class or interface eg: Event listener
Eg:a is an inner class that inherits the abstract class B new B () {overriding abstract method}
Anonymous intrinsic class Essence: An anonymous subclass object that simplifies encapsulation
Anonymous inner class format: New parent class/interface () {Subclass Content-Override abstract method}--represents subclass object and overrides abstract method
Application of anonymous inner class:
The number of abstract methods to implement an interface is less than 3, otherwise it appears too much code
Can be passed as an actual parameter using an anonymous inner class

Java Basics Summary-object-oriented 2

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.