The concept of inheritance
Inheritance is the subclass inherits the characteristics and behavior of the parent class, so that the subclass object (instance) instance fields and methods having a parent class or subclass inherits methods from parent classes, subclasses that have the same parent class behavior.
The main role of inheritance is that the continued extension of the existing basis.
Inherited characteristics
Subclass the parent class has no private properties, methods.
Subclasses can have their own attributes and methods that a subclass can extend the parent class.
Subclass the parent class can implement in their own way.
Java's single inheritance is inherited, but multiple inheritance, single inheritance is that a subclass can only inherit from a parent class, multiple inheritance is, for example, A class inherits class B, class B inherits Class C, so the relationship is in accordance with Class C is B parent class, the class B is the parent class of class a, which is different from the java inherited a C ++ inheritance characteristic.
To improve the coupling between classes (inherited shortcomings, coupled with high will cause the link between the code).
Interface classes
In the JAVA programming language is an abstract type, is a collection of abstract methods, usually the interface declared interface. Interfaces inherit a class manner, thereby inherit the abstract interface methods.
Interface classes are not the way to write interfaces and classes are similar, but they belong to different concepts. Class attributes and methods described objects. Interface to be implemented for class contains.
Unless the implementation of the interface class is an abstract class, all methods of the class or interface to be defined.
The interface can not be instantiated, but can be implemented. A class that implements the interface, all the methods described within the interface must be implemented, otherwise it must be declared as an abstract class. In addition, in Java, interface type can be used to declare a variable, they can become a null pointer, or is bound to an object of this interface.
Interfaces and class differences:
Interface can not be used to instantiate the object.
Interface no constructor.
All interface methods must be abstract methods.
Interfaces can not contain member variables, in addition to static and final variables.
Interface classes are not inherited, but to be the class implementation.
Interface supports multiple inheritance.
Definition Format:
{public interface interface name
Abstract method 1;
Abstract method 2;
Abstract method 3;
}
Inherited keyword
Inheritance can be used both extends and implements keywords to inheritance, and all classes are derived from java.lang.Object, when a class does not inherit two keywords, the default inherited object (the class in java. lang package, there is no need Import ) ancestor class.
extends keyword
In Java, class inheritance single inheritance, that is, a subclass can have only one parent, it extends only inherit a class.
extends format
. 1 class parent class { 2 } . 3 . 4 class subclass extends parent { 5 }
extends application
father:
1 public class Animal { 2 private String name; 3 private int id; 4 public Animal(String myName, int myid) { 5 name = myName; 6 id = myid; 7 } 8 public void eat(){ 9 System.out.println(name+"正在吃"); 10 } 11 public void sleep(){ 12 System.out.println(name+"正在睡"); 13 } 14 public void Introduction () { 15 System.out.println ( "Hello everyone I am!" + Id + "No." + name + "." ); 16 } 17 }
Subclass:
1 public class Mouse extends Animal { 2 public Mouse(String myName, int myid) { 3 } 4 }
The method of the parent class has been inherited by subclasses, without creating a direct call to
implements keyword
Disguise using the implements keyword has multiple inheritance make java characteristics, use of the class inherits the case interfaces can inherit multiple interfaces simultaneously (comma separated with the interface between the interface).
implements format
1 public interface A { 2 public void eat(); 3 public void sleep(); 4 } 5 6 public interface B { 7 public void show(); 8 } 9 10 public class C implements A,B { 11 }
implements application
interface:
public interface Smoking { // member variable public static Final Double A = 3.14159 ; int B =. 7 ; // Interface methods are abstract methods public abstract void Smoke (); }
inherit:
1 public class Person implements Smoking { 2 3 @Override 4 public void smoke() { 5 System.out.println("抽烟"); 6 7 } 8 9 }
Precautions
1. The interface can not create an object, so that others can only achieve
2. Interface member variables can only be modified with public static final
public static constant
public: public, in any class in any package can access
static: static, as long as static modifications can be invoked through the class name property name / method name ().
final: the final, final modification variable, the variable is a constant, a life-long assignment
3. If you do not write the interface modifiers, you add to the default modifiers, which Tim which lack
4. interface can only be modified lack of which add public abstract which