java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

Java继承inheritance

Java继承
Java inheritance

我们可以把继承看成是儿子,继承了父亲的一些特点,但是儿子可以有一些改变。

java里儿子继承了父亲的公共属性 和公共方法,儿子可以修改这些属性和方法。儿子修改父亲的公共属性,我们叫重载。没有修改的叫继承。

We can think of that their children inherit their father, the child can have the father of all public properties and public methods, you can also change these properties and methods. When the children change his father's property, we call it overide. Does not change is called inheritance.

下面看一个汽车和蓝色汽车的例子。

father class

/**
java-er.com
learn java is so easy
*/
import java.util.*;
public class Car // Car is a class
{
	public int wheel = 4; // wheel,car's property
	
	public void run(){ // a method , car can run
		System.out.println("car run");
	}

	public void stop(){ // a method , car can run
		System.out.println("car stop");
	}


	public static void main(String[] args){
			Car c1 = new Car(); //a new object
			c1.run();

			Car c2 = new Car();// second object
			c2.stop();

			//there are two car
			// first run, second stop.

			/*
			Car is a class, c1 is an Object,c2 is other Object.
			*/
	}
}

child class

/**
java-er.com
learn java is so easy
*/
import java.util.*;
public class BlueCar extends Car// Car is a father class,Blue Car is a child class.
{
	
	
	public void MYColor(){
		System.out.println("my color is blue");
	}

	public void stop(){ // overide
		System.out.println("I can stop!");
	}


	public static void main(String[] args){
			BlueCar c1 = new BlueCar(); //a new object
			c1.run();  // child has father's method and property

			System.out.println("c1 wheel is " + c1.wheel);

			c1.stop(); // child can change the method what father has. we call it overide in java.

			c1.MYColor(); // child has same method what father has not			
	}
}


This entry was posted in JAVA and tagged . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/java-inheritance/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

Leave a Reply