Java Fundamentals 2: Abstract classes, interfaces, and polymorphism

Source: Internet
Author: User
Tags define abstract



On the Java Foundation of the article, I think it can also be written in my other blog, is definitely original, and now share to everyone out.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ---------



Abstract class, interface, polymorphism are object-oriented objects are very basic things, I believe see can this blog people will not be entangled in its basic definition, this article will try to dig deeper into its connotation, hope to be helpful to everyone.

First, abstract class

1. Form

Abstract class Ceshi   {      abstract void Show ();  }  Class Demoa extends Ceshi  {      void Show ()     {          System.out.print ("Hello World");}     }


2. Features

A, abstract class can not be instantiated

b, the abstract class must have its subclasses covered by all abstract methods, the subclass can be instantiated, otherwise, the subclass is an abstract class.

C, the abstract method must be defined in an abstract class, and the class must also be abstract.

3. Then the question comes.

A, abstract class has a constructor function?

There, used to instantiate subclasses.

B. Can abstract classes not define abstract methods?

It's actually possible, but it's rare.

C, abstract class and general class similarities and differences.
Same point:
Both abstract and generic classes are used to describe things, and members are set internally.
Different points:
1, the general class has enough information to describe things. Abstract classes may not have enough information to describe things.
2, abstract methods cannot be defined in general classes, only non-abstract methods can be determined. Abstract methods can be defined in abstract classes, and non-abstract methods can also be defined.
3, the generic class can be instantiated. Abstract classes cannot be instantiated.

Second, the interface

In a sense, an interface is a special form of abstract class, in the Java language the abstract class represents an inheritance relationship, a class can inherit only one abstract class, and a class may implement multiple interfaces.

We all know how the interface is defined

Interface demo{   void Show ();      


In fact, for the common members of the interface: and these members have fixed modifiers.
A, global constants: public static final

B, abstract method: Public abstract

1, it concludes that the members of the interface are public permissions. That is, the full write is

Interface demo{public    static final int num = 4;    public abstract void Showa ();}


2. Interface Features

A, interface is the rule of external exposure, is the extension of the function of the program.

b, the interface reduces the coupling.

C, a class can implement a plurality of interfaces

Abstract class Test2 extends Q implements a,z{}


3. Interface and abstract class similarities and differences

Same point:
are constantly upward drawn.

Different points:

1, abstract classes need to be inherited, and can only be inherited. interfaces need to be implemented and can be implemented more.

2. Abstract methods and non-abstract methods can be defined in an abstract class, and after the subclass inherits, non-abstract methods can be used directly. Only abstract methods can be defined in an interface and must be implemented by subclasses.

Three, polymorphic

An object has a different shape, which is polymorphic.

such as animal ANI = new Dog ();

This is the embodiment of polymorphism in code: The parent class reference points to the subclass object.

1. Multi-state benefits and drawbacks

Polymorphism Benefits: Improved code extensibility, pre-defined code can use later content.

The disadvantage of polymorphism: pre-defined content cannot use (invoke) the content of later subclasses.

2. Downward transformation

Animal ANI = new Cat (); Cat C = (cat) ANI;


3. Inner class

The inner class is the less we built before, in fact there are classes inside the class.

Class outer//external class {Class    inner//inner class.    {    }   }

A, how does an inner class reference an external class?

The inner class holds a reference to the external class. External class name. this.

class outer{    int num = 3;    Class Inner    {        int num = 4;        void Show ()        {            int num = 5;            System.out.println (Outer.this.num);        }    }    void method ()    {        new Inner (). Show ();    


3, the inner class can be stored in the local location, and in the local location can only access the local is the final modified local variables.

Class outer{    void Method ()    {        final int x = 9;        Class Inner        {public            String toString ()            {            }        }    


4. Anonymous inner class

An anonymous inner class is a shorthand form of an inner class, which is actually an anonymous child object.

    public void Method ()    {        new Demo ()//anonymous inner class.        {            void Show ()            {                System.out.println ("Show:" +num);            }        }. Show ();    


Iv. final

Abstract classes, interfaces, and polymorphism are simple, but commonly used, especially like inner classes, anonymous inner classes, which we have less contact with before, should focus on mastering.

Java Fundamentals 2: Abstract classes, interfaces, and polymorphism

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.