1. inherited Overview
Inheritance is a subclass inherits variables and methods of the parent class, use the code below to explain:
class Student {// definition of class students
String name;
int age;
void study() {
System.out.println(name + "studay good" + age);
}
}
class Work {// definition of class workers
String name;
int age;
void work() {
System.out.println(name + "work good" + age);
}
}
You can see from the above example, the class students and workers class has common characteristics, in order to achieve reusability of code, extraction, the Java extract with class represents the common behavior or characteristics extraction, then turned to the class also can be used, we need to extend our class extracted with the original class, with extends call keywords, such as:
class Student extends Person {// definition of class students
void study() {
System.out.println(name + "studay good" + age);
}
}
class Work extends Person {// define the categories of workers
void work() {
System.out.println(name + "work good" + age);
}
}
class Person {// define our class extraction
String name;
int age;
}
Usually called the Person (inherited class) class as a parent class (also known as exercise classes, base class), said the Work and Student (inherited class) called subclasses.
2. inherited advantages
Improve code reusability
Let the relationship between class and class produce, to provide a multi-state premise
java support single inheritance, do not support multiple inheritance of C ++ multiple inheritance make improvements
Single inheritance: a subclass can have only one direct parent
class A {
}
class B {
}
class C extends A {
}
or
class A {
}
class B {
}
class C extends B {
}
Multiple inheritance: a subclass can have more than one immediate parent class ( why not multiple inheritance ) [does not directly support, create uncertainty calls]
class A {
void show() {
System.out.println(a);
}
}
class B {
void show() {
System.out.println(b);
}
}
class C extends A,B {
}
When our new c (); calls the show method is called A 's or B 's? To distinguish java this was modified to do multiple inheritance with interfaces
java supports multiple (multiple inheritance) => transitive
class A {
}
class B extends A {
}
class C extends B {
}
3. The principle of succession of class
Single inheritance
Inheritance is transitive
Inheritance can not be recycled
In addition Object class, all class has a parent class
4. The use of inheritance
When you want to use an inheritance system, how to use ?
View the top class in the system, understand the basic functions of the system.
The system created a subclass of the object of the most complete use of functions.
When writing to inherit?
When there is an owning class relationship inheritance definition, A belongs to B, then A succession B
5. inherited a member variable
When members of this class of local variable names are the same, and with this distinction
When the sub parent class member variable with the same super distinguish
this and super usage is very similar
the this : a class object reference to the representative of the present
Super : on behalf of a parent class space
class A {
int num = 5;
}
class B extends A {
int num = 10;
void show() {
System.out.println (this.num + " ...... " + super.num); // call the parent class and subclass member variables
}
}
Note: sub-class can not directly access private members of the superclass
6. The members of the inherited method
class A {
int num = 5;
void show1() {
System.out.println(num);
}
}
class B extends A {
int num = 10;
void show2() {
System.out.println(num);
}
}
public class Extendstext {
public static void main(String[] args) {
B b = new B();
b.show1();
b.show2();
}
}
When the sub-class of the parent function, the function will run subclass, called overwriting
class A {
int num = 5;
void show() {
System.out.println(num);
}
}
class B extends A {
int num = 10;
void show() {
System.out.println(num);
}
}
public class Extendstext {
public static void main(String[] args) {
B b = new B();
b.show();
}
}
6.1 two characteristic function
Overload: the same class
Overrides: subclass, covering also called rewrite
6.2 Considerations
When a subclass overrides the parent class method, rights authority subclass must be greater than the parent class
Static can only cover foreign exchange rebate static, or static override
6.3 When to use to cover operating
When a class is the subcategory expansion, subclasses need to retain the function of the parent class declaration, but the content to define specific functional subclass, with a cover to complete the operation. ( Modification method, add functionality ) , such as:
class Phone {// define a class phones
void call () {// call function
}
void show () {// display function
System.out.println("number");
}
}
class newphone extends Phone {
void show () {// display function rewrite
System.out.println("name");
System.out.println("jpg");
System.out.println("number");
}
}
or
class newphone extends Phone {
void show () {// display function rewrite
System.out.println("name");
System.out.println("jpg");
super.show (); // call the parent class show method
}
}
7. inheritance constructor
In a subclass object is constructed, when accessing the sub-class constructor, the parent class is also run in the constructor subclass, a default recluse statements, Super (); call the constructor of the superclass hollow parameters, uncoated, no inheritance
No arguments
class fu {
fu () {
System.out.println("fu run");
}
}
class zi extends fu {
day () {
super (); // Hermit comes null call the parent class constructor parameter
System.out.println("zi run");
}
}
There are parameters
class fu {
fu(int x) {
System.out.println("fu run");
}
}
class zi extends fu {
day () {
super (4); // Hermit comes null call the parent class constructor parameter
System.out.println("zi run");
}
}
Subclass instantiation, the constructor of each subclass will access the parent class constructor parameter hollow
The results: AC AD
public class Extendstext {
public static void main(String[] args) {
new zi();
new zi(6);
}
}
class fu {
fu () {
System.out.println("A");
}
fu(int x) {
System.out.println("B");
}
}
class zi extends fu {
day () {
System.out.println("C");
}
zi (you x) {
System.out.println("D");
}
}
The results: BD
public class Extendstext {
public static void main(String[] args) {
new zi(6);
}
}
class fu {
fu () {
System.out.println("A");
}
fu(int x) {
System.out.println("B");
}
}
class zi extends fu {
day () {
System.out.println("C");
}
zi (you x) {
super (x); // covering implicit Super ();
System.out.println("D");
}
}
Inherited advantages :
Improve the reusability of code, more concise.
Memory structure regarding inheritance :( code below)
// subclass
package com.ss.cn;
/*
* Object-Oriented - Inheritance
* To create both a teacher of the class java php teacher
* Teacher what attributes : name age
* The teacher's behavior: the Teach SLEEP rest
*
*/
public class JavaTeacher extends Teacher {
// embodiment using encapsulated
/*private String name;
private int age;*/
// Methods:
public void teach() {
/*
* The current sub-class that is not so in the name , this is not the kind of thing that is absolutely not visit
* Name now in the parent class, Private String name private, protected, and that is if the other classes can not visit, even if he has inherited relationship
*
*/
System.out.println (getName () + " teach lessons ");
}
// Why setget method
/*
* To obtain the value needs to be generated using the package must also be set / get method
* Generating set / get the shortcut keys: Shift + Alt + S
*/
}
// sub-categories:
package com.ss.cn;
/*
* Object-Oriented - Inheritance
* To create both a teacher of the class java php teacher
* Teacher what attributes : name age
* The teacher's behavior: the Teach SLEEP rest
* Create a parent teacher
* Inheritance syntax: keyword extends
* Class subclass extends parent {
* Attribute subclass;
* Method subclass;
* }
* Features: All properties and methods of the subclass will inherit blessing of the parent class (out Final )
*/
public class HtmlTeacher extends Teacher{
public void teach() {
System.out.println (getName () + " teach html class ");
}
}
package com.ss.cn;
// parent class
public class Teacher {
// public class
private String name;
private int age;
public void sleep() {
System.out.println (name + " sleeping ");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
// test class
package com.ss.cn;
public class ExtendsDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1. The object properties must create objects
JavaTeacher jTeacher = new JavaTeacher ();
jTeacher.setName("Mrs");
jTeacher.setAge(25);
jTeacher.teach();
// Similarly:
HtmlTeacher hTeacher=new HtmlTeacher();
hTeacher.setName("ssy");
hTeacher.setAge(23);
hTeacher.sleep();
hTeacher.teach();
}
}
Why is self-instance of a subclass of the time, will call the parent class constructor?
Subclass inherits the parent class, so subclasses have attributes of the parent class, before using the parent class content, to look at how to parent their content is initialized, the sub-class constructor to initialize the time, you must call the parent class constructor , so the default constructor subclass added Super ();
If the parent class is not defined or no argument constructor is the parent class has defined parameters, no parameters are not defined, it must Super (); to specify the parent class constructor call
If the constructor of the subclass using this (); call this class constructor, the super (); there is no, because the super and this can only be defined in the first line, so only one, but can guarantee that there will certainly be other subclass constructor to access the constructor of the parent class.
Note: Super (); statement must be defined in the first row of sub-class constructor, because the initialization is completed first parent class