16 object oriented 04.2 -- abstract class oriented programming

abstract class

1 Concept

Can be defined in Java abstract Keyword modified method, which has only declaration and no method body, is called abstract method
Classes modified by abstract keyword can be defined in Java. Classes modified by abstract keyword are called abstract classes

If a class contains abstract methods, it must be an abstract class
The method implementation in the abstract class is handed over to the subclass

2 format of abstract method

3 features

  1. abstract can modify methods or classes
  2. The class modified by abstract is called abstract class, and the method modified by abstract is called abstract method
  3. Abstract classes can have no abstract methods
  4. If there are abstract methods in a class, the class must be defined as an abstract class
  5. After the subclass inherits the abstract class, it is either an abstract class or overrides all the abstract methods of the parent class
  6. Mostly used in polymorphism
  7. Abstract classes cannot be instantiated

Exercise 4: introduction case of abstract class

Create package: CN tedu. oop
Create class: abstractdemo java

package cn.tedu.oop;
/*This class is used as an introductory case for abstract testing*/
public class AbstractDemo {
    public static void main(String[] args) {
        /*4.Abstract class cannot be instantiated- create object*/
        //5. Test whether the abstract parent class can create objects
        //Animal a = new Animal();
        //6. Create polymorphic objects for testing
        Animal a = new Pig();
        a.eat();//Call the normal method of the abstract parent class
        a.fly();//Call the abstract method of the abstract parent class

    }
}
//1. Create the parent class Animal
/*2.The class modified by abstract is an abstract class
* If a class contains abstract methods, the class must be declared as an abstract class*/
//4.2 after adding abstract methods, this class needs to be modified with abstract
abstract class Animal{
    //3. Create common methods
    public void eat(){
        System.out.println("You can eat anything~");
    }
    public void play(){
        System.out.println("Play anything~");
    }
    //4.1 creating abstract methods
    /*1.The method modified by abstract is an abstract method, which has no method body*/
    public abstract void fly();
    public abstract void fly2();
}
//2. Create a subclass Pig and establish an inheritance relationship with the Animal class
/*3.When a subclass inherits the abstract parent class, there are two solutions:
* Scheme 1: become an abstract subclass, "lie flat, I won't implement it, continue to abstract"
* Scheme 2: implement all abstract methods in the abstract parent class, "parent debt and child repayment"*/
//4.3 subclasses need to be processed. Continue to abstract / implement all abstract methods of the parent class
//abstract class Pig extends Animal{-- scheme I
class Pig extends Animal{//Scheme II
    @Override//This is a rewritten method
    public void fly() {
        System.out.println("I finally paid off my father's debt and my pig finally flew up~");
    }

    @Override
    public void fly2() {
        System.out.println("All abstract methods in the abstract parent class need to be implemented");
    }
}




Exercise 5: abstract class constructor test

Constructors in abstract classes are usually used when subclass objects are instantiated

Create package: CN tedu. oop
Create class: abstractdemo2 java

package cn.tedu.oop;
/*This class is used as an abstract class constructor test*/
/*Does the abstract class have a constructor? have
* Since abstract classes cannot be instantiated, why should there be construction methods?
* Not for your own use, but to use super() when creating objects for subclasses; */
public class AbstractDemo2 {
    public static void main(String[] args) {
        //4. Test whether abstract classes can create objects? may not!!!
        //Animal2 a = new Animal2();
        //5. Create subclass objects for testing
        Pig2 p = new Pig2();
    }
}
//1. Create an abstract parent class Animal2
abstract class Animal2{
    //3. Create construction method
    public Animal2(){
        System.out.println("I am Animal2 Construction method of~");
    }
}
//2. Create subclass Pig2
class Pig2 extends Animal2{
    //6. Create parameterless construction of subclasses
    public Pig2(){
        super();//Represents a parameterless construct that calls the parent class
        System.out.println("I am Pig2 Construction method of~");
    }
}

Exercise 6: abstract class member test

Create package: CN tedu. oop
Create: abstract3 java

package cn.tedu.oop;
/*This class is used as a member test in an abstract class*/
public class AbstractDemo3 {
}
//1. Create abstract parent class Fruit
abstract class Fruit{
    /*1.Can member variables be defined in abstract classes-- sure!!!*/
    //3.1 defining member variables in abstract parent classes
    int sum = 100;
    /*2.Can member constants be defined in abstract classes-- sure!!!*/
    //3.2 defining member constants in abstract parent classes
    final String name = "XIAOHUANGREN";
    /*3.Can ordinary methods be defined in abstract classes-- sure!!!
    * Can all abstract classes be ordinary methods-- it's fine too!!!*/
    /*4.If there are ordinary methods in a class, why should it be modified into an abstract class?
    * Because abstract classes cannot be instantiated, if you don't want the outside world to create objects of this class
    * You can declare an ordinary class as an abstract class*/
    //4. Common methods for defining Abstract parent classes
    public void clean(){
        System.out.println("The fruit should be washed before eating~");
    }
    /*5.Can abstract methods be defined in abstract classes-- sure!!!*/
    /*6.If an abstract method is added to a class, the class must be declared as an abstract class*/
    //5. Define the abstract method in the abstract parent class
    public abstract void grow();
    public abstract void clean2();
}
//2. Create subclass Banana
/*If a subclass inherits the abstract parent class, there are two solutions:
* Scheme 1: continue to abstract, that is, as an abstract subclass, there is no need to implement the abstract method - "lying flat"
* Scheme 2: no longer abstract, implement all unrealized abstract methods inherited from the parent class - "parent debt and child repayment"*/
class Banana extends Fruit{
    @Override
    public void grow() {
        System.out.println("A bunch of bananas is old and heavy~");
    }
    @Override
    public void clean2() {
        System.out.println("Bananas don't need washing. Bananas like to be peeled");
    }
}

expand

Summary: abstract considerations

Abstract methods must be overridden after subclass inheritance.
So, what keywords can't abstract keyword be used with? The following keywords, in an abstract class. It can be used, but it's meaningless.
1.private: after being privatized, subclasses cannot be rewritten, which is contrary to abstract.
2.static: static takes precedence over object, and there is a problem of loading order.
3.final: it cannot be rewritten after being modified by final, which is contrary to abstract.

Programming: analysis teacher example - Abstract oriented programming

Specific things: Peiyou class teacher and expert class teacher
Commonality: Lecture Preparation

Create package: CN tedu. design
Create class: designteacher java

package cn.tedu.design;
/**This class is used to design teacher class, which is oriented to abstract programming*/
public class DesignTeacher {
	public static void main(String[] args) {
		CGBTeacher ct = new CGBTeacher();
		SCDTeacher st = new SCDTeacher();
		ct.ready();
		ct.teach();
		st.ready();
		st.teach();
	}

}
//Things in life -- classes
//Feature attribute behavior method

//Attribute: name and job number
//Method: preparing lessons
abstract class Teacher{
	int id ;//Job number
	String name;//full name
	//Lesson preparation method
	public abstract void ready();
	//Lecture method
	public abstract void teach();
}

/**CGB teacher of Peiyou class -- focusing on e-commerce projects*/
class CGBTeacher extends Teacher{
	@Override
	public void ready() {
		System.out.println("Preparing lessons...E-commerce project....");
	}
	@Override
	public void teach() {
		System.out.println("Teaching...E-commerce project....");
	}
}
/**SCD Big data teacher -- focusing on Scala language*/
class SCDTeacher extends Teacher{
	@Override
	public void ready() {
		System.out.println("Preparing lessons...hadoop");
	}
	@Override
	public void teach() {
		System.out.println("Giving a lecture...spark");
	}
}

Keywords: Java JavaEE

Added by vh3r on Tue, 01 Feb 2022 12:33:19 +0200