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 { } class Frog implements IAmphibious { ... }
Clonable interface and deep copy
Some useful interfaces are built in Java, and Clonable is one of them
There is a clone method in the Object class. Calling this method can create a "copy" of the Object
However, if you want to call the clone method legally, you must implement the Clonable interface first, otherwise you will throw a clonnotsupportedexception exception
class Animal implements Cloneable { private String name; @Override public Animal clone() { Animal o = null; try { o = (Animal)super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return o; } } public class Test { public static void main(String[] args) { Animal animal = new Animal(); Animal animal2 = animal.clone(); System.out.println(animal == animal2); } } // Output results // false
Shallow copy VS deep copy
The object copied by clonable is a "shallow copy". Observe the following code:
public class Test { static class A implements Cloneable { public int num = 0; @Override public A clone() throws CloneNotSupportedException { return (A)super.clone(); } } static class B implements Cloneable { public A a = new A(); @Override public B clone() throws CloneNotSupportedException { return (B)super.clone(); } } public static void main(String[] args) throws CloneNotSupportedException { B b = new B(); B b2 = b.clone(); b.a.num = 10; System.out.println(b2.a.num); } } // results of enforcement 10
The b object copied through the clone only copies the b itself, not the a object contained inside At this time, the a reference contained in b and b2 still points to the same object When you modify one side, the other side will also change
summary
==
Abstract classes and interfaces are common uses of polymorphism in Java
. At the same time, we should recognize the difference between the two (important!!! Common interview questions)
Core difference: abstract classes can contain ordinary methods and fields. Such ordinary methods and fields can be directly used by subclasses (without rewriting), while interfaces cannot contain ordinary methods. Subclasses must override all abstract methods
summary
The interview inevitably makes people anxious. Everyone who has experienced it knows. But it's much easier if you anticipate the questions the interviewer will ask you in advance and come up with appropriate answers.
In addition, it is said that "the interview makes a rocket, and the work screws". For friends preparing for the interview, you only need to understand one word: brush!
Brush me, brush hard! Since I'm here to talk about the interview today, I have to come to the real interview questions. It didn't take me 28 days to do a "collection of analysis of interview questions for high posts in large Java front-line factories: Java foundation - intermediate - Advanced interview + SSM framework + distributed + performance tuning + microservice + concurrent programming + Network + design pattern + data structure and algorithm, etc."
Data collection method: click here to download for free
And in addition to simply brushing questions, You also need to prepare a copy [JAVA advanced core knowledge manual]: JVM, JAVA collection, JAVA multithreading concurrency, JAVA foundation, Spring principle, microservice, Netty and RPC, network, log, Zookeeper, Kafka, RabbitMQ, Hbase, MongoDB, Cassandra, design pattern, load balancing, database, consistency algorithm, JAVA algorithm, data structure, encryption algorithm, distributed cache, Hadoop, Spark , Storm, YARN, machine learning and cloud computing are best used to check leaks and fill vacancies.
Analysis collection of interview questions for senior positions in va front-line large factories: JAVA foundation - intermediate - Advanced interview + SSM framework + distributed + performance tuning + microservice + concurrent programming + Network + design pattern + data structure and algorithm, etc
[external chain picture transferring... (img-cOMqC6iv-1629317130678)]
Data collection method: click here to download for free
And in addition to simply brushing questions, You also need to prepare a copy [JAVA advanced core knowledge manual]: JVM, JAVA collection, JAVA multithreading concurrency, JAVA foundation, Spring principle, microservice, Netty and RPC, network, log, Zookeeper, Kafka, RabbitMQ, Hbase, MongoDB, Cassandra, design pattern, load balancing, database, consistency algorithm, JAVA algorithm, data structure, encryption algorithm, distributed cache, Hadoop, Spark , Storm, YARN, machine learning and cloud computing are best used to check leaks and fill vacancies.