Java Basics-Learning notes (12)--abstract classes and interfaces

Source: Internet
Author: User

1. Definition of abstract class

In Java, you can define some methods that do not contain any statements, and the method body implementation can be implemented by subclasses, which is called abstract methods, as long as there is an abstract method in the class, this class is called abstract class. Classes that contain abstract methods are abstract classes, and all methods in an abstract class are not necessarily abstract methods.

2. Rules for abstract classes

1) Both abstract and abstract methods need to be decorated with the abstract keyword

2) Abstract classes cannot be instantiated, that is, objects of abstract classes cannot be created with new

3) Abstract method only need to declare, do not write statements

4) Subclasses of inherited abstract classes can only be instantiated if all the abstract methods that inherit from the parent class are overwritten, or the subclass is abstract

3. How to do abstract methods

Abstract method Name (parameter list) for return value type

1 Abstract class Person2 {3 String name;4     Private intAge ;5      Public voidGetage (intAge )6     {7          This. age=Age ;8     }9     Abstract voidGetInfo () {}Ten } One classStudentextends Person A { - String School; -      Public voidGetInfo ()//overwrite GetInfo () in the parent class the     { -System.out.println ("name=" +name+ "" + "age=" +age+ "" + "school=" +school); -     } -      Public voidStudy () +     { -System.out.println ("I ' m studing"); +     } A}Abstract class Person at { - String name; -     Private intAge ; -      Public voidGetage (intAge ) -     { -          This. age=Age ; in     } -     Abstract voidGetInfo () {} to } + classStudentextends Person - { the String School; *      Public voidGetInfo ()//overwrite GetInfo () in the parent class $     {Panax NotoginsengSystem.out.println ("name=" +name+ "" + "age=" +age+ "" + "school=" +school); -     } the      Public voidStudy () +     { ASystem.out.println ("I ' m studing"); the     } +}
View Code

4. Interface

All methods in an abstract class are abstract methods, so we can define this class in a different way. An interface is a collection of definitions of abstract methods and constant values, in essence, an interface is a special kind of abstract class that can only be defined by constants and methods, without the implementation of variables and methods.

Abstract class Person
{
public static FINA String name;
public static FINA Int. INT age;
abstract void GetInfo () {}
}

This person class can be defined as an interface because it is a set of constants (both global static variables) and abstract methods, so abstract can be omitted, the public static final of the constant can be omitted, and the default is modified with public, So you can also omit public. As follows:

public interface Person

{
String name;
int age;
 void GetInfo ();
}

5. Implement the interface

A new interface can be defined to inherit an existing interface, which is also required to be implemented with the extends keyword. You can also define a class that implements all the methods in the interface and requires the Implements keyword to implement

1 InterfaceRunner2 {3     intId=3;4     voidrun ();5 }6 InterfaceAnimalextendsRunner7 {8     voidBreathe ();9 }Ten classFishImplementsAnimal One { A     voidRun () -     { -System.out.println ("I can swim!"); the     } -     /*Public Void Breathe () -     { - System.out.println ("I can breathe!"); +     }*/ - } + /* A F:\java_example\lesson5>javac Test.java at test.java:10: Error: Fish is not abstract and does not overwrite abstract methods in animal breathe () - class Fish implements Animal - ^ - test.java:12: Error: Run () in fish cannot implement run () in runner - void Run () -              ^ in attempting to assign lower access rights; previously public - 2 Errors to */

The above code is wrong, first: is not implemented in the interface animal all classes; second, because we are going to override the method, the access modifier level in the fish class should be higher or equal to the access modifier of the method in the interface

A class can implement one or more interfaces while inheriting a parent class, but the extends keyword must precede the Implements keyword as follows

 1  class  Student extends  person implements   Runner  2  {   ...  4  public  void   run ()  5   { 6  System.out.println ("I can run!"  7   8   ...  9 } 

Since the interface is a static-modified constant, it can be referenced directly by the class name. The class used to implement the interface can also use the class to directly refer to the constants in the implemented interface, or you can create new objects to reference

1 InterfaceRunner2 {3     intId=3;4     voidrun ();5 }6 InterfaceAnimalextendsRunner7 {8     voidBreathe ();9 }Ten classFishImplementsAnimal One { A      Public voidRun () -     { -System.out.println ("I can swim!"); the     } -      Public voidBreathe () -     { -System.out.println ("I can breathe!"); +     } - } + InterfaceFlyer A { at     voidFly (); - } - classBirdImplementsRunner,flyer - { -      Public voidRun () -     { inSystem.out.println ("I can run!"); -     } to      Public voidFly () +     { -System.out.println ("The Bird is flying!"); the     } *      Public Static voidmain (String [] args) $     {Panax NotoginsengBird birds=NewBird (); -System.out.println ("The runner ' s ID is" +runner.id); the birds. Fly (); +System.out.println ("The runner ' s ID is" +bird.id); ASystem.out.println ("The runner ' s ID is" +birds.id); the     } + } - /* $ F:\java_example\lesson5>java Bird $ The runner ' s ID is 3 - The bird is flying! - The runner ' s ID is 3 the The runner ' s ID is 3 - */

6. Interface Features:

Implementing an interface is to implement all the methods in the interface (except abstract classes);

The methods in the interface are abstract;

Multiple unrelated classes can implement the same interface, and a class can implement multiple unrelated interfaces.

7, the purpose of the interface design:

In Java, the purpose of designing an interface is to allow classes to not be constrained by a single inheritance, but to have the flexibility to inherit some common features to achieve multiple inheritance, and to avoid the problems caused by complex relationships of multiple inheritance in C + +. The danger of multiple inheritance is that a class may inherit different implementations in the same method, which is not the case for interfaces because the interface is not implemented.

Java Basics-Learning notes (12)--abstract classes and interfaces

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.