[Java] inherited class

inherit

What is inherited?

On the basis of an existing class to build up a new class
so generated between the class and classThe parent class and subclassRelations
----------------------------------------------

Keywords are inheritedextends
Java supports only single inheritance, but multiple layers can inherit
inherit only public members of the parent class
benefits: improve code reusability, maintainability
drawbacks: too strong coupling between class and class
----- -----------------------------------------

Cat class inherits the Animal class:

public class Cat extends Animal {
}

Override the parent class method

Override the method occurs in the child-parent relationship in class
when the functionality of the parent class provides a subclass does not meet specific needs, you need to be rewritten to the parent class
subclass must override the parent class method with the parent class exactly the same method declaration
@override: whether to force the inspection method is overridden method
--------------------------------- -------------

Cat class overrides the Animal class:

public class Cat extends Animal {
	public void eat() {
	syso();
	}
}
Published 38 original articles · won praise 4 · Views 824

Guess you like

Origin blog.csdn.net/Hide111/article/details/104986540