Java: [object-oriented: abstract class, interface], java object-oriented

Source: Internet
Author: User

Java: [object-oriented: abstract class, interface], java object-oriented

 

Content:

 

  • Abstract class
  • Interface
  • Similarities and differences between abstract classes and interfaces

Release date:

Abstract class:

 

  • Although the parent class already exists, sometimes the parent class cannot directly describe some common attributes. For example, both mammals and humans call, generally, the parent class of the Breastfeeding class does not accurately define the attribute "called". It is obvious that the subclass decides how to "call", but the attribute "called" is common, this attribute can be abstracted (abstraction indicates that there is no specific content) and the subclass can implement the specific content.
  • Abstract classes are defined because abstract methods cannot be defined in common classes.
  • Since the subclass of the abstract method must be overwritten, otherwise it will fail to run, you can ensure that the subclass overrides the abstract method.
  • Abstract classes can have abstract methods or abstract methods (abstract classes without abstract methods). If there are no abstract methods, subclass inheritance does not need to overwrite methods.
  • Abstract classes cannot be instantiated, and objects cannot be created with the new keyword.
  • Only when the subclass covers all abstract methods and the subclass is specific can the subclass create an object. If not all abstract methods are overwritten, The subclass is still an abstract class.
  • Abstract methods must be defined in abstract classes and must be modified in abstract. Abstract METHODS can only be declared and cannot be defined. Therefore, there cannot be braces. Abstract methods cannot be modified using final.
  • Definition Format of abstract classes:
  • Definition Format of abstract methods:

 

 

Abstract class A {abstract void talk (); // It can only be declared and cannot be defined.} class B extends A {void talk () {System. out. println ("run in B"); // The subclass must override the abstract method} public class Demo {public static void main (String [] args) {B B = new B (); B. talk ();}}

 

Interface:

 

  • The interface is also abstract, and the reason for abstraction is similar to that for abstract classes. However, different functions are used. interfaces are generally used to define the Unified Behavior of classes, while abstract classes are "inherited ".
  • Class override all the abstract methods in an interface is called the implementation of the interface.
  • Interface solution: JAVA does not directly support multiple inheritance, but supports multiple implementations.
  • The interface body contains constant definitions and method declarations. methods cannot be defined.
  • An interface can be a fully abstract class.
  • The subclass must overwrite all the abstract methods in the interface before it can be instantiated. Otherwise, it is an abstract class.
  • If a class implements an interface, the class must be defined by the method declared in the mouth. The method name, return type, number of parameters, and parameter type must be consistent with the interface declaration.
  • In the interface, the modifier of the method is public abstract by default; the modifier of the constant in the interface is public static final by default; therefore, the subclass must use public to modify the defined method.
  • Interface Definition Format:
  • Interface implementation:

 

 

Package interface; interface A {public static final int size = 100; public abstract void talk (); // public abstract void eat ();} class B implements A {// implement the abstract method public void talk () {System. out. println ("B talk") ;}} public class Demo {public static void main (String [] args) {new B (). talk ();}}

 

 

 

Note:

 

Similarities and differences between abstract classes and interfaces:
  • Same:
    • Are abstract
    • Cannot be instantiated.
    • Can contain abstract methods. These abstract methods are used to describe the functions of a class, but do not provide specific implementation.
  • Differences:
    • The interface is completely abstract

 

 

  • Conceptual design differences:
    • Abstract classes can have non-abstract methods, while interfaces are completely abstract. In fact, they can be understood as follows: A and B are all abstract classes, but they are "parent classes ", generally, abstract classes have specific attributes. abstract classes are abstract, but they cannot be separated from the relationship between parent classes and child classes. Interfaces define behaviors, it does not have a strong parent-child relationship. It just completely abstracts and defines some behaviors as standards, just like the various standards of components in the factory.
    • The modifiers of variables and methods in the interface show that the interface is an "open and fixed behavior standard"

 

 

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.