Interfaces, Abstract Classes and Inner Classes

---Restore content begins---

abstract class

Detailed grammar

When a class has an abstract method, it must be an abstract class

An abstract class cannot create an object with new

Abstract classes can contain abstract methods, but are not required to

If a class inherits from an abstract class, it must implement the abstract methods in the parent class, or declare itself as an abstract class.

When a class declares to implement one or some interfaces, but does not implement all its abstract methods, then the class must also be declared as abstract class

Abstract methods in interfaces must be modified with public, and abstract methods in abstract classes can be modified with modifiers other than private.

Presumably an interface, an abstract class can contain properties and methods, an interface can only contain abstract methods

The role of abstract classes

If you want the methods in the parent class to be overridden by the subclass, you need to use an abstract class

inner class

  Classes can exist inside classes and methods, and they are collectively referred to as inner classes.

  Inner class within a class - member inner class

  Inner class in method - local inner class (similar to member variable local variable)

  The syntax rules of inner classes and ordinary classes are similar, and inheritance, interfaces can also be used

  Non-static inner classes cannot be used in static methods

  Use of inner classes

  The purpose of introducing inner classes in java is to assist outer classes, so in most cases inner classes are used inside outer classes, and an inner class is rarely used outside of outer classes.

  When importing an inner class, import the outer class. Inner class

  (*) Use inner class outside of outer class: 1. Non-static inner class new + outer class reference. new + inner class class name (constructor parameter) 2. Static inner class, direct new inner class

  The inner class can access all members of the outer class, no matter what his modifiers are

  anonymous inner class

  1. Anonymous inner class has no name

  2. Anonymous inner class has no constructor

  3. Anonymous inner classes cannot display inheritance or implementation interfaces

  4. Anonymous inner classes have no modifiers

  Syntax of anonymous inner class

  new interface name (abstract class name). As mentioned before, interfaces and abstract classes cannot be instantiated. The new keyword is equivalent to creating an instance of an anonymous class that implements the interface (inherits the abstract class).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325301030&siteId=291194637