Four weeks Study Summary supplement

Source: Internet
Author: User
Tags abstract definition

1. Construction methods cannot be inherited by subclasses, but when the subclass object is created, the constructor method of the parent class is called to produce the object.

2. Because the subclass object contains the full parent class object, all properties and methods in the parent class object are inheritable (including private properties and private methods). Only private properties and private methods cannot be accessed directly by the quilt class, and can be called through public methods.

3. Static code block

static{

}

Executes automatically when the class loads, and executes only once.

When you need to import, such as scanner and FileReader, you can use static blocks of code when you need to import or operate only one time, which is better than in the construction method.

4. Instantiating a block of code

{

}

Each time an object is generated, a code block is executed once, and execution is performed immediately after the super () in the subclass constructor method, before the other statements.

5. polymorphic

Allows a parent class variable to refer to an object of a subclass (equivalent to allowing a double x=int type of 3, a small type to be transformed into a large type); Allows an interface type variable to reference the implementation class object. Not in turn.

One sentence description: The same behavior, but there are different implementations.

Example:

public class animals () {

}

public class Dog extends animals () {

}

public class Cat extends animals () {

}

Animals an1=new Dog ();

Animals an2=new Cat ();

Because a child class contains a complete parent class object, the methods and properties defined in the parent class variable are found in the subclass object.

So the parent class object can point to the subclass object. In turn, however, the child class object may have more methods and properties than the parent class, so the subclass variable cannot point to the parent class object.

When a polymorphic call is made, the parent class variable is used to point to the subclass object, only methods and properties defined in the parent class variable can be called, and methods and properties specific to the subclass cannot be called.

When a polymorphic call method is called, the subclass override method is called first.

Similarly, object as the parent of all classes can point to objects of any class such as: Object Ob1=new Animals (), Object Ob1=new Dog (), Object Ob1=new Cat ();

6. Inner class

Defining a class inside a class is called an inner class that contains an inner class that is an external class

The inner class is equivalent to the outer class, and the generation of the inner class object must depend on the generation of the outer class object.

Naming rules for internal classes: external classes such as A.class internal class name B so the inner class name is A$b.class

Example:

public class student{

Public student () {System.out.println ("Student class Creation Complete")};

public class goodstudent{

Public Goodstudent () {

System.out.println ("Good student internal class creation complete");

}

}

public static void Main (String [] args) {

Student stu1=new Student ();

Student.goodstudent gs1=stu1.new goodstudent ();

}

}

7. Multi-State implementation

Cohesion Poly-low coupling (temporarily not understood)

8. For Each loop for iterating over array elements

Example:

Animals [] Arr1=new Animals [8];

for (Animals a:arr1) {

A.eat ();

}

9. A (variable name) instanceof type (for example: Student) Returns a Boolean value note that instanceof is linked together.

Example:

if (arr[2] instanceof Human) {
System.out.println ("homogeneous");
}

For a variable he belongs to his own class also belongs to the parent class all return True

10. Object-oriented features

Inheritance, encapsulation, polymorphism

Supplemental: Static polymorphic and dynamic polymorphic

Static polymorphism refers to the program at compile time, the system can decide which function to call, such as overloading
Dynamic polymorphism means that the object that the operation pointer refers to can be determined dynamically in operation, mainly through virtual function and rewriting

11. Abstract class

If a method in a class has only a definition and no specific implementation, then this method is called an abstract method.

With the keyword abstract definition, a class that has an abstract method must be an abstract class, and must be modified with an abstract.

An abstract class cannot instantiate an object. (Human, for example, is an abstract class in itself.) Can not be said to instantiate a human out, and should be said to instantiate a person out, this person is an abstract class of human subclasses, the instantiation of this subclass to achieve. If you want to implement only by pointing to the subclass object Human hu1=new man ();

Abstract methods of abstract classes are implemented by subclasses according to their own characteristics.

General situation of abstract methods: Public abstract void Getmoney ();

Cannot have curly braces {}, otherwise an error, because with {} is also an implementation, and the abstract method is not possible.

After the subclass inherits the abstract class, the quick way to add the abstract method is to click on the Red Fork and choose the Add unimplement methods option.

and abstract classes have several abstract methods, subclasses must all implement a few. If not fully implemented, then it is only possible to define this subclass as an abstract class.

Add: You can create multiple classes in a file, but only one public class and the main method must be placed in the public class.

Four weeks Study Summary supplement

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.