Older Dick Silk Self-study note--java 0 basics to rookie--019

Source: Internet
Author: User
Tags define abstract

Abstract classes, interfaces, classes, interface relationships, the differences between abstract classes and interfaces

1. Abstract class

Abstract method: Only method body method, used to define abstract, non-specific functions, such as abstract void Eat ();.

Abstract class: A class that can contain abstract methods (abstract classes do not necessarily contain abstract methods, classes containing abstract methods must be abstract classes), such as abstract class animal{}.

Abstract class Features: Abstract classes can contain either abstract methods or non-abstract methods.

Abstract classes have constructors, but they cannot be instantiated. The constructor is used for subclasses to access the initialization of the parent class data.

The subclass of an abstract class is an abstract class that does not need to override an abstract method.

The subclass of an abstract class is a non-abstract class that must override all abstract methods (instantiation).

1 Abstract classanimal{2      Public Abstract voidsleep ();3 }4 5 classCatextendsanimal{6       Public voidsleep () {7System.out.println ("Kitten Sleeps");8     }9 }Ten  One classfin{ A      Public Static voidMain (string[] args) { -Animal ani=NewCat (); - ani.sleep (); the     } -}

Caveats: Abstract classes conflict with private, final keywords, and the static keyword is meaningless.

2. Interface

Interface: interface, a reference type that defines a method without implementing a method (other classes and interfaces can implement an interface), such as: interface itrain{}.

Interface features: the implementation of the interface with implement keywords, such as: Class Itrainimpl implement itrain{}.

An interface is abstract and cannot be instantiated. instantiated by subclasses.

The subclass of an interface can be an abstract class, but an abstract class implements an interface that is meaningless.

The subclass of an interface is a non-abstract class that must override all abstract methods (instantiation).

The interface can contain only abstract methods and cannot contain non-abstract methods, the default modifier public.

The variables in the interface, which are constants by default.

The interface has no constructors.

Note: The nature of the class implementation interface is: Class extends Object Implement interface {}.

     The object class, which is the root class for all class hierarchies, uses the object class as the superclass and is the method by which all objects (packet-amplified arrays) implement this class.

3, class, interface relationship

Class-To-Class relationships: Inheritance relationships can only be single-inheritance, multiple-tier inheritance.

Interface and interface relationship: implementation of the relationship, can be implemented multiple layers can be implemented.

Class-To-Interface relationships: Inheritance relationships, multiple inheritance, and multiple layers of inheritance.

4. The difference between abstract class and interface

Member Differences: Abstract classes, constants, abstract methods, variables, non-abstract methods, there are constructors.

interfaces, constants, abstract methods, no constructors.

Relationship difference: Abstract class, class can only inherit single

interface, interface can be multiple inheritance/implementation

The concept of Distinction: abstract classes, inherited by the embodiment of the abstract of the generality of the relationship, such as: Cat (Class) is an animal (abstract class).

Interface, is implemented is extended extended function, such as: Cat (Class) will sell Moe (interface).

Older Dick Silk Self-study note--java 0 basics to rookie--019

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.