J2SE basics: 6. Object-oriented features 3. j2se object-oriented

Source: Internet
Author: User

J2SE basics: 6. Object-oriented features 3. j2se object-oriented
1: abstract method:

When a method of the parent class is overwritten by the quilt class, the method of the parent class does not exist.
Implementation is necessary. But it must be declared.

Therefore, this method can define abstract methods (only declarations are not implemented ).


2: abstract class:


Abstract methods are incomplete, so the class to which Abstract METHODS belong is also an incomplete
Complete class, so the class must be defined as an abstract class.

Abstract classes can contain abstract and non-abstract methods.


3: When a subclass inherits the abstract class

Subclass must overwrite all abstract methods in the abstract class.



If the subclass does not want to be implemented, you must define an abstract class.


 
4: The abstract class cannot be instantiated. It can only be instantiated as a subclass.
Animal animal = new Dog ();

This type of code is traced back to the instance, and the implementation method is definitely polymorphism.




Interface:
An interface is a special abstract class. Contains only the definitions of constants and methods.

All variables defined in the interface are variables (public static final is used by default)

All methods defined in the interface are abstract methods (public abstract is used by default)



Because an interface is a special abstract class, the interface cannot be instantiated.

Instantiated is also a subclass of the interface.

Subclass inheritance class that implements one or more interfaces.

Public class TestImp extends Animal
Implements Test, TestI2 {
}
Because the class implements the interface, it also inherits the constant defined by the interface. The method in the interface is overwritten.
Multi-inheritance relationships are implemented.

The interface cannot be instantiated. Only the implementation class of the interface can be instantiated.

Note: When multiple interfaces are implemented, do not define
Interface Method.


Relationship between interfaces and abstract classes
An interface can only inherit interfaces, but cannot inherit abstract classes.

Abstract classes can only inherit abstract classes. abstract classes can implement interfaces.




When to use interfaces and abstract classes
An abstract class is a class that can contain the definition of variables and (abstract) methods.
It focuses on Describing Objects and the inheritance relationship between objects.

The interface is essentially not a class, and only contains the definition of Class constants and abstract methods.
Focus only on the functions and implementation required by many classes.

Such as on/off, it is more suitable to define interface methods.

Such as opening/closing: Suitable for defining Abstract METHODS


Category:
External class:
The class defined outside class {} is an external class.
External classes can be called by other classes.
Internal class: the class defined inside class {} is an internal class.
Internal classes can only be called for their own classes.

The internal class may directly call the attributes and methods of the external class.

System. out. println (I );
System. out. print (TestInnerClass. this. I );

Generate the class format
External class. class $ internal class. class

Anonymous class:

The three main features of object-oriented, and briefly describe their functions,

1. Information Hiding and encapsulation features:
Encapsulation is to enclose the process and data. Data access can only be performed through the defined interface. Object-oriented computing begins with the basic concept that the real world can be depicted as a series of completely autonomous and encapsulated objects that access other objects through a protected interface.
2. Inheritance:
Inheritance is a hierarchical model that connects classes and allows reuse of encouraging classes. It provides a way to clearly express commonalities. A new class of an object can be derived from an existing class. This process is called class inheritance. The new class inherits the features of the original class. The new class is called the derived class (subclass) of the original class, and the original class is called the base class (parent class) of the new class ). A derived class can inherit methods and instance variables from its base class, and the class can modify or add new methods to make it more suitable for special needs.
3. Combination features
A combination is used to indicate the "whole/part" relationship of a class. For example, host, display, keyboard, and mouse are combined into a computer.
4. Dynamic Features
(1) Abstraction:
Abstraction is to ignore those aspects irrelevant to the current target in a topic, so that you can pay more attention to the aspects related to the current target. Abstraction is not intended to understand all the problems, but to select a part of the problem. Abstract: Process abstraction and data abstraction.
(2) polymorphism:
Polymorphism allows different types of objects to respond to the same message. Polymorphism includes parameterized polymorphism and inclusion polymorphism. The polymorphism language has the advantages of flexibility, abstraction, behavior sharing, and code sharing, which effectively solves the same name problem of application functions.

What are J2SE basics?

1 Java Introduction 2 basic syntax 3 object-oriented 4 exception handling 5 arrays 6 common Class 7 container 8io stream 9 thread 10 network 11GUI
 

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.