Abstract class and interface, abstract class Interface

Source: Internet
Author: User
Tags class manager define abstract

Abstract class and interface, abstract class Interface
1 abstract class 1.1 abstract class overview

  • In java, a method without a method body should be defined as an abstract method. If an abstract method exists in a class, this class must be defined as an abstract class.

 

  • Example:
Package java008;/**** * Description: Animal class */public abstract class Animal {/*** Animal eating */public abstract void eat ();}
Package java008;/**** * Description: */public class Dog extends Animal {@ Override public void eat () {System. out. print ("Dog eats meat ");}}
Package java008;/**** * Description: */public class Cat extends Animal {@ Override public void eat () {System. out. print ("Cat eats fish ");}}

 

1.2 characteristics of abstract classes
  • When a method is declared to be unimplemented, the method is an abstract method and needs to be modified in abstract. abstract methods must be defined in abstract classes.
  • Abstract classes cannot be instantiated.
  • An abstract class must have its subclass covering all abstract methods before it can be instantiated. Otherwise, this subclass is still an abstract class.

 

1.3 details of abstract classes
  • Q: Do abstract classes have Constructors?
  • A: Yes. It is used to initialize the sub-class to access the parent class data.

 

  • Q: Can abstract classes not define abstract methods?
  • A: Yes. The purpose is to prevent this class from creating objects, such as the AWT adapter object.

 

  • Q: Can abstract keywords coexist with final?
  • A: No.

 

  • Q: What are the similarities and differences between abstract classes and general classes?
  • A:
    • Similarities: abstract classes and general classes are used to describe things and define members internally.
    • Differences:
      • ① General classes have enough information to describe things. abstract classes may not have enough information to describe things.
      • ② Abstract methods cannot be defined in a general class. abstract classes can define abstract methods or non-abstract methods.
      • ③ General classes can be instantiated, but abstract classes cannot be instantiated.

 

  • Q: Must an abstract class be a parent class?
  • A: Yes.

 

1.4 abstract class exercises
  • Requirement: programmers in the company have names, employee numbers, salaries, and work content. In addition to name, employee ID, salary, bonus, and work content, the Project Manager.
Package java008;/**** * Description: */public abstract class Employee {private String name; private String id; private double pay; public Employee (String name, String id, double pay) {this. name = name; this. id = id; this. pay = pay;} public abstract void work ();}
Package java008;/**** * Description: */public class Programmer extends Employee {public Programmer (String name, String id, double pay) {super (name, id, pay) ;}@ Override public void work () {System. out. print ("write code ");}}
Package java008;/**** * Description: */public class Manager extends Employee {private double bonus; public Manager (String name, String id, double pay, double bonus) {super (name, id, pay); this. bonus = bonus;} @ Override public void work () {System. out. print ("manage ");}}
Package java008;/*** release /9/10 * Description: */public class Test {public static void main (String [] args) {Programmer p = new Programmer ("James ", "30", 9000); p. work (); Manager m = new Manager ("Li Si", "1", 15000,9000); m. work ();}}

 

2. Definition of interface 2.1
  • When methods in an abstract class are all abstract methods, the abstract class can be defined and expressed in another form, that is, the interface.
  • Interface member:
    • Global constant: public static final
    • Abstract method: public abstract

 

2.2 interface implementation
Package java008;/*** * Description: */public interface interfaceDemo {public void show ();}
Package java008;/**** * Description: */public class interfaceDemoImpl implements interfaceDemo {@ Override public void show () {System. out. print ("single implementation of interfaces ");}}
  • Classes and classes are inherited, and classes and interfaces are implemented.
  • The interface cannot be instantiated. This subclass can only be instantiated after the subclass that implements the interface and overwrites all abstract methods in the interface. Otherwise, this subclass can only be an abstract class.

 

2.3 multiple implementations of interfaces
Package java008;/*** * Description: */public interface interfaceDemo {public void show ();}
Package java008;/*** * Description: */public interface InterfaceDemo2 {public void show2 ();}
Package java008;/**** * Description: */public class interfaceDemoImpl implements interfaceDemo, InterfaceDemo2 {@ Override public void show () {System. out. print ("show");} @ Override public void show2 () {System. out. print ("multiple implementations of the interface: show2 ");}}

 

2.4 Interface Details
  • A class can implement multiple interfaces while inheriting another class.
  • The appearance of interfaces avoids but inherits the limitations.
  • Classes and classes are inherited, classes and interfaces are implemented, interfaces are inherited, and more can be inherited.

 

2.5 interface features
  • Interface is the principle of external exposure.
  • An interface is a function extension of a program.
  • Interfaces reduce coupling.
  • The interface can be used for multiple implementations.
  • Classes and interfaces are implementation relationships, and classes can inherit one class and implement multiple interfaces at the same time.
  • The interface and interface can have an inheritance relationship, and can inherit more.

 

2.6 differences between interfaces and abstract classes
  • Member differences:
    • Abstract class: variables, constants, abstract methods, and common methods
    • Interfaces: constants and abstract methods
  • Link differences:
    • Class and Class: inheritance, single inheritance.
    • Class and interface: implementation, single implementation, and multiple implementations.
    • Interfaces and interfaces: inheritance, single inheritance, and multiple inheritance.
  • Differences in design theory:
    • Abstract class: the inherited class represents the "is a" relationship. Common features.
    • Interface: The implementation shows the relationship of "like. Extended functions.

 

2.7 application of interfaces
  • Notebook Simulation

 

  • Define USB Interface
Package java008;/***** * Description: */public interface USB {public void open (); public void close ();}
  • Define USB flash drive
Package java008;/**** * Description: */public class Upan implements USB {@ Override public void open () {System. out. print ("U disk starts running");} @ Override public void close () {System. out. print ("U disk stops running ");}}
  • Define notebook
Package java008;/**** * Description: */public class BookPC {public static void main (String [] args) {useUSB (new Upan ());} public static void useUSB (USB u) {u. open (); u. close ();}}

 

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.