Preface
Hello everyone, I am still the programmer who can’t punch. This issue brings you an explanation of abstract classes, interfaces, and Object classes. The article passes the basic concept of abstract class, the concrete implementation of abstract class, the basic concept of interface, and the realization of interface. Then to explain the basic concept of Object and the implementation of Object to rewrite the equals method, the article is worth reading with both pictures and texts.
Directory
1. Abstract class
1.1 The concept of abstract class
1.2 Abstract class syntax
1.3 Characteristics of abstract classes
2. Interface
2.1 The concept of interface
2.2 Grammar Rules
2.3 Characteristics of the interface
2.4 Inheritance between interfaces
2.5 Inheritance and interface inheritance at the same time
2.6 Example of interface usage
2.7 Difference between abstract class and interface class
3. Object class
3.1 Object comparison equals method
1. Abstract class
1.1 The concept of abstract class
In object-oriented, all objects are described by classes. But if one class cannot completely describe the specific information of another class, then we call this class an abstract class. for example:
Description:
- In the vegetable Vegetable class, there is an eat() method, which means that the vegetables can be eaten. But it cannot represent the taste of all vegetables, so the eat() method of each vegetable cannot be implemented.
- Chinese cabbage is a vegetable, so Pakchou and the Vegetable Vegetable class have an inheritance relationship, but Chinese cabbage tastes sweet, and only its own eat() method can realize it.
- Tomatoes are also vegetables, so Tomato is an inheritance relationship with the Vegetable class, but tomatoes taste sweet and sour, which can only be realized by its own eat() method.
- Bitter melon is also a vegetable, so Bitter and the vegetable Vegetable class have an inheritance relationship, but bitter melon tastes bitter, which can only be represented by its own eat() method.
- So Vegetable can be designed as an “abstract class”.
1.2 Abstract Class Grammar
The grammatical format of an abstract class is:abstract class class name{}, the abstract class is modified by abstract, and then the unified symbol of the class< strong>class followed by class name. For example, a vegetable class:
abstract class Vegetable { //Member variables public int num; //Member method public void fun(){ } //abstract method abstract void show(); //Construction method public void Vegetable() { } }
Abstract classes are also classes, which can contain common methods and attributes, and even construction methods.
1.3 Characteristics of abstract classes
Feature 1, abstract classes do not necessarily have abstract methods
abstract class Vegetable { public void eat() { System.out.println("Vegetable benefits"); } }
It is also possible to have ordinary methods in an abstract class, and it is not limited to only abstract methods.
Feature 2, abstract methods in abstract classes can be defined without initialization
abstract class Vegetable { abstract void show(); }
The abstract method in the abstract class can be terminated directly with a semicolon (;) without initialization.
Feature 3, if a class inherits an abstract class, then this class must rewrite the abstract method in the abstract class
abstract class Vegetable { abstract void show(); } class Tomato extends Vegetable { }
Output after running:
Therefore, When there are abstract methods in our abstract class, as long as a subclass inherits this abstract class, the abstract method must be overridden in the subclass. We should write code like this:
abstract class Vegetable { abstract void show(); } class Tomato extends Vegetable { public void show() { } }
Feature 4, abstract classes cannot be instantiated, but subclass objects can be instantiated.
abstract class Vegetable { public void eat() { System.out.println("Vegetables are delicious"); } } public class Test { public static void main(String[] args) { Vegetable vegetable = new Vegetable(); } }
Output after running:
But when we use an abstract class to instantiate a subclass object.
abstract class Vegetable { public void eat() { System.out.println("Vegetable benefits"); } } class Tomato extends Vegetable { @Override public void eat() { System.out.println("Sweet and sour tomatoes"); } } public class Test { public static void main(String[] args) { Vegetable vegetable = new Tomato(); vegetable. eat(); } }
Output after running:
We can See, the abstract class Vegetable can instantiate its subclass object Tomato.
Feature 5, abstract methods cannot be modified by private, static, final, and must meet the rewriting rules
abstract class Vegetable { abstract private void show(); abstract final void fun(); abstract private void show(); }
Output after running:
Feature 6, the abstract class can have a construction method for the subclass to initialize the member variables of the parent class when creating an object
abstract class Vegetable { public Vegetable(int a,int b ) { System.out.println(a + b); } } class Tomato extends Vegetable { public Tomato(int a,int b) { super(a,b); } public void show() { } } public class Test { public static void main(String[] args) { Vegetable vegetable = new Tomato(2,3); } }
Output after running:
2. Interface
2.1 Concept of interface
Interfaces include USB interfaces, plug-in interfaces and so on in our lives. The USB interface is specially used for data transmission or charging, and the socket interface has two-hole sockets and three-hole sockets.
Then these interfaces are stipulated, which stipulates what kind of operations are implemented. The USB interface is used for data connection, and the power strip is used for charging. This is their specification. Therefore, Java also has its own specifications and operations, which we call interfaces.
2.2 Grammar Rules
The interface is defined by the keyword interface, just change the class to interface on the basis of the class.
interface IUSB { //... }
The above is the definition of an interface, which defines an interface named IUSB.
Tip:
- When creating an interface, the name of the interface generally starts with a capital letter I.
- Interfaces are generally named using “adjective” words.
- As stipulated in the Ali coding specification, the methods and properties in the interface should not add any modifiers to keep the code concise.
2.3 Features of Interface
(1) The interface cannot be instantiated
interface Usb { //... } public class Test { public static void main(String[] args) { // Interface cannot be instantiated Usb usb = new Usb(); } }
Output after running:
When we , after instantiating an interface, the compiler will consider the interface as abstract, so we believe that the interface cannot be instantiated.
(2) Member variables must be initialized when defining in the interface
interface Usb { //Member variables in the interface are not initialized public int num; public String name; } public class Test { public static void main(String[] args) { } }
Output after running:
When we After running without initializing the member variables in the interface, an error will be reported, so we should initialize the member variables in the interface.
(3) Member variables in the interface default to public static final type
interface Animal { // complete definition public static final int num = 10; //The compiler will add public static final by default String nam = "abc"; }
If our variables do not have public static final, the compiler will default to these variables.
(4) The methods in the interface default to abstract methods
interface Usb { //abstract method public abstract void show(); //The public abstract is omitted, but the compiler will add it by default void fun(); }
(5) The method in the interface cannot have content
interface Usb { //method with method body public void show() { } } public class Test { public static void main(String[] args) { } }
Output after running:
The above code When displaying a method defined in an interface, The method cannot contain a method body, that is, {}, but the method in the interface can be defined without initialization. like:
interface Usb { //method without method body public void show(); } public class Test { public static void main(String[] args) { } }
We just need to end this method with a semicolon (;).
(6) To implement the method in the interface, you must add default
//Interface named Usb interface Usb { //The method modified by default can have a method body in the interface default void show(){ System.out.println("Today"); } }
(7) There can be no constructor in the interface
interface Usb { public int num = 10; //Interfaces cannot have constructors public int Usb(int num){ } }
Output after running:
After the above code is run, the result is the same as the method in the interface that cannot have content.
(8) There can be static modified methods in the interface
interface Animal { public static void eat() { System.out.println("Animals love food"); } }
When we run the above code it is completely passable.
(9) When inheriting an interface, use implements to modify the interface name
interface Usb { public int num = 10; } //Inheritance interface uses implements class Fun implements Usb { }
Unlike ordinary class inheritance, ordinary classes are inherited by extends, while interfaces are inherited by implements.
2.4 Inheritance between interfaces
The extends keyword is also used for the inheritance between interfaces, and the interface can implement single inheritance, multiple inheritance and multi-layer inheritance.
(1) Single inheritance
interface A { } interface B extends A { }
The so-called single inheritance is like interface 2 inheriting interface 1. What is shown in the above code is that interface B inherits interface A.
(2) Multi-level inheritance
interface A { } interface B extends A { } interface C extends B { }
Multi-layer inheritance means that interface 3 inherits from interface 2, and interface 2 inherits from interface 1. The above code shows that C inherits from B, and B inherits from A.
(3) Multiple inheritance
interface A { } interface B { } interface C extends A,B { }
Multiple inheritance means that an interface inherits multiple interfaces. This is not feasible in inheritance between our classes, but it is perfectly possible to do this between interfaces.
2.5 inheritance and interface inheritance at the same time
When there is the following code:
abstract class Animal { public void run() { System.out.println("Run"); } } class Dog extends Animal { @Override public void run() { super. run(); } } class Bird extends Animal { @Override public void run() { super. run(); } } public class Test { public static void main(String[] args) { Dog dog = new Dog(); Bird bird = new Bird(); dog. run(); bird. run(); } }
Output after running:
In the above code, when the Dog class inherits the Animal class, the run method in it is suitable for the Dog class because the dog’s walking method is to run, and the run method in the Bird class after inheriting the Animl class is not suitable for the Bird class. Because the way birds walk is to fly, we can provide the methods that birds need through an interface.
abstract class Animal { public void run() { System.out.println("Run"); } } class Dog extends Animal { @Override public void run() { super. run(); } } interface Irun { void run(); } class Bird extends Animal implements Irun{ @Override public void run() { System.out.println("fly"); } } public class Test { public static void main(String[] args) { Dog dog = new Dog(); Bird bird = new Bird(); dog. run(); bird. run(); } }
Output after running:
We can It is found that the Bird class inherits the Animal class and inherits the Irun interface. Finally, there is an effect that the bird can fly.
2.6 interface usage examples
(1) Realize simple inheritance interface
We know that methods in interfaces are abstract methods. Therefore, when a subclass inherits this interface, the methods in the interface should be rewritten in the subclass.
interface Animal { void eat(); } class Dog implements Animal { @Override public void eat() { System.out.println("Eat dog food"); } } public class Test { public static void main(String[] args) { Animal animal = new Dog(); animal. eat(); } }
Output after running:
Above code It is a good demonstration that the inherited interface overrides the method and outputs it.
(2) Implement class name reference
From 2.4 above, I learned that there can be static modified methods in the interface, so when there are static methods in our interface, we can also directly use the class name to refer to them.
interface Animal { public static void eat() { System.out.println("Animals love food"); } } public class Test { public static void main(String[] args) { Animal. eat(); } }
Output after running:
(3) Realize the USB interface
//IUSB.java file public interface IUSB { //Open void open(); //Finish void close(); } //Mouse.java file public class Mouse implements IUSB{ @Override //rewrite open public void open() { System.out.println("Mouse click to start"); } //rewrite close @Override public void close() { System.out.println("Mouse click ends"); } //mouse operation public void mouseServe() { System.out.println("click~~~~"); } } //Under the KeyBoard.java file public class KeyBoard implements IUSB{ @Override //rewrite open public void open() { System.out.println("Keyboard printing starts"); } //rewrite close @Override public void close() { System.out.println("keyboard print end"); } //keyboard operation public void keyBoardServe() { System.out.println("Printing~~~~"); } } //Computer.java file public class Computer { public static void serive(IUSB str) { str. open(); if (str instanceof Mouse) { //Downcast, because there is no mouseSerive method in the IUSB interface Mouse mouse = (Mouse) str; mouse. mouseServive(); }else if (str instanceof KeyBoard) { //Downcast, because there is no keyBoardServive method in the IUSB interface KeyBoard keyBoard = (KeyBoard) str; keyBoard. keyBoardServe(); } str. close(); } public static void main(String[] args) { //Mouse's anonymous object is passed to the service method serive(new Mouse()); //The anonymous object of KeyBoard is passed to the service method serive(new KeyBoard()); } }
Output after running:
Mouse click to start
click~~~~
mouse click end
Keyboard printing starts
printing~~~~
keyboard print end
Through the above examples, we can find that the interface can be transformed upwards, downwards, and polymorphic.
2.7 Difference between abstract class and interface class
Abstract classes use abstract plus class decoration, while interface classes use interface instead of class decoration.
abstract class Animal { } interface IUSB{ }
The member methods and member variables in the interface are modified by public static final by default.
interface IUSB{ //Comprehensive writing public static final int num = 10; //The compiler defaults to public static final name = "Zhang San"; String name = "Zhang San"; }
When the method in the abstract class is inherited by the subclass, the subclass does not need to rewrite this method, and when the method in the interface class is inherited, the subclass must rewrite this method. The keyword extends is used to inherit abstract classes, and implements is used to inherit interface classes.
interface IUSB{ void show(); } class Try implements IUSB { @Override public void show() { } } abstract class Animal { public void fun() { } } class Dog extends Animal { }
The member variables in the abstract class may not be initialized, but the member variables in the interface class must be initialized.
interface IUSB{ public String name; public int age; } abstract class Animal { public String name; public int age; }
A warning occurred:
A method in an abstract class can have a code block, but a method in an interface class cannot have a code block.
interface IUSB{ void show(){ } } abstract class Animal { public void show() { } }
Output after running:
3. Object class
The Object class is the parent class of all classes, how to reflect it, please see the following code:
//A dog class inherits the Object class class Dog extends Object{ } //A cat class inherits the Object class class Cat extends Object{ } //An abstract class inherits the Object class abstract class Brid extends Object { } public class Test { public static void main(String[] args) { //obj refers to an Object class object Object obj = new Object(); } }
Through the above code, we can see. Our normal classes, abstract classes, instantiated objects. You can use the Object class to inherit or generate objects.
3.1 Object comparison equals method
When we have a code like this:
class Student { public String name; public int age; public int score; } public class Test { public static void main(String[] args) { Student student1 = new Student(); Student student2 = new Student(); student1.name = "zhangsan"; student2.name = "zhangsan"; boolean flag = student1. equals(student2); System.out.println(flag); } }
Output after running:
At this point we have a question, why does it return false when student1 and student2 refer to the same value of the two objects? Because at this time when we call the equals method, we call the equals method in the Object class, we can see that it is useless. The following is the equals method in the Object class:
What we really need to call is the equals method in String, which is the following code:
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value. length; if (n == anotherString. value. length) { char v1[] = value; char v2[] = anotherString. value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i + + ; } return true; } } return false; }
So we have to override an equals method in the Student class to achieve our desired effect.
class Student { public String name; public int age; public int score; @Override public boolean equals(Object obj) { Student student = (Student) obj; if (this.name.equals(student.name)) { return true; } return false; } } public class Test { public static void main(String[] args) { Student student1 = new Student(); Student student2 = new Student(); student1.name = "zhangsan"; student2.name = "zhangsan"; boolean flag = student1. equals(student2); System.out.println(flag); } }
Output after running:
We can see that the output is true, the only difficulty is which object this.name in the Class class is. We know that the first object created when an object is created is the object of this class. So our student1 reference represents the Student class. So student1 is the current object, so this.name is the name in student1. In the main method, we pass the parameter to the equals method that we rewrite. The parameter is the name in the object referenced by student2. I believe that if you distinguish these two references, it will not be difficult to understand.
Therefore, we can find that all classes can refer to the Object class, but if we do not make good use of the Object class, we will not achieve the effect we want to achieve. So we have to think twice when using the Object class.
Okay, this blog post is over here, thank you for reading. If you gain something, please give the blogger a little attention, thank you for your support.
Next notice: library management system