Java Interface----Inheritance (Implementation) method

Source: Internet
Author: User

    1. The term "implementation" in the text refers specifically to the inheritance of interfaces.
    2. When a class implements multiple interfaces, the default method with the same name cannot occur.
    3. A class must implement both interfaces and abstract classes, inheriting them first.
    4. An abstract class can inherit multiple interfaces (implements), an interface cannot inherit an abstract class, and an interface can inherit multiple interfaces (extends).
    5. The default method in the interface, the abstract method is omitted, the data member must be assigned the initial value, final can be omitted.
    6. The role of interfaces is to establish standards, a code that all parties need to follow.
    7. In order to simplify the client (not listing all the objects to choose from), the interface is taken as an example, and the Factory mode is preferred.

public class Test1 {

public static void Main (string[] args)

{Fruit a = factory1.getinstance ("Apple");

A.eat ();
}
}

Interface Fruit
{
public void eat ();
}
Class Apple implements Fruit
{
public void Eat ()
{
System.out.println ("Eat apples");
}
}
Class Orange implements Fruit
{
public void Eat ()
{
System.out.println ("Eat oranges");
}
}
Class Factory1//Get instance object of fruit class
{
public static Fruit getinstance (String classname)
{
if ("Apple". Equals (classname))
return new Apple ();
if ("Orange". Equals (classname))
return new Orange ();
return null;
}
}
At this time the program, the client (main method) is not coupled with the specific subclass, if there are more Friut sub-class interface only need to modify the factory class, all interface objects are obtained through the factory class, in the development
You should use factory design mode whenever you encounter an operation that obtains an instance of an interface object.

Java Interface----Inheritance (Implementation) method

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.