First : Inherit , Refers to the fact that one object directly uses the properties and methods of another object .
Inherited format :
-
-
-
- public class Subclass name entends Parent class name {} /* Indicates that the previous subclass inherits the parent class */
- example :public class Zi extends Fu{}
-
-
The following is the code description :
/*
* Animal species , Parent class
* This is a parent class
*/ public class Animal {
private String name;
private int age;
private String color; public Animal(String name, int age, String color) {
this.name = name;
this.age = age;
this.color = color;
}
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;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public void show() {
System.out.println(" Animal calls :");
} }
Next, let's create a subclass
/*
* This is a subclass of cats , It belongs to animals , So we inherit the above animals
*/
public class Cat extends Animal { public Cat(String name, int age, String color) {
super(name, age, color);
super.show();
}
public void show() {
super.show();
System.out.println(" cat ...");
}
}
Let's create a subclass
/*
* This is a subclass of dogs , Dogs also belong to animals , We can also inherit the animal class, that is, the parent class
*/
public class Dog extends Animal{ public Dog(String name, int age, String color) {
super(name, age, color);
}
public void show() {
System.out.println(" Wang Wang Wang ...");
}
}
Next , We create a new test class
public class AbimalDemo { public static void main(String[] args) {
Dog dog1 = new Dog(" Li heihei ",6," gray ");
System.out.println(dog1.getName()+"\t"+dog1.getAge()+"\t"+dog1.getColor());
dog1.show(); Cat cat1 = new Cat(" Song Maomao ",5," Pink ");
System.out.println(cat1.getName()+"\t"+cat1.getAge()+"\t"+cat1.getColor());
cat1.show(); } }
Let's look at the results :
:
In the running result, we can see that the show Method, when we create a subclass object, we can call the result of the parent class That is, the sound of animals
To sum up :
The advantage of inheritance is that subclasses can override the methods of the parent class to easily extend the parent class
①: The inner details of the parent class are visible to the child class .
②: The methods that subclasses inherit from their parents are determined at compile time , So you can't change the behavior of methods inherited from a parent class during runtime .
12.16 Java More articles on inheritance
- mysql Query exercises -2016.12.16
>>>>>>>>>> Practice time :2016.12.16 Edit time :2016-12-20-->22:12:08 topic : involve : Multi-table query .ex ...
- java Member variables in inheritance relations , Construction method , Relationship between member methods
Java Membership in succession A: Member variables a: The member variable name of the subclass is different from that of the parent class , It's too easy to write that name and visit that name ! b: The member variable name of the subclass is the same as that of the parent class , How to visit this ? Son ...
- java Inheritance mechanism
1 Inherit keyword :extends java There is no multiple inheritance example Parent class : package unit4; public class Base { public int publicVarofBase= ...
- Java The method of inheritance rewrites
Catalog Java The method of inheritance rewrites The code is Concept matters needing attention " Two are the same, two are small and one is big " Other considerations Overrides and overloads @Override annotation Java The method of inheritance rewrites stay Java In the inheritance , A subclass can get a parent ...
- 2019.12.11 java Several common exceptions in the program and the reasons for this exception
1.java.lang.NullpointerException( Null pointer exception ) reason : This exception is often encountered , The cause of the exception is a null pointer in the program , That is, the program calls an uninitialized object or an object that does not exist. . Often occurs when creating pairs ...
- Java Inherit Basic Edition
Inheritance is an effective means to realize code reuse in software development , If a class A Inherited classes B So class B Medium public.protected And the instance member or static member decorated by the default modifier will be modified by the class A Inherit , It can also be said that class B Members of are classes A Members and classes A On this basis ...
- Java Inheritance and combination
Java Inheritance and combination Inherit java Use in extends Keywords represent inheritance relationships , When creating a class , If you don't specify the class you want to inherit from , Is implicitly derived from the root class Object Inheritance . The subclass inherits the member variables of the parent class Subclasses can inherit from their parents ...
- JAVA The problem of constructors in inheritance
See today java When inheriting part , There's a problem with whether and how to use constructors , Later, we found the relevant information and solved it . Here is my personal summary : First create a parent class , There are two constructors in it : public class Jisuan ...
- Java Inheritance and interfaces
The most critical role of interface , It is also the most important reason for using interfaces : Can be traced back to multiple basic classes . The second reason for using interfaces is the same as for using abstract base classes : Prevent client programmers from making an object of this class , And stipulate that it is only an interface . This brings a ...
- Java Initialization of inheritance
/** * Created by xfyou on 2016/11/2. * Java Initialization of inheritance */ public class Beetle extends Insect { int k = pr ...
Random recommendation
- Chapter 10 .bootstrap Wheel planting
Use bootstrap The rotation plug-in can add sliders to the site , The content can be an image , Embedded frame , Video or anything else , To use the rotation plug-in, you need to introduce bootstrap.min.js. <div id="carous ...
- Mybatis Paging plug-ins
mybatis To configure <!-- mybatis Paging plug-ins --> <bean id="pagehelper" class="com.github.pageh ...
- How to disable Marlin Temperature protection
Recently playing 3D Print , It's a set MEGA 2560 + RAMPS 1.4 + A4988, brush Marlin(https://github.com/MarlinFirmware/Marlin) The firmware , Connect the motor for commissioning ...
- Android Studio Jar、so、library Project dependence
Eclipse Follow AS Different from Eclipse To AS Don't take it with you Eclipse Subjective color in AS Use in , It is different from the composition of the project to the construction , Listed below are Eclipse and AS Differences in some concepts in : WorkSpace ...
- Basic4android: Multifunctional Android Application software rapid development platform
Basic4android It's by far the simplest . The most powerful Android Platform rapid application development tool . ( "Basic4android is the simplest and most powerful ...
- Unity3D Editor Extension ( 5、 ... and )—— Common features (Attribute) as well as Selection class
I wrote four articles about the editor : Unity3D Editor Extension ( One )—— Define your own menu buttons Unity3D Editor Extension ( Two )—— Define your own windows Unity3D Editor Extension ( 3、 ... and )—— Use GUI Drawing window Unity3D ...
- adopt WebChannel/WebSockets And QML Medium HTML Interaction
source : adopt WebChannel/WebSockets And QML Medium HTML Interaction GitHub: Eight to author : Fox fish Link to this article :QML And HTML Interaction In the query QML And HTML There is very little information in the communication between them , This article ...
- C Language · FBI Trees
Algorithm training FBI Trees The time limit :1.0s Memory limit :256.0MB kits 1 Binary tree . Problem description We can turn the “0” and “1” There are three types of strings : whole “0” The string is called B strand , whole “1” The string is called I ...
- Android : Get the test code of sound card information
Complete compilation package (android platform ): link :http://pan.baidu.com/s/1qXMTT7I password :2bow /* * ALSA parameter test program * * C ...
- C# Hadoop Learning notes ( Two )— Framework principle
One , framework Two . A term is used to explain ( One )NameNode( abbreviation NN),Hadoop The primary node of , Be responsible for listening whether the node is active , Opening up interface, etc . In the future big data processing , Due to the increasing number of visits and nodes , The processing energy of this node is required ...