Abstract class, interface, polymorphism are object-oriented objects are very basic things, I believe see can this blog people will not be entangled in its basic definition, this article will try to dig deeper into its connotation, hope to be helpful to everyone.
First, abstract class
1. Form
1 Abstract classCeshi2 {3 Abstract voidShow ();4 }5 classDemoaextendsCeshi6 {7 voidShow ()8 {9System.out.print ("Hello World");Ten } One}
2. Features
A, abstract class can not be instantiated
b, the abstract class must have its subclasses covered by all abstract methods, the subclass can be instantiated, otherwise, the subclass is an abstract class.
C, the abstract method must be defined in an abstract class, and the class must also be abstract.
3. Then the question comes.
A, abstract class has a constructor function?
There, used to instantiate subclasses.
B. Can abstract classes not define abstract methods?
It's actually possible, but it's rare.
C, abstract class and general class similarities and differences.
Same point:
Both abstract and generic classes are used to describe things, and members are set internally.
Different points:
1, the general class has enough information to describe things. Abstract classes may not have enough information to describe things.
2, abstract methods cannot be defined in general classes, only non-abstract methods can be determined. Abstract methods can be defined in abstract classes, and non-abstract methods can also be defined.
3, the generic class can be instantiated. Abstract classes cannot be instantiated.
Second, the interface
In a sense, an interface is a special form of abstract class, in the Java language the abstract class represents an inheritance relationship, a class can inherit only one abstract class, and a class may implement multiple interfaces.
We all know how the interface is defined
Interface demo{ void Show ();
In fact, for the common members of the interface: and these members have fixed modifiers.
A, global constants: public static final
B, abstract method: Public abstract
1, it concludes that the members of the interface are public permissions. That is, the full write is
Interface demo{ publicstaticfinalint num = 4; Public Abstract void Showa ();}
2. Interface Features
A, interface is the rule of external exposure, is the extension of the function of the program.
b, the interface reduces the coupling.
C, a class can implement a plurality of interfaces
Abstract class extends Implements a,z{}
3. Interface and abstract class similarities and differences
Same point:
are constantly upward drawn.
Different points:
1, abstract classes need to be inherited, and can only be inherited. interfaces need to be implemented and can be implemented more.
2. Abstract methods and non-abstract methods can be defined in an abstract class, and after the subclass inherits, non-abstract methods can be used directly. Only abstract methods can be defined in an interface and must be implemented by subclasses.
Three, polymorphic
An object has a different shape, which is polymorphic.
such as animal ANI = new Dog ();
This is the embodiment of polymorphism in code: The parent class reference points to the subclass object.
1. Multi-state benefits and drawbacks
Polymorphism Benefits: Improved code extensibility, pre-defined code can use later content.
The disadvantage of polymorphism: pre-defined content cannot use (invoke) the content of later subclasses.
2. Downward transformation
New= (Cat) ANI;
3. Inner class
The inner class is the less we built before, in fact there are classes inside the class.
class Outer// external class { class Inner// inner class. {
}
A, how does an inner class reference an external class?
The inner class holds a reference to the external class. External class name. this.
class outer{ int num = 3; class Inner { int num = 4; void Show () { int num = 5; System.out.println (Outer. This . num); } } void method () { new Inner (). Show (); }}
3, the inner class can be stored in the local location, and in the local location can only access the local is the final modified local variables.
class outer{ void method () { finalint x = 9; class Inner { public String toString () { } }}}
4. Anonymous inner class
An anonymous inner class is a shorthand form of an inner class, which is actually an anonymous child object.
Public void method () { new Demo ()// anonymous inner class. { void Show () { System.out.println ("show:" +num); } }.show (); }
Iv. final
Abstract classes, interfaces, and polymorphism are simple, but commonly used, especially like inner classes, anonymous inner classes, which we have less contact with before, should focus on mastering.
Detailed Java Basics-abstract classes, interfaces and polymorphism