Java basics-abstract classes, interfaces and polymorphism, java Polymorphism

Source: Internet
Author: User
Tags define abstract

Java basics-abstract classes, interfaces and polymorphism, java Polymorphism

Abstract classes, interfaces, and polymorphism are basic objects. I believe that people who can see this blog will not be entangled in its basic definition, this article will try to explore its connotation at a deeper level, hoping to help you.

I. abstract classes

1. Form

 1 abstract class Ceshi  2 { 3     abstract void show(); 4 } 5 class DemoA extends Ceshi 6 { 7     void show() 8     { 9         System.out.print("Hello world");10     }11 }

 

2. Features

A. abstract classes cannot be instantiated.

B. the abstract class must have its subclass covering all abstract methods before it can be instantiated. Otherwise, the subclass is still an abstract class.

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

3. The problem arises.

A. Do the abstract classes have Constructors?

Yes. It is used to instantiate the subclass.

B. Can abstract classes not define abstract methods?

Actually yes, but it is rare.

C. Similarities and Differences between abstract classes and general classes.
Similarities:
Abstract classes and general classes are used to describe things, and members are set internally.
Differences:
1. The general class has enough information to describe things. Abstract classes may not provide sufficient information to describe things.
2. abstract methods cannot be defined in a general class. Only non-abstract methods can be defined. Abstract classes can define abstract methods and non-abstract methods.
3. General classes can be instantiated. Abstract classes cannot be instantiated.

Ii. Interfaces

In a sense, an interface is a special form of abstract class. in java, an abstract class represents an inheritance relationship. A class can only inherit one abstract class, A class can implement multiple interfaces.

We all know that the interface definition method is

interface Demo{   void show();      }

In fact, it is common for interface members: And these Members have fixed modifiers.
A. Global constant: public static final

B. abstract method: public abstract

1. It is concluded that all members in the interface have public permissions. That is, full write is

interface Demo{    public static final int num = 4;    public abstract void showa();}

2. interface features

A. interfaces are externally exposed rules and extensions of program functions.

B. Interfaces reduce coupling.

C. A class can implement multiple interfaces.

    

abstract class Test2 extends Q implements A,Z{}

3. Similarities and Differences between interfaces and abstract classes

Similarities:
Are constantly extracted.

Differences:

1. abstract classes must be inherited and can only be inherited separately. The interface needs to be implemented and can be implemented in multiple ways.

2. abstract classes can define abstract and non-Abstract METHODS. After subclass inheritance, you can directly use non-abstract methods. Only abstract methods can be defined in the interface and must be implemented by sub-classes.

 

Iii. Polymorphism

An object has different forms, which are polymorphism.

For example, animal ani = new dog ();

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

1. benefits and disadvantages of Polymorphism

Benefits of polymorphism: It improves code scalability and can use later content for pre-defined code.

Disadvantages of polymorphism: the pre-defined content cannot use (called) the special content of the Post-subclass.

2. Downward Transformation

Animal ani = new Cat (); Cat c = (Cat) ani;

3. Internal class

Internal classes are rarely created before. In fact, there are classes in the class.

Class Outer // external class {class Inner // internal class. {
}}

A. How do internal classes reference external classes?

Internal classes hold references to external classes. 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. Internal classes can be stored in local locations, and only local variables modified by final can be accessed in local locations.

class Outer{    void method()    {        final int x = 9;        class Inner        {            public String toString()            {            }        }    }}

 

4. Anonymous internal class

An anonymous internal class is an abbreviated form of an internal class. It is actually an anonymous sub-object.

Public void method () {new Demo () // anonymous internal class. {Void show () {System. out. println ("show:" + num) ;}}. show ();}

 

Iv. Final

Abstract classes, interfaces, and polymorphism are simple but common, especially internal and anonymous internal classes. We have less contact with each other before, so we should focus on them.

 

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.