Use of Java object-oriented abstract methods, abstract classes and interfaces

Keywords: Java C# Programming OOP mvc

1, Abstract

1. Brief description

  • It's just a statement, not a realization
  • Abstract method: public abstract void act();, There is no method body and it ends directly. Abstract methods can only exist in abstract classes.
  • Abstract class: public abstract class Test {}. The subclass inheriting the abstract class must override the methods (including abstract methods) in the parent class, otherwise the subclass must also be an abstract class.

2. Precautions
(1) It can be used to force which methods must be in the subclass to be evaluated, because the methods of the abstract parent class must be overridden.
(2) Abstract classes cannot instantiate objects of other classes.
(3) However, there can be normal methods in abstract classes, not necessarily abstract methods.

3. Test exercise

  • An error is reported when instantiating an object in an abstract class:
  • No error will be reported when instantiating an object in a common subclass that inherits an abstract class:
abstract class Test06_Father{  //Define an abstract class 
	//final constant
	final int height = 180;
	
	//Define abstract methods in abstract classes (abstract methods can only exist in abstract classes)
	public abstract void act0();
	//Define common methods in abstract classes
	public void act1(){}
	public void act2(){}
	
	//Instantiate an object in an abstract class: an error will be reported
	// Test06_Father tf = new Test06_Father();
}

public class Test06_Son extends Test06_Father{  //Subclasses inherit an abstract class
	//The parent class method that must be overridden in a subclass that inherits the abstract class
	public void act0(){
		System.out.println("Rewrite the abstract method for the zero time");
	}
	public void act1(){
		System.out.println("Rewrite the normal method for the first time");
	}
	public void act2(){
		System.out.println("Rewrite the normal method for the second time");
	}

	//Define normally executed methods in subclasses that inherit abstract classes
	public void speeking(){
		System.out.println("I won't change my mind!!!");
	}

	//Define the main method in a subclass
	public static void main(String[] args){
		//Redefining variables can
		int height = 99;  
		
		//Instantiate an object in an ordinary subclass that inherits an abstract class: no error will be reported
		Test06_Son ts = new Test06_Son();  
		ts.act0();
		ts.act1();
		ts.act2();
		ts.speeking();  //Calling a method in an object
	}
}

2, Interface

1. Brief description

  • Interface is actually a special abstract class, in which all contents are exposed.
  • All methods in the interface are abstract methods (ordinary methods are not allowed), which is equivalent to occupying a bit with abstract methods.
  • Use interface to declare an interface. Abstract can not be used when defining abstract methods in the interface.

2. Precautions
(1) The properties defined in the interface must be public static final, while the methods defined in the interface must be public
Must be public abstract, so these modifiers can be partially or completely omitted;

(2) The value of the attribute defined in the interface cannot be changed in the implementation class;

(3) A class can only implement an interface and cannot inherit an interface; But interfaces can inherit interfaces
Interfaces can inherit not only interfaces, but also multiple interfaces, that is, interfaces allow multiple inheritance;

(4) If a class overrides (implements) only part of the methods of an interface, the class must be declared as an abstract class, otherwise it must override (implement) all the abstract methods in the inherited interface;

(5) A class can implement one or more interfaces while inheriting a parent class, but the extensions keyword must precede the implementations;

(6) new interface objects are not allowed, but variables of interface reference type can be defined and pointed to
Implement the object of the interface to achieve the purpose of polymorphism.

3. Test exercise

interface Test06_Valuable{  //Define an interface (special class)
	//Defining abstract methods in interfaces: no need to use abstract
	void getMoney();
}
class Test06_Gold implements Test06_Valuable{  //An interface that inherits (implements) an interface
	//In the interface that inherits the interface, you must override (implement) the abstract method of the parent class (add a public to improve access rights)
	public void getMoney(){
		System.out.println("Gold is always valuable!");
	}
}
public class Test06_Son{  //Defines the class of the main method
	public static void main(String[] args){
		Test06_Gold go = new Test06_Gold();  //Instantiate sub interface object
		go.getMoney();  //Calling methods in sub interface objects
		
		//Interfaces are also polymorphic
		Test06_Valuable tv = new Test06_Gold();  //Assignment up
		tv.getMoney(); 
	}
}

Posted by kylebragger on Fri, 10 Sep 2021 21:04:01 -0700