Java abstract classes, interfaces, inner classes

The concept of abstract classes:

1, Java can define specific method is not a method thereof, a method is also implemented by the subclasses completed, the process is called an abstract method, the method comprising the abstract class is an abstract class;

2. The method of calculating the perimeter and the area of ​​the shape class can not be determined, then it can be a method declared abstract, in order to achieve a particular subclass

Abstract class wherein:

 

Declare an abstract method

Modifier abstract return type method name ([parameter list]);

Note: Because an abstract method can not determine the specific functions performed, so there is no way abstract method body, you need to add a semicolon after the parentheses

Abstract class declaration

Grammar: Modifiers abstract class extends the parent class name class name

Abstract classes and classes in addition to the ordinary and conventional modified outer abstract classes using similar;

An abstract class can be no abstract methods; but once there is an abstract method, then this class must be declared as an abstract class

static final private and can not coexist abstract

 

The use of abstract classes:

Because the abstract class is not a specific class, so it can not be instantiated, but the abstract class can be used to declare variables ;

Abstract classes can be inherited, all the abstract methods abstract class in all subclasses to achieve a particular abstract class;

Note: Because an abstract method can not determine the specific functions performed, all the abstract methods no method body, we need to add in parentheses after the semicolon;

Abstract methods: Method not allowed in the body; subclasses need to override the parent class abstract method, or subclass is also abstract class

Format: public abstract void eat ();

 

Interface: Interface not only in Java application development process, "agreement", but also more abstract abstract class

      Can not be grouped into a same class, but certain features

   Syntax: interface name interface modifier {[constant]; [abstract method];}

    Interface abstract method can not write the abstract keyword, the default public access modifier

    When a class implements an interface, the interface of all abstract methods required to achieve, or to set the class as an abstract class;

    Interface can contain constants, default public static final

   Interface inheritance can also be achieved, and can inherit multiple parent interfaces

      After the new tape may be the default method jdk1.8 method body; override may be implemented in a class, and may be invoked by reference interface: default

   After jdk1.8 new static methods with the method body; not rewritten in the implementation class, calling through the interface name: static

 

Interface syntax: class modifiers extends parent class name for the class name 1 implements the interface, the interface 2, ...} {class body portion

        A class can implement multiple interfaces, so as to solve the shortcomings of Java single inheritance

Inheritance between interfaces: a single inheritance in inheritance relationships between classes in Java, which means that a class has one and only one parent

        The Java interface in multiple inheritance, meaning that a parent can have multiple interfaces Interface

Functions as an interface: improved reusability procedures; improved scalability program; reduce the coupling procedure; implements multiple inheritance

 

Inner classes

What is the inner class? In Java, the class can be defined inside another class or a method which, such a class called internal type

Internal classification categories:

Internal class members: a position of the other members of the class definition in the class;
  obtaining internal class object instance, mode 1: new outer class inner class .new

            Embodiment 2: external object internal category .new

            Embodiment 3: The method of obtaining external object

  1, when used outside the internal class can not be instantiated directly, it needs to be done by means external to instantiate the class information;

  2, access to the internal modifier such, can be any, but the access range will be affected;

  3, inner class can directly access the external members of the class; if there is property of the same name. Access priority class defined in the interior;

  4, a member can use external .this class information from the external access class of the same name;

  5, external access to internal category class information, required by internal class instance can not be accessed directly;

  6, the inner classes compiled .class file naming: external internal class $ class .class;

  7, whether the internal class can contain the same manner as the method signatures outer class

Partial internal classes: a class defined in the method of the body of another class or code block;

  1, defined inside method, also within scope

  2, inner member and with methods of using rules, can not be added in front of class public, private, protected, static

  3, the class can not contain static members

  4, the class can contain final, abstract modified member

  

Static inner classes: one class is defined in the position of the other members of the class, and the static modifier;

  1, static inner classes, static members can directly access the outer class, if you need to call non-static member, can be instantiated by objects

  2, the internal static class object instances, without depending on the external object

  3, via an external class. Inner classes. Static members, access to internal static member class

  4, when the internal class attributes outside the class attribute of the same name, the default calling members of the internal class; If you need to access external class static properties, the external class how properties can; if you need to access external class non-static attribute, new external classes by (). attribute mode

Anonymous inner classes: anonymous inner classes have no name

  1, not anonymous inner classes type name, name of the object instance

  2, compiled files named: external digital class $ .class

  3, can not use the private, public, protected, abstract, static modification

  4, can not write constructor may be configured to add a code block

  5, can not appear static member

  6, anonymous inner classes can implement interfaces can inherit the parent class, but can not have both

 

Guess you like

Origin www.cnblogs.com/scar1et/p/10962005.html