Java abstract classes and interfaces (note 13)

abstract classes and interfaces

An abstract class

In java, there is a method that only needs to be declared, not implemented. Its implementation is handed over to subclasses to complete. Such a method is called an abstract method.
A class with abstract methods is called abstract class
//example
abstract class GongAnJu {
abstract void catchThief(); //abstract method
abstract void sleep(); 
}

//implementation class of abstract class
class DaoWaiFenju extends GongAnJu{
void catchThief(){
System.out.println("catch one, run one");
}
void sleep(){
System.out.println("Sleep to death day by day");
}
}

class Test{
public static void main(String[] args) {
// DaoWaiFenju fenju=new DaoWaiFenju();
GongAnJu fenju=new DaoWaiFenju();
fenju.catchThief();
fenju.sleep();
}
}
abstract description 
1) declare with abstract
2) Abstract methods, only need to declare, do not need to implement
3) An abstract class cannot be new, only its subclass can be new (and this subclass must implement all abstract methods, otherwise its subclass is still an abstract class)
4) There can be no abstract methods in an abstract class
5) An abstract class can have ordinary members (attributes, ordinary functions, constructors, static members) //In fact, except for abstract methods, other abstract classes are the same as ordinary classes

2. Interface

If all the methods in an abstract class are abstract, they can be represented by an interface. Strictly speaking, an interface is a special abstract class
For example, the above abstract class can be expressed as 
interface  GongAnJu {
void catchThief(); 
void sleep(); 
}
Features of the interface
1) All the methods in the interface are abstract, you can use abstract or not
2) All methods in the interface are public by default
3) Ordinary members in the interface, like public static final
4) The interface is also inherited by subclasses, and the methods declared in it are rewritten. The keyword used to inherit the interface is implements
   Note that when implementing a method in an interface, be aware that its access level must be public
5) The interface can inherit the following writing method of the interface.
interface A{
int a();
}
interface B extends A{
int b();
}
6) The same class can implement multiple interfaces at the same time //Multiple implementations
interface Runner {
void run();
}
interface Flyer {
void fly();
}
class Bird implements Runner, Flyer {
public void fly() {
System.out.println("Swish flying");
}
public void run() {
System.out.println("Running with great strides");
}
}
7) A class, while inheriting another class, can implement one or more interfaces 
interface Runner {
void run();
}
interface Flyer {
void fly();
}
class Bird implements Runner, Flyer {
public void fly() {
System.out.println("Swish flying");
}
public void run() {
System.out.println("Running with great strides");
}
}
class Cat {
String nickname;
void catch mouse(){
System.out.println("Meow....The mouse hangs up");
}
}
class 貓精 extends Cat implements Flyer, Runner {
public void run() {
System.out.println("Swoosh to the room");
}
public void fly() {
System.out.println("Flying in the sky");
}
void eatPerson(){
System.out.println("Like to eat people");
}
}
class Test{
public static void main(String[] args) {
/*Cat essence mj=new cat essence();
mj.fly();
mj.run();
mj.eatPerson();
mj.catch mice();*/
Runner mj=new cat essence(); //ok
mj.run();
// ((Cat Essence)mj).fly();
// ((Flyer)mj).fly();
((Cat) mj).Catch the mouse(); 
}
}
8) Can an implementation class of an interface implement only a part of the methods in the interface? Yes, if so, the implementation class must be an abstract class
Comprehensive example
public interface  Bank{
void saveMoney();
void takeMoney();
void jiesuan();
void jiaoshui();
}

public class ABC implements Bank {
public void saveMoney() {
System.out.println("Money is deposited into the Agricultural Bank of China");
}
public void takeMoney() {
System.out.println("Withdraw money from ABC");
}
public void jiesuan() {
System.out.println("After leaving part of the ABC's money, hand it over to the People's Bank of China");
}
public void jiaoshui() {
System.out.println("Just pay less");
}
}

public class ICBC implements Bank{
public void saveMoney() {
System.out.println("Silver is deposited into ICBC's card");
}
public void takeMoney() {
System.out.println("The ATM often makes mistakes and swallows the card");
}
public void jiesuan() {
System.out.println("I don't understand the account");
}
public void jiaoshui() {
System.out.println("No money to pay");
}
}

public class Test {
public static void main(String[] args) {
Bank bank=new ICBC();
/*bank.saveMoney();
bank.takeMoney();
bank.jiesuan();
bank.jiaoshui();*/
Bank bank2=new ABC();
/* bank2.saveMoney();
bank2.takeMoney ();
bank2.jiesuan();
bank2.jiaoshui();*/
check the bank; 
check bank(bank2); 
}
static void Check Bank (Bank bank){ //Define parameters with interface
bank.jiaoshui();
bank.takeMoney();
bank.saveMoney();
bank.jiesuan();
}
}

3. Object comparison equals

class Student  {
public Student(String name, int score) {
this.name = name;
this.score = score;
}
String name; 
int score;
}

class T{
public static void main(String[] args) {
Student stu1=new Student("赵明明",20);
Student stu2=new Student("赵明明",20);
Compare directly with the double equals sign:
//System.out.println(stu1==stu2); //false Use the double equals sign to compare, which is to directly compare the values ​​of the two variables themselves
//For object comparison, JAVA recommends that we use equals 
boolean result= stu1.equals(stu2);    
System.out.println("The result is" + result); //false
/*In fact, the equals ratio in the Object class is the memory address of the two objects. The code is as follows
public boolean equals(Object obj) {
return (this == obj);
}
*/
}
}

//Override of the example equals method
class Student  {
public Student(String name, int score) {
this.name = name;
this.score = score;
}
String name; 
int score;
public boolean equals(Object obj) {
if(this==obj){
return true; //if the target object and this object are the same
}
if(obj instanceof Student){ //Only compare objects of the same type
Student tempStu = (Student) obj;
if(this.name.equals(tempStu.name) && this.score==tempStu.score){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
}   

Four, toString method

public class Student extends Object{
Student(int age, String name) {
this.age = age;
this.name = name;     // Alt+shift+s
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]";
}
int age;
String name;
}

class Test {
public static void main(String[] args) {
Student stu = new Student(20, "赵明明");
//System.out.println(stu); // Student@785d65 hash value default toStirng() will return a string like this
System.out.println(stu); //After rewriting: Student [age=20, name=Zhao Mingming]
Description: Printing an object directly is actually printing the return value of the object after calling the toString() method.
}
}

Five, final keyword

final means final
1) A variable defined with final becomes a constant, and the value of the constant cannot be changed
For example final double PI=3.14 ; // constants are usually named with all capital letters
PI=3.14.15926 ; //Error, because the value of constant cannot be changed
2) Classes modified with final cannot be inherited 
3) Methods modified with final cannot be overridden//Cannot override the final method from Parent
4) The class members defined with final must be initialized when they are defined. If they are not initialized when they are defined, they must be initialized in the constructor.
  If there are multiple constructors, initialize in each constructor//Possible error The blank final field ADDRESS may not have been initialized
5) If it is a member defined by static final, it must be initialized at the time of definition, such as static final ADDRESS="China"
6) Pay attention to the following situations
class Parent {
final double PI=3.14;
}
class Son extends Parent {
double PI=3.1415926;  //OK  ****
void test(){
//super.PI=3.1415926; //Error, trying to modify the constant value of the parent class
}
}
7) Use final to modify a variable of reference type, whether the value of the variable itself cannot be changed, or the reference it points to cannot change//the value of itself cannot be changed
class T{
public static void main(String[] args) {
Student stu=new Student();
stu.score=59;
change(stu);
System.out.println(stu.score); //90
}
static void change(final Student stu){ //final can be used to modify formal parameters
//stu=null; error trying to change the value of the stu reference itself
//stu=new Student(); Error trying to change the value of the stu reference itself
stu.score=90; //Yes
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560881&siteId=291194637