[Learn Java from scratch notes Interface

We can focus on the author's account, focus on learning Java from scratch notes anthology. We can also learn from a catalog visit the author's blog garden blog. The film documents the learning and the sharing of information on employment-based programmers dark horse class video, and record notes and their own views. Welcome to learn and discuss.

[Notes] from scratch to learn Java directory

Just before the chapter we mentioned that Java language inheritance single inheritance, a subclass can have only one parent (a son only a Qindie)
Java language provides us with a mechanism to deal with single inheritance the limitations of the interface. A class can implement multiple interfaces (a son can have a lot of godfather)

What is the interface

Interface is an abstract class also than abstract classes, interfaces in all of the methods are all abstract methods, the relationship between interfaces and classes are realized.
Keywords: implements, interface

Format:
interface interface name {
}

Example:

public class InterfaceDemo {

}

interface InterA {
	public abstract void a();
}

interface InterB {
	public abstract void b();
}

interface InterC extends InterA, InterB {
	public abstract void c();

}

class A implements InterC {

	@Override
	public void a() {
		// TODO Auto-generated method stub

	}

	@Override
	public void b() {
		// TODO Auto-generated method stub

	}

	@Override
	public void c() {
		// TODO Auto-generated method stub

	}

}

Members of the interface features

There can be only an abstract method
only constant
default public&abstractmodification method
can only be used public&abstractmodification methods
used by default public static finalto modify the member variables
Recommendation: We recommend the default modifier on hand to

Note: The
interface can not create an object (can not be instantiated)
the relationship between classes and interfaces are achieved, a class can implement an interface must implement all its methods

Class and class: inheritance, single inheritance, multi inherited
classes and interfaces: realization relationship, and more to achieve.
The relationship between the interface and the interface: Continuation multiple inheritance relationship

Advantage interface

1. The relationship between class interface, realized relationship, but also to achieve more than one class can implement multiple interfaces, is the inheritance relationship between classes, java inheritance in a single inheritance, a class can have only one parent class, breaking the inheritance limitations.
2. The rules provide external (USB interface)
3. Reduce the coupling procedure (modular development can be achieved, well defined rules, each person achieve their own modules to improve the efficiency of the development)

The difference between abstract classes and interfaces

Common: continuous extraction, extract the abstract concept of
relationship with the class: 1 difference

  • Class and interface is implemented relations, but also to achieve more than one class may be first multiple interfaces
  • Class and abstract class inheritance, Java Inheritance is single inheritance, a class can have only one parent class, Java is multi-layered in succession inheritance

The difference between 2: Member

  • Member variables:
    abstract class can have member variables, constants can also
    interface has only constant
  • Member methods:
    abstract class can have abstract methods, there may be non-abstract methods
    interface can have only abstract methods, but the method has a default modifier public abstract
  • Constructor:
    abstract class has a constructor method of
    the interface method is not configured

Guess you like

Origin www.cnblogs.com/zllk/p/12656784.html