J2se basics: 5. Object-oriented features 3

Source: Internet
Author: User
Tags define abstract
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:

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.