9, Java abstract class

Posted by Galia on Fri, 05 Nov 2021 03:04:03 +0100

Concept:

Class: a custom composite data type used to describe a group of things with common properties

Class: class keyword creation

Public class Hello {} -- > class

Abstract class:    The java class modified by abstract is.

Public abstract class Hello {} -- > abstract class

1) Why abstract classes?

For example:

         Charging by car, taxi has charging function [1 yuan / km],

         The bus has the function of charging [1 yuan for the whole journey].

Taxi / bus -- > car -- > charging function

Taxi extends car - > rewrite charging function  = [1 yuan / km]

Bus extensions car -- > rewrite charging function  = [1 yuan for the whole process]

        We can understand that the car is an abstract class. Taxis / buses are subclasses of cars. Taxis / buses will inherit the charging function from the car, and then rewrite the charging function according to their actual situation to get their own charging function implementation.

Subclasses of abstract classes are often of the same type.

Abstract classes actually provide the public content of the same type of things. The subclasses of abstract classes implement the public content provided by this abstract class according to their own actual situation. In this way, subclasses do not need to create this public content, but only need to inherit to rewrite it.

Abstract part       If the subclass is not an abstract class, the override is mandatory and must be overridden

2) What elements can there be in an abstract class?

       Local variables cannot be accessed in other methods (not considered)

Class:   Instance variable, static member variable, construction method, instance method, static method

Abstract class:   Instance variable, static member variable, constructor, instance method, static method, [abstract class] (optional).

misunderstanding  : Judging whether a class is an abstract class does not depend on whether there are abstract methods. It depends on whether the current class has abstract   modification

Basic format of method:

Modifier   Return value   Name ([parameter]) {method body}

Instance method:

Modifier   Return value   Name ([parameter]) {method body}

Static method:

Modifier static   Return value   Name ([parameter]) {method body}

Construction method:

Modifier     Class name ([parameter]) {method body}

Abstract method:

Modifier abstract return value   Name ([parameter])

Abstract method: a method without method body modified with abstract

example:

public abstract class AbstractTest {
	public static String name  = "Zhang San";
	public int age = 16;
	public AbstractTest(){
		System.out.println("Construction method of abstract class");
	}
	
	public void methods(){
		System.out.println("Instance method of abstract class");
	}
	
	public static void staticMethods(){
		System.out.println("Static methods of abstract classes");
	}
	//Abstract method
	public abstract void fn();
}

3) Concrete usage of abstract classes

(1) . the abstract class cannot be new, and the instance elements in the abstract class can be accessed with the help of subclasses.

(2) If an ordinary java class inherits an abstract class, you need to rewrite all the abstract methods in the abstract class, otherwise change the ordinary java class into an abstract class.

(3) . abstract classes can inherit other abstract classes without overriding abstract methods.

(4) . create an abstract class object by using the upper transformation object

(5) . abstract class objects can access instance variables, class variables, construction methods, instance methods, class methods, and [abstract methods are actually methods after subclass rewriting].

(6) When the method parameter in an ordinary java class is an abstract class type, it can be passed as an upper transformation object or a subclass object of an abstract class.

Example (2) (3):

① Ordinary classes inherit abstract classes
class Fdr extends AbstractTest1{

	@Override
	public void fn() {
		// This method must be implemented
	}
}
② Abstract classes inherit abstract classes
Class abstract Fdr extends AbstractTest1{
    // This method may not be implemented
}

//abstract class
 abstract class AbstractTest1 {
	public static String name  = "Zhang San";
	public int age = 16;
	public AbstractTest1(){
		System.out.println("Construction method of abstract class");
	}
	
	public void methods(){
		System.out.println("Instance method of abstract class");
	}
	
	public static void staticMethods(){
		System.out.println("Static methods of abstract classes");
	}
	//Abstract method
	public abstract void fn();
}

Example (4)

Using the example above:

AbstractTest1  abt  =  new Fdr();

abstract class     variable   =  new   Subclass ();

This subclass must inherit the declared abstract class, and the subclass cannot be new   Subclasses of cannot be abstract classes

Example (5) (6)

public class MainTest1 {
	public static void main(String[] args) {
		AbstractClass b = new B();
		b.sayHi();
		((B) b).methods(); //Cast to subclass, otherwise an error will be reported
	}
}

//abstract class
abstract class AbstractClass {
	public String name = "Li Si";
	//Abstract method
	public abstract void sayHi();
}
//Class B inherits abstract classes
class B extends AbstractClass{
	
	@Override
	// Override the abstract method in the AbstractClass class
	public void sayHi() {
		System.out.println("How do you do" + super.name);
	}
	public void methods(){
		System.out.println("I am an example method");
	}
}

4) Upper transformation object

1. Upper transformation object: the object of the subclass is assigned to the variable of the parent class. At this time, the subclass object will be transformed upward into the parent object

Format:  

Parent class parent class variable name = new subclass ();

2. The upper transformation object cannot access the variables and methods of the subclass itself. If we need to access the variables and methods of the subclass itself, we need to force type transformation

Format:

Subclass subclass variable name  = (subclass) parent class variable name;

Format:
Subclass subclass variable name  = (Subclass) parent class variable name;

Abstract class example resolution:
Examples of selling fruit↓
1.There needs to be a fruit field to hold fruit
2.Need a way to buy fruit
3.There are different kinds of fruits
4.Fruits have different names
5.Fruit has color

Abstract classes should: 
Name of fruit,Color, (whether it can be sold), abstract method: production method
 Apple class: method of implementing parent class
 Banana class: method to implement parent class

Store category:
Attribute: store name
 Method: enter the fruit: go to the corresponding fruit field in the store and enter the corresponding fruit

public class BuyFruits {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//	Ten catties of red apples were put in
		//  Ten catties of white apples were put in
		// 	20 jin of yellow bananas
		Fruites f1 = new Ipone("Apple", 'red', true); 
		Fruites f2 = new Ipone("Apple", 'white', true);
		
		Fruites f3 = new Banana("Banana", 'yellow', true);
		
		ShopFruites xiaohong = new ShopFruites(); //Create a Duoduo fruit store
		
		//Sell fruit for sale
		xiaohong.buyFruits(f1);
		xiaohong.buyFruits(f2);
		xiaohong.buyFruits(f3);
	}

}
/*
 * 
 * Store class
 * */
class ShopFruites{
	public String name ="Duoduo fruit store";
	public void buyFruits(Fruites fruites){
		//Can I buy it
		if (fruites.isBuy) {
			switch (fruites.name) {
			case "Apple":
			if (fruites.isBuy) {
				//The default apple is only 10 jin
				int f = fruites.productNmubs(10);
				System.out.println("Altogether"+f+"Jin");
			}
				break;
			case "Banana":
				if (fruites.isBuy) {
					//The default banana is only 20 kg
					int f = fruites.productNmubs(20);
					System.out.println("Altogether"+f+"Jin");
				}
			default:
				break;
			}
		}
		
	}
}


/**
 * Fruits
 * */
abstract class Fruites{
	public String name;
	public char color;
	public boolean isBuy;
	public Fruites(String name,char color,boolean isBuy) {
		// TODO Auto-generated constructor stub
		this.name = name;
		this.color = color;
		this.isBuy = isBuy;
	}
	public abstract int productNmubs(int i);
}
//Apple fields
class Ipone extends Fruites{
	public Ipone(String name, char color, Boolean isBuy) {
		super(name, color, isBuy);
		// TODO Auto-generated constructor stub
	}
	@Override
	public int productNmubs(int nums){
		if (isBuy) {
			return nums;
		}
		return 0;
	}
}
//Banana production land
class Banana extends Fruites{
	public Banana(String name, char color, Boolean isBuy) {
		super(name, color, isBuy);
		// TODO Auto-generated constructor stub
	}
	@Override
	public int productNmubs(int nums){
		if (isBuy) {
			return nums;
		}
		return 0;
	}
}

Helplessness comes from not being strong enough

 

Topics: Java Back-end JavaSE