Java abstract classes, interfaces, and inner classes

Source: Internet
Author: User

1. Abstract methods, abstract classes

1) Abstract Method:

    • Modified by abstract
    • Only the definition of the method, no concrete implementation of the method (not even {})

The abstract method is a method of abstraction, and the abstract method is only defined by the method, without the method body implementation, ending with a semicolon. Namely: In the method five elements, the abstract method lacks one element (method body), also may interpret the abstract method as the incomplete method.

2) abstract class:

    • Span style= "font-family:"microsoft yahei"; FONT-SIZE:16PX "> classes that contain abstract methods must be abstract classes, classes that do not contain abstract methods can also declare abstract classes (with little meaning)
    • abstract class cannot be instantiated
    • abstract class is a that needs to be inherited.
        • subclasses need to override all abstract methods of an abstract class---Common
        • subclasses can also be declared as abstract classes---infrequently used
    • Meaning of the abstract class: ul>
      • encapsulates properties and behaviors common to subclasses---code reuse
      • provides a uniform type for all subclasses---up styling
      • The
      • can contain abstract methods that provide a uniform portal for all subclasses, with different implementations of the subclasses, but the portals are consistent

Abstract classes that are modified by the abstract class are not instantiated, and it is meaningless for a class to be instantiated, so you need to define the class to inherit the abstract class, and its subclasses must override all the abstract methods unless the class is also declared as an abstract class.

Abstract class Foo{private double C;public Foo (double c) {  //meaningless, requires quilt class rewrite this.c = C;} public abstract double area ();  Abstract method, no method body, curly brace also does not exist}class sub extends Foo{public sub (double c) {  //Requires overriding construction method super (c);} Public double area () {return 0.0;}} Abstract class Sub2 extends foo{  //Abstraction method inherits abstract method public Sub2 (double c) {  //need to override the constructor method, do not need to override abstract method super (c);}}

  

3) Abstract classes can not be instantiated

Foo f = new foo ();  Compilation error, abstract class cannot be instantiated

Even if there is no abstract method in a class, it can be defined as an abstract class, and again, the class cannot be instantiated.

Note: The abstract and final keywords cannot be decorated with a class at the same time, because final makes the class non-inheritable, and the abstract-decorated class has no meaning if it cannot be inherited.

4) Inheriting abstract classes

After a class inherits an abstract class, it must implement its abstract methods, and different subclasses can have different implementations.

Abstract class Foo{private double C;public Foo (double c) {  //meaningless, requires quilt class rewrite this.c = C;} Public abstract String Sayhi ();} Class Sub extends Foo{public sub (double c) {  //Requires overriding construction method super (c);} Public String Sayhi () {return "Hello";}}

  

4) meaning of abstract class

    • Provide a common type for its subclasses (the parent class refers to the subclass object, which is the upward shape)
    • Encapsulate duplicate content in subclasses (member variables and methods)
    • There are abstract methods defined, although subclasses have different implementations, but the definition of the method is consistent (subclasses need to implement this abstract method)

Case 1: Abstract class presentation

public Class Abstractdemo {public static void main (string[] args) {//shape s = new Shape ();//Compilation error, abstract class cannot be instantiated shape[] Shape = new S  HAPE[4];  Create a shape array object shape[0] = new Circle (1);  SHAPE[1] = new Circle (2), shape[2] = new Square (1), shape[3] = new Square (2); Maxarea (shape); Called to find the maximum area method}private static void Maxarea (shape[] Shape) {Double max = Shape[0].area (); int maxindex = 0;//maximum area subscript for (int i=0 ; i<shape.length;i++) {Double area = Shape[i].area (); if (Area>max) {max = area;maxindex=i;}} System.out.println ("Maximum area:" +max+ "is labeled as:" +maxindex);}} Abstract class shape{//Abstraction Classes---Incomplete class protected double C;//perimeter public abstract double area ();//abstract method---incomplete}class Circle extend S shape{public Circle (double c) {this.c = C;} Public double Area () {//Override abstract method---becomes incomplete for full return 0.0796*c*c;}} Class Square extends Shape{public square (double c) {this.c = C;} Public double area () {return 0.0625*c*c;}}  

  

2. Interface

    • It's a standard, a norm, to be able to do something by following this standard.
    • is a data type (reference type)
    • Defined by interface, can only contain constants and abstract methods, methods are modified by public abstract by default
    • Interface cannot be instantiated
    • Interfaces are implemented by the Implements keyword implementation class: All abstract methods in the interface must be rewritten
    • A class can implement multiple interfaces, comma-delimited, if inherited and implemented, must first inherit after implementation
    • Interfaces can inherit one or more interfaces, comma-delimited (extends)

1) Define the interface

Interfaces can be thought of as special abstract classes. That is, abstract classes that contain only abstract methods and constants. The interface is defined by the interface keyword.

interface demo{public static int x = 100;public int y = 50;double area ();   The default is to add public abstract to modify public abstract void Test ();}

  

2) Implement Interface

Unlike inheritance, a class can implement multiple interfaces, and the implemented interfaces are separated directly by commas, and the class needs to implement all the methods defined in those interfaces. Implements the interface through the Implements keyword. An interface can declare a variable as a type, and a variable of an interface type can refer to an object that implements the class of that interface, through which the method defined in the interface can be called (The implementation class provides the realization of the method). An interface type variable that refers to an object of a subclass, called when a specific implementation of a subclass object is called.

public class Test {public static void main (string[] args) {Demo d = new Aoo ();   An interface type variable that refers to the object of the subclass, when called, is called the specific implementation of the subclass object D.test ();   This is aoo}}interface demo{public the static int x = 100;public int y = 50;double area ();   The default is to add public abstract to modify public abstract void Test ();} Class Aoo implements Demo{public double area () {return 0.0;} public void Test () {System.out.println ("This is Aoo");}}

  

3) Inheritance of interfaces

There can be an inheritance relationship between interfaces, an interface can inherit another interface through the extends keyword, and the subinterface inherits all the methods defined in the parent interface.

Interface foo{public void Funfoo ();} Interface goo{public void Fungoo ();} Interface Hoo extends foo,goo{  //interface can inherit one or more interfaces, comma separated public void Funhoo ();} Interface Ioo{void funioo ();} Class Joo implements hoo,ioo{  //classes can inherit multiple interfaces public void Funfoo () {}//overriding the parent class's  abstract method public void Fungoo () {}  /// Overrides the parent class's abstract method of the parent class public void Funhoo () {}  //Overriding the abstract method of the parent class public void Funioo () {}   //Overriding the abstract method of the parent class}

  

4) The difference between an interface and an abstract class

    • A class can inherit only one abstract class, but may implement multiple interfaces
    • Abstract classes can contain abstract and non-abstract methods, and all methods in an interface are abstract
    • Subclass inheriting an abstract class must implement all abstract methods in the abstract class, or the subclass must be an abstract class. The subclass implementation interface must implement all the abstract methods in the interface.

Case 2: Presentation of the interface

public   Class Interfacedemo {public static void main (string[] args) {//inter6 o1= new Inter6 ();  Compile error, interface cannot be instantiated Inter6 O2 = new Moo (); Up styling Inter5 O3 = new Moo ();}} Demo interface Syntax interface inter1{public static final int NUM = 5;public abstract void Show ();d ouble PI = 3.14;//default public static F  Inal modified void Say (); Default public abstract modifier//int count; Compile errors, constants must be declared at the same time to initialize//void test () {}//Compile error, abstract method without method body}//Demo Interface Implementation Interface inter2{void Show (); void say ();} Class Joo implements Inter2{public void Show () {}public void Say () {}}//Demo interface multiple implementations, inheritance interface inter3{void Show ();} Interface inter4{void say ();} Abstract class Koo{abstract void Test ();} Class Loo extends Koo implements Inter3,inter4{public void Show () {}public void Say () {}void test () {}}//Demo interface inherits interface interface I Nter5{void show ();} Interface Inter6 extends inter5{void say ();} Class Moo implements Inter6{public void Show () {}public void Say () {}}  

  

Java abstract classes, interfaces, and inner classes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.