"Java Notes"--abstract classes and interfaces

Source: Internet
Author: User

Now the mainstream of development is object-oriented, the process has been almost eliminated. To understand object-oriented, it is necessary to know its three characteristics, polymorphic inheritance and encapsulation. In the process of learning object-oriented, it always comes into contact with abstract classes and interfaces, which involve inheritance and implementation, and these two concepts are always innocently divided. But after unremitting efforts, or the solution is almost. The following is an introduction to the abstract classes and interfaces:


Abstract class


Concept


Classes that are decorated with the abstract keyword are called abstract classes


Characteristics


1. Not a complete class, only as a base class

2. Cannot be instantiated

3. A class with an abstract method must be declared as an abstract class, and the abstract method must be overridden


Example

<span style= "font-family:kaiti_gb2312;font-size:18px;" >abstract class animal{private String name; Animal (String name) {this.name=name;} /*public void Enjoy () {System.out.println ("barking ..."); */public abstract void enjoy ();} Class Cat extends Animal{private String eyecolor; Cat (String n,string c) {super (n); eyecolor=c;} public void Enjoy () {System.out.println ("cat barking ...");} Class Dog extends Animal{private string Furcolor;dog (String n,string c) {super (n); furcolor=c;} public void Enjoy () {System.out.println ("barking dogs ...");}} Class Lady{private String name;private Animal Pet; Lady (String name,animal pet) {This.name=name;this.pet=pet;} public void Mypetenjoy () {Pet.enjoy ();}} public class Test{public static void Main (String args[]) {cat c=new cat ("CatName", "Blue");D og d=new Dog ("Dogname", "BLACK" ); Lady l1=new Lady ("L1", c); Lady L2=new Lady ("L2", d); L1.mypetenjoy (); L2.mypetenjoy ();}} </span> 


Results



I understand.


I think the abstract class mainly embodies the polymorphism, for example, in the above example, the animal class of the Enjoy () method, if you change the comment out of the two code, the result of the operation is the same, so that this method in the animal class implementation of the same effect is the same, So just declare it for an abstract method, then the class must be declared as an abstract class. When other classes inherit the animal class, not satisfied with the method implementation of the base class, the method of the base class is rewritten to achieve its own different functions, that is, animals will be called, but different animal calls, which is polymorphic.


Interface


Concept

An interface is a collection of definitions of abstract methods and constant values, in essence, an interface is a special abstract class that contains only constants and method definitions, with no variables and method implementations.


Characteristics

1, can be multiple implementation

2. You can inherit other interfaces and add new attributes and abstract methods

3, a number of unrelated classes can implement the same interface, a class can also implement a number of unrelated interfaces


Example

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >interface singer{public void Sing ();p ublic void Sleep (); interface painter{public void Paint ();p ublic void Eat (); Class Student implements Singer{private String name; Student (String name) {this.name=name;} public void Study () {System.out.println ("studying");} Public String GetName () {return name;} public void Sing () {System.out.println ("Student is singing"); public void Sleep () {System.out.println ("Student is Sleeping");}} Class Teacher implements Singer,painter{private string Name;public string getString () {return name;} Teacher (String name) {this.name=name;} public void Teach () {System.out.println ("teaching");} public void Sing () {System.out.println ("Teacher is singing"); public void Sleep () {System.out.println ("Teacher is Sleeping");} public void Paint () {System.out.println ("Teacher is painting");} public void Eat () {System.out.println ("Teacher is Eating");}} public class Testinterface{public static void Main (string[] args) {Singer s1=new StudenT ("le"); s1.sing (); S1.sleep (); Singer s2=new Teacher ("Steven"); S2.sing (); S2.sleep (); Painter p1= (Painter) s2;p1.paint ();p 1.eat ();}} </span>

Results



I understand.


In fact, the same as the abstract class, interface and implementation of the existence of polymorphism between the class, embodied in the implementation of the interface at the same time, but also allow themselves to implement a new method, such as the above example, student implementation of the singer interface, but also realized his own study () method. At that time, I thought that the interface is more encapsulation, define the same specification, implement the implementation of the class according to the specification. Multiple implementations of interfaces are actually multiple inheritance, in Java, a class can inherit only one parent class or abstract class, but it can implement multiple interfaces, which is the difference between abstract classes and interfaces.


Summarize

Polymorphism has three characteristics: to have inheritance, to have overrides, the parent class refers to the object of the subclass, abstract classes and interfaces manifested polymorphism is also reflected in these three aspects. These two are not a pair of contradictory concepts, but each has its own characteristics. The relationship between the two concepts and the implementation classes is different, one is inheritance, and the other is implementation. In the above example, we can also realize that the interface is more flexible than the abstract class, because it is the difference between inheritance and implementation, the realization is more flexible than the inheritance, and the natural interface is more flexible than the abstract class.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Java Notes"--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.