Java learning (interface polymorphism)

Posted by ambo on Tue, 28 Dec 2021 13:48:33 +0100

Interface

  1. The interface is the skeleton of the project.

    An interface is a standard and a specification. Common attributes (constants) and methods can be defined in the interface.

  2. Purpose of interface:

    1. Interface polymorphism

    2. Storage constants

    3. Implemented by other classes

3. Features:

  • The interface cannot create an object, and there is no constructor in the interface.

    Reason: the interface definition is a specification, so there can be no constructor and initialization block.

  • Methods in the interface: public abstract / / abstract methods

  • Interface properties: public static final, which are constants.

4. Interface and interface relationship:

Use the keyword extends to implement the inheritance relationship between interfaces.

Multiple inheritance is supported.

5. Whether the interface inherits from Java lang.Object?

No.

Interface is a standard and a specification.

If you inherit, the implementation methods in the Object will be in the interface, but there are only abstract methods in the interface.

final keyword

  1. final modifier basic type variable:

    • A basic type variable decorated with final can only be assigned once.

    • final modifies class variables, which must be assigned at the time of static code block or declaration.

    • final modifies an instance variable. You must specify the initial value in a non static code block, constructor, or when declaring the instance variable.

    • final modifies a local variable that can be assigned in a method or code block.

  2. final modifies variables of reference type:

    • The object pointed to by the reference variable cannot be changed, but the content of the object can be changed.

  3. final method: cannot be overridden.

  4. final class: cannot be inherited.

abstract keyword

Abstract class:

  1. Concept: a class modified by abstract is called an abstract class.

  2. Purpose of existence: to be inherited.

  3. Must a class containing abstract methods be an abstract class?

    A class containing abstract methods must be an abstract class, but this should not be used as the definition of an abstract class.

Abstract method:

  1. Concept: methods modified by abstract are called abstract methods.

  2. Purpose of existence: to be rewritten. Only at the design architecture level.

  3. be careful:

    1. Cannot contain method body. (it is only abstract and not implemented concretely. It is implemented by subclass rewriting.)

    2. Exists in abstract classes, interfaces.

Summary:

  1. Abstract methods must be written in abstract classes, but there are not necessarily abstract methods in abstract classes.

  2. Abstract methods cannot create instances, and their construction methods are mainly used in subclasses. (that is, the purpose of existence: overridden by subclasses)

  3. The non Abstract subclass of an abstract class must implement all the abstract methods in the abstract class.

Example 1: the example of zoo keepers in the previous article is rewritten

public interface Animal {
	public void eat();

}

The interface is Animal

public class Cat implements Animal {

	public void eat() {
		// TODO Auto-generated method stub
		System.out.println("I  like fish ");
	}

}

public class Dog implements Animal{
	public void eat(){
		System.out.println("I like eating gutou");
	}

}
public class People {
	public void feed(Animal a){
		System.out.println("kaishi  ganfan!!!!");
		a.eat();
		
	}

}
public class Test {public static void main(String[] args) {
	People p =new People();
	
	Animal d=new Dog();
	Animal c = new Cat();
	
	p.feed(c);
	p.feed(d);
	//d.eat();
	//c.eat();
}

}

Example 2: in the previous article, the example of printer school teachers inherited polymorphism was replaced by interface implementation polymorphism

public interface Detail {
	//String detail();

	

	public String detail();

}
public class Teacher implements Detail {

	public String detail() {
		// TODO Auto-generated method stub
		return "I am a teacher;";
	}

}
public class Collage implements Detail {
    private Printer printer;
    
    public void setSet(Printer printer) {
        this.printer = printer;
    }
    public String detail() {
        // TODO Auto-generated method stub
        return "I am a collage;";
    }

public void printContent(Detail d){
    printer.print(d.detail());
}

}

public interface Printer {
	public void print(String content);

}

public class BlackPrinter implements Printer {

	public void print(String content) {
		// TODO Auto-generated method stub
		System.out.println("start black and white printing ..." + content);

	}

}
public class ColorPrinter implements Printer {

	public void print(String content) {
		// TODO Auto-generated method stub
		System.out.println("start color printting ..." + content);

	}

}
public class Test {public static void main(String[] args) {
	Collage c = new Collage();
	
	Printer printer = new ColorPrinter();
	c.setSet(printer);
	
	c.printContent(c);
	
	System.out.println("---------------");
	
	Teacher t = new Teacher();
	
	c.printContent(t);
	
	System.out.println("===============");
	
	c.setSet(new BlackPrinter());
			
	c.printContent(c);
	
	System.out.println("---------------");
	
	//Teacher t = new Teacher();
	
	c.printContent(t);
	
}

}

Topics: Java