Abstract Classes and Interfaces

Abstract Classes and Interfaces

1. final keyword

Final means final and can be used to modify variables, methods, and classes

1. Modifiers

A variable modified by final, called a constant, whose value cannot be modified

Variables modified by static final are called static constants and are generally referred to as constants

Naming conventions for constants: All letters are capitalized, with underlines connecting multiple words such as USER_STATE

Application: Values commonly used as a substitute for hard/zombie code are mostly difficult to read and understand

2. Modification methods

Method modified by final cannot be overridden

3. Modifiers

A class modified by final cannot be inherited, that is, it cannot have subclasses

All methods in the final class default to the final method and cannot be overridden

4. Modifying parameters

Parameter modified by final, can only be read, cannot be modified

public class Test01_final Keyword {

	static final int b = 6;
	static final int c;
	static final double PI = 3.1415936;

	static {
		c = 2;// Initialize static constants in a static code block
	}

	public static void main(String[] args) {
		final int a = 5;// constant
        //a=8; //Can't be modified
		Dog dog = new Dog();
		dog.sum(2, 3);
	}
}

class Animal {
	public void show() {
		System.out.println("Animal.show()");
	}
    
	// final-modified methods are not allowed to be overridden
	public final void print() {
		System.out.println("Animal.print");
	}
}

//final-modified classes are not allowed to be inherited
final class Dog extends Animal {

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
	}

	public void sum(final int num1, int num2) {
		// num1=111;
		num2 = 222;
		System.out.println("Dog.sum()" + num1 + "," + num2);
	}
}

2. Abstract Classes

How do I prevent my parent from being instantiated?

How do I ensure that subclasses must override the methods of the parent class?

1. Concepts

A class modified by abstract, called an abstract class

  • Definition: public abstract class class name {}
  • Abstract classes cannot be instantiated, that is, they cannot create an object with new, they can only be inherited

A method modified by abstract, called an abstract method

  • Definition: public abstract return value type method name ();
  • An abstract method has only a declaration, no concrete implementation, no method body, and ends with a semicolon

2. Characteristics

Abstract classes may or may not have abstract methods

Classes containing abstract methods must be abstract

After a subclass inherits an abstract class, all abstract methods in the abstract class must be implemented/overridden, otherwise the subclass will remain Abstract

Abstract classes may have constructive methods, not for creating objects, but for initializing member properties

3. Interfaces

If there are only abstract methods and constant values in an abstract class, it can be defined as an interface

1. Concepts

Essentially, an interface is a special abstract class that can only contain declarations of constants and methods

2. Usage

2.1 Define interfaces

Grammar:

interface Interface name{
    constant;
    Abstract method;
}

Attributes in an interface, which are modified by public static final by default, are constants only in the interface

Methods in interfaces, which are decorated by public abstract by default, are abstract methods only in interfaces

Interface cannot be instantiated without a constructor in it

2.2. Implementing interfaces

Grammar:

class Class name extends Parent Class implements Interface name{
    
}

A class that implements an interface, called an implementation class

Implementation classes must implement all abstract methods in an interface, and methods must be decorated with public, otherwise they are abstract classes

3. Characteristics

A class implements multiple interfaces, separated by commas

JDK1.8 and beyond, default keyword modifiers can be used, where methods may have body, but are generally empty (simplify methods that implement classes, do not need to implement all methods)

An interface can inherit multiple other interfaces, add new properties and abstract methods, but an interface cannot inherit classes

Supports multiple inheritance of interfaces and classes in java

/*
 * Relationship between classes and interfaces
 * 1.Between classes
 *   One class can inherit from another: the class subclass extends parent
 * 2.Interface to Interface
 *   An interface can inherit from multiple interfaces: interface subinterface extends parent interface 1, parent interface 2
 * 3.Between classes and interfaces
 *   A class implements multiple interfaces: class implements class implementsInterface 1, interface 2
 */
public class Test04_Inheritance between interfaces {
	public static void main(String[] args) {
		D d = new D();
		d.a();
		d.b();
		d.c();
	}
}

interface A {
	public void a();
}
interface B {
	public void b();
}
interface C extends A, B {
	public void c();
}

class D implements C {
	@Override
	public void a() {
	}
	@Override
	public void b() {
	}
	@Override
	public void c() {
	}
}

4. Role

The function of the interface:

  • Indirect implementation of multiple inheritance (java does not support multiple inheritance but implements multiple interfaces)
  • Define specifications and standards through interfaces
  • Separate the development and implementation of standards

Recommend Interface-oriented programming to improve scalability

public class Test05_The function of the interface {
	public static void main(String[] args) {
		Computer computer = new Computer();
		// Implement polymorphism by referencing an interface to an instance of an implementation class
		Usb usb = new Mouse();// Upward Transition
		Keyboard keyboard = new Keyboard();
		computer.runDevice(keyboard);
	}
}

class Computer {
	public void runDevice(Usb usb) {// Any device that meets the Usb interface can
		usb.power();
	}
}

/*
 * Usb Interface, a standard
 */
interface Usb {
	// With power function
	public void power();
}

class Mouse implements Usb {
	@Override
	public void power() {
		System.out.println("This is the mouse,accord with USB Specification of interface");
	}
}

class Keyboard implements Usb {
	@Override
	public void power() {
		System.out.println("This is the keyboard, match USB Specification of interface");
	}
}

5. Comparison of interfaces and abstract classes

Same:

  • Neither interface nor abstract class can be instantiated
  • Can contain abstract methods

Difference:

  • There are only static constants in an interface, and common member variables in abstract classes
  • There are only abstract methods in an interface, and there can also be non-abstract methods in an abstract class (or there can be non-abstract methods in an interface when using the default keyword)
  • Interfaces do not contain constructors, abstract classes may contain constructors
  • Interfaces support multiple inheritance, while abstract classes do not support multiple inheritance

‚Äč

Tags: Java interface abstract class

Posted by RiBlanced on Tue, 25 May 2021 01:34:25 +0930