Reprint: http://www.cnblogs.com/hellokitty1/p/4409474.html
1: Definition of inheritance:
Java inheritance is one of the most prominent features of object-oriented. Inheritance is the derivation of new classes from existing classes, which can absorb data properties and behaviors of existing classes, and can extend new capabilities.
2: Keyword: extends: inheritance
3: Format: public class A extends B {} A: Represents subclass B: Represents the parent class
4: Subclasses can inherit the public,protected member of the parent class.
The inheritance in 5:java is single-inheritance, and it has single-root and transitive nature. So there is only one parent class for a class.
6: Method Overrides: (Child class overrides of parent class method)
Its rules:
1: The return value type of the subclass, the method name, and the argument list must be the same as the parent class;
2: The access modifier must be greater than the scope of the parent class modifier;
3: Subclasses overriding exceptions cannot be thrown more than the parent class.
Method overloads that differ from
Rule: There are two or more methods in a class with the same name, and the parameter list is different.
7:super Keyword: parent class super Class
8:final keywords
1:final class: No subclass, cannot be inherited
2:final method: cannot be overridden
3:final constants: Constant values cannot be modified
9: Advantages of Inheritance:
1: reusability of code
2: Methods and properties of the parent class can be used for subclasses
3: Designing applications makes it easy
4: Subclasses can inherit properties and methods of parent class
10: Instantiation Process
: Inherited exercises:
1. Create Pet class, PET has name, health value, and owner intimacy value attributes
2. Create dog class, dog has name, health value, intimate value with host, variety of attributes
3. Create Penguin class, Penguin has name, health value, intimacy value with host, sex attribute
4. Create a Dog object and print out basic information about the dog
5. Create Penguin Objects and print basic penguin information
6. Add the method of pet barking, test the method of barking dogs and penguins, such as the dog is "Wang Bark" of the call, the Penguin is "Goo Cuckoo" called
Analysis:
Pet Dog class Penguin dog class and Penguin inherit the name of the pet class, health value, and the owner of the intimacy value of the property
Code:
Package com.lovo.exercise;
/**
* Pet Class
*
* @author Administrator
*
*/
Public abstract class Pet {
private String name;
private int health;
private int love;
Public Pet () {
Super ();
System.out.println ("In the Pet constructor");
}
Public Pet (String name, int health, int. Love) {
THIS.name = name;
This.health = health;
This.love = Love;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int Gethealth () {
return health;
}
public void sethealth (int health) {
This.health = health;
}
public int Getlove () {
return love;
}
public void Setlove (Int. love) {
This.love = Love;
}
Public String toString () {
Return "Pet [name=" + name + ", health=" + health + ", love=" + love+ "]";
}
public abstract void shout ();
}
Dog class
Package com.lovo.exercise;
/**
* Dog
*
* @author Administrator
*
*/
public class Dog extends Pet {
Private String strain; Varieties
Public Dog () {
System.out.println ("In the Dog constructor");
}
Public Dog (string name, int health, Int. Love, string strain) {
Super (name, health, Love);
This.strain = strain;
}
Public String Getstrain () {
return strain;
}
public void Setstrain (String strain) {
This.strain = strain;
}
Public String toString () {
return super.tostring () + "\ndog [strain=" + Strain + "]";
}
public void shout () {
System.out.println ("Wang Bark");
}
}
Penguin class
Package com.lovo.exercise;
/**
* Penguin
*
* @author Administrator
*
*/
public class Penguin extends Pet {
Private String sex;
Public Penguin () {
}
Public Penguin (string name, int. health, int love, string sex) {
Super (name, health, Love);
This.sex = sex;
}
Public String Getsex () {
return sex;
}
public void Setsex (String sex) {
This.sex = sex;
}
@Override
Public String toString () {
return super.tostring () + "\npenguin [sex=" + Sex + "]";
}
public void shout () {
System.out.println ("Goo-cluck");
}
}
Test
Package com.lovo.exercise;
public class Test {
public static void Main (string[] args) {
Dog dog = new Dog ("Small white", 88, 99, "Snow Larry");
System.out.println (dog);
Dog.shout ();
System.out.println ("**********************");
Penguin pen = new Penguin ("Q sister", 88, 99, "female");
System.out.println (pen);
Pen.shout ();
}
}
Second, using abstract class abstract method to complete
Cars have license plate number, brand attributes
Sedan has model properties
Passenger cars have seating number properties
Inheritance of JAVA