The third week of gains

This week drunken spider to learn the class inheritance:

The concept of inheritance

Inheritance is a cornerstone of the object-oriented programming java technology, as it allows the creation of hierarchical level classes.

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.

In Java, a class can be declared by the extends keyword is as follows from another class inherited, the general form:

Inherited class format

class parent class { }
class subclass extends parent class { }
  • 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 is the parent class a class B class, which is different from the C ++ Java inherited inherited a feature.

  • To improve the coupling between classes (inherited shortcomings, it will cause a high degree of coupling between the more closely the code, the code independence worse).


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

Problems encountered: the class inheritance in particular the use of fuzzy travel

Next week learning java string class

Guess you like

Origin www.cnblogs.com/kongfanbing/p/11220486.html