Please disable adblock to view this page.

← Go home

Inheritance snippet – Vehicle (Java)

April 27, 2016
Published By : Pratik Kataria
Categorised in:

//******************* Vehicle.java *******************

public class Vehicle {
	private String PNo;
	private int CNo;
	
	Vehicle(){
		System.out.println("Vehicle() ");
		PNo = "NA";
		CNo = 0;
	}
	
	Vehicle(String PNo, int CNo){
		System.out.println("Vehicle( String, int ) ");
		this.PNo = PNo;
		this.CNo = CNo;
	}
	
	public String getPNo(){
		return this.PNo;
	}
	
	public void setPNo(String PNo){
		this.PNo = PNo;
	}
	
	public int getCNo(){
		return this.CNo;
	}
	
	public void setCNo(int CNo){
		this.CNo = CNo;
	}
	
	
	
}


//******************* Car.java *******************

public class Car extends Vehicle{

	private int type;
	private String color;
	
	Car(){
		System.out.println(" Car() ");
		type = 0;
		color = "NA";
	}
	
	Car(String PNo, int CNo, String color , int type){
		
		//This should be the first statement if we are overriding the default constructor to be called by base class.
		super( PNo,  CNo);
		
		System.out.println("Car( String, int, String, int )");
		//setPNo("MH12 1213");	//By doing this we are overwriting the default values of 0 which is being set by the default constructor of vehicle. So extra CPU cycles are invested
		//setCNo(12334);
		this.type = 2;
		this.color = "Black";
		
	}
	
	
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	
	public void display(){
		//System.out.println("PNo     : " + PNo); This is invalid as it breaks encapsulation
		System.out.println("PNo    : " + getPNo() );
		System.out.println("CNo    : " + getCNo() );
		System.out.println("Color  : " + color );
		System.out.println("Type   : " + type );
		
		
	
	}
	
	
	
}


//******************* Main.java *******************

public class Main {

	public static void main( String [] args){
		Car c1 = new Car("MH12 233", 23445, "Red", 1);
		
		c1.display();
		
		/* Commented since we are using parameterized constructor
		c1.setPNo("MH12J3552");
		c1.setCNo(213432);
		c1.setColor("Red");
		c1.setType(2); //2 means diesel lol
		*/
		
		//Vehicle v1 = new Vehicle();		//There is no connection with c1. It is a different object
		
	
		
		
		//v1.setPNo("MH21");
		//v1.setCNo(9999);
		
		//Commented since we have display
		//print(c1); //since it is static we can directly call. Note: c1 is passed and not v1. We cannot pass v1. If it were Vehicle v in function's formal parameters. We could have passed c1 and v1 in print()
		
		System.out.println("------------------------");
		
		
		//c1.display();
	}
	
	public static void print( Car c ){
		System.out.println("PNo    : " + c.getPNo() );
		System.out.println("CNo    : " + c.getCNo());
	}
	
}

Output:

Vehicle( String, int )
Car( String, int, String, int )
PNo : MH12 233
CNo : 23445
Color : Black
Type : 2
————————

View Article Page
Download

Pratik Kataria is a budding programmer, web designer and developer.