Some knowledge of abstract classes and interfaces in Java

Source: Internet
Author: User

"Abstract class"

1. Concept: A class that contains an abstract method is an abstract class.

2. Abstract method: A method declared without being implemented.

3. When an abstract class quilt inherits, subclasses must override all abstract methods in the abstract class.

4, abstract class keywords: abstract

5. Define the format:

Abstract class ClassName {

Property

Method

Abstract methods

}

6. Emphasis: Abstract classes cannot be instantiated directly, and must be instantiated by subclasses. When writing a subclass using the extends keyword, you need to be aware of overriding all the abstract methods of the parent abstract class.

(interpretation: In simple terms, abstract classes are classes that cannot be directly instantiated.)

For example, a shape class, we cannot instantiate a shape class directly, because it cannot be said that a Shape object is directly instantiated. Such a class is an abstract shape class

But it can be said to instantiate a circular class that inherits from the (extends) abstract shape of the parent class. )

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

Example:

Create a sample class.

Abstract class abc{

private String name;

public void Tell () {

System.out.println ("I am the Tell method in ABC class");

}

public abstract void Say ();

}

As above is a standard sampling class, the class name is ABC, the property has a string type name, which contains a common tell () method, and a sampling method say ();

Because the class contains an abstract say (); Method so the ABC class is a sample class.

You can try to remove the abstract keyword before the ABC class. The compiler will make an error. Because the class that contains the abstract method is a sample class, you must declare the abstract keyword.

Main method:

public class demo{

public static void Main (String [] args) {

ABC abc=new ABC ();

The compiler has an error because the sample class cannot be instantiated. Subclasses must be created to inherit the abstract class and override the abstract method. The following will write the rewrite format.

     

Son s=new son ();

S.tell ();

S.say ();

Use the subclass son class of the abstract class directly, calling the tell and say methods.

}

}

To create a subclass:

Class Son extends abc{

pulic void Say () {

System.out.println ("I am the say method of the son class. Rewrite the ABC abstract class say Method ");

}

}

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

Interface

1, interface: Can be understood as a special class.

(only single inheritance in Java, that is, one or more subclasses can inherit only extends a parent class.)

However, interfaces can be implements by classes to multiple implementations. )

2, in the interface: all by the global constant and the public abstract method.

3, Global constants: public static final int max=100; That is, the global public, static, non-changing.

3, the implementation of the interface must also be implemented through subclasses. However, the keyword implements is used instead of extends, and a class can use more than one implements keyword, that is, it can be implemented more.

4. Format:

Interface name{

Global constants

Abstract methods

}

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

Interface inter1{

public static final int max=100; Global constants

public abstract void tell (); Static methods

}

Interface inter2{

public abstract void Say ();

}

Class A implements inter1,inter2{//interface multi-implementation. Unlike subclasses, you can inherit only one parent class.

public void Tell () {

}

}

public class demo{

public static void Main (String [] args) {

A a=new a ();

A.tell ();

A.say ();

System.out.println (Inter.max);

}

}

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

Focus:

1. A class can inherit a class and implement some interfaces at the same time.

That is: Class A extends ABC implements inter1,inter2{

Writes an override of an abstract method that corresponds to an interface.

}

2. Interfaces cannot inherit abstract classes. However, you can use the extends keyword to inherit multiple interfaces.

namely: Inter extends inter1,inter2{

}//Multiple Inheritance of interfaces

Interfaces can inherit more, but classes cannot inherit more.

The difference between an abstract class and an interface
    • 1. Methods in an abstract class can have a method body, which is the specific function that can implement the method, but the method in the interface is not.
    • 2. member variables in an abstract class can be of various types , whereas member variables in an interface are only public static final types ( Global constants ).
    • 3. Interfaces cannot contain static code blocks and static methods (methods that are decorated with static), whereas abstract classes can have static code blocks and static methods.
    • 4. A class can inherit only one abstract class, while a class may implement multiple interfaces.

Some knowledge of abstract classes and interfaces in Java

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.