1. Abstract class
rule of grammar
Role of abstract classes
2. Interface
rule of grammar
Implement multiple interfaces
Inheritance between interfaces
Clonable interface and deep copy
Shallow copy VS deep copy
summary
1. Abstract class
=======
rule of grammar
There is no actual working method. We can design it as an abstract method. The class containing the abstract method is called abstract class
abstract class Shape { abstract public void draw(); }
For a class containing abstract methods, the abstract keyword must be added to indicate that it is an abstract class
matters needing attention
-
Abstract classes cannot be instantiated directly
-
Abstract methods cannot be private
-
Abstract classes can contain other non abstract methods or fields The rules of this non abstract method are the same as those of ordinary methods. It can be overridden or called directly by subclasses
Role of abstract classes
Abstract classes exist in order to be inherited
An abstract class itself cannot be instantiated. To use it, you can only create subclasses of the abstract class Then let the subclass override the abstract method in the abstract class
Some students may say that ordinary classes can also be inherited and ordinary methods can also be rewritten. Why do you have to use abstract classes and abstract methods? Such is the case. However, the use of abstract classes is equivalent to an extra check of the compiler
The scenario of using abstract classes is like the above code. The actual work should not be completed by the parent class, but by the child class At this time, if you accidentally misuse the parent class, you will not report an error using the ordinary class compiler However, if the parent class is an abstract class, an error will be prompted when instantiating, so that we can find the problem as soon as possible
The meaning of many grammars is to "prevent errors". For example, final, which we used to use, is similar If the user does not modify the created variable, it is equivalent to a constant? But in addition, final can let the compiler remind us in time when it is accidentally modified Making full use of compiler verification is very meaningful in practical development
2. Interface
======
Interface is a further step of abstract class Abstract classes can also contain non - abstract methods and fields The methods contained in the interface are abstract methods, and the fields can only contain static constants
rule of grammar
interface IShape { void draw(); } class Cycle implements IShape { @Override public void draw() { System.out.println("○"); } } public class Test { public static void main(String[] args) { IShape shape = new Rect(); shape.draw(); } }
Define an interface using interface
The methods in the interface must be abstract methods, so abstract can be omitted
The methods in the interface must be public, so public can be omitted
Cycle uses implements to inherit the interface At this time, the meaning of expression is no longer "extension", but "implementation"
When calling, you can also create an interface reference corresponding to an instance of a subclass
Interfaces cannot be instantiated alone
Extensions vs. implementations extension refers to the further expansion of functions that already have certain functions Implementation means that there is nothing at present and needs to be constructed from scratch
An interface can only contain abstract methods For fields, the interface can only contain final static
interface IShape { void draw(); public static final int num = 10; }
The public, static and final keywords can be omitted The omitted num still represents the static constant of public
Tips:
1. When we create an interface, the name of the interface generally starts with the capital letter I
2. The naming of the interface generally uses the word of "adjective"
3. Alibaba code specification stipulates that methods and attributes in the interface should not be decorated with any symbols to keep the code concise
Implement multiple interfaces
Sometimes we need to make a class inherit from multiple parent classes at the same time This is achieved in some programming languages through multiple inheritance However, only single inheritance is supported in Java, and a class can only extend a parent class However, multiple interfaces can be implemented at the same time, and the similar effect of multiple inheritance can be achieved Now we represent a group of animals by classes
class Animal { protected String name; public Animal(String name) { this.name = name; } }
In addition, we provide a group of interfaces, which respectively mean "can fly", "can run" and "can swim"
interface IFlying { void fly(); } interface IRunning { void run(); } interface ISwimming { void swim(); }
Next, we create several specific animals
Cats can run
class Cat extends Animal implements IRunning { public Cat(String name) { super(name); } @Override public void run() { System.out.println(this.name + "Running on four legs"); } }
Fish can swim
class Fish extends Animal implements ISwimming { public Fish(String name) { super(name); } @Override public void swim() { System.out.println(this.name + "He is swimming with his tail"); } }
Frogs can run and swim (amphibians)
class Frog extends Animal implements IRunning, ISwimming { public Frog(String name) { super(name); } @Override public void run() { System.out.println(this.name + "Jumping forward"); } @Override public void swim() { System.out.println(this.name + "He is kicking his legs to swim"); } }
There is also a magical animal called "duck", which lives in water, land and air
class Duck extends Animal implements IRunning, ISwimming, IFlying { public Duck(String name) { super(name); } @Override public void fly() { System.out.println(this.name + "Flying with wings"); } @Override public void run() { System.out.println(this.name + "He is running on two legs"); } @Override public void swim() { System.out.println(this.name + "Floating on the water"); } }
The above code shows the most common usage in Java object-oriented programming:
A class inherits a parent class and implements multiple interfaces at the same time
The meaning of inheritance expression is is - a semantics, while the meaning of interface expression has xxx characteristics
What are the benefits of this design? Keep the benefits of polymorphism in mind and let the program forget the type With an interface, users of a class do not have to focus on specific types, but only on whether a class has certain capabilities
Inheritance between interfaces
Interfaces can inherit an interface to achieve the effect of reuse Use the extends keyword
interface IRunning { void run(); } interface ISwimming { void swim(); } // Amphibians can run and swim interface IAmphibious extends IRunning, ISwimming { }