Super Command In JAVA


Super is an important command in java. It is used to call the upper class inside a sublcass


eg



1. office.java:

import java.util.*;

public class office {

	public static void main(String[] args){
		
		int i,j,k,l,caseid;
		
		Scanner oc_var=new Scanner(System.in);
		
		System.out.println("Starting multi level hierachy code"+ "\n");
		
		System.out.println("Enter you choice of box variables");
		System.out.println("1. if no variable"+"\n"+"2. if all variables"+"\n"+"3. if cube");
		caseid=oc_var.nextInt();
		
		switch(caseid){
		
		case 1:
			System.out.println("No variable mode selected. Default values loaded");
			vol kk1=new vol();

			System.out.println("Values are, len: "+ kk1.len+ "\n"+"wid: "+ kk1.wid+"\n"+"hieght: "+ kk1.het+"\n"+ "mass: "+kk1.mass);	
			
			kk1.display_vol();
			
		break;
		
		case 2:
			System.out.println("inputs mode selected");
			System.out.println("Enter lenght of box");
			i=oc_var.nextInt();
			System.out.println("Enter width of box");
			j=oc_var.nextInt();
			System.out.println("Enter hieght of box");
			k=oc_var.nextInt();
			System.out.println("Enter mass of box");
			l=oc_var.nextInt();
			vol kk2=new vol(i,j,k,l);

			System.out.println("Values are, len: "+ kk2.len+ "\n"+"wid: "+ kk2.wid+"\n"+"hieght: "+ kk2.het+"\n"+ "mass: "+kk2.mass);	
			
			kk2.display_vol();
		break;
			
		case 3:
			System.out.println("cube mode selected");
			System.out.println("Enter lenght of box");
			i=oc_var.nextInt();
			System.out.println("Enter mass of box");
			j=oc_var.nextInt();
			vol kk3=new vol(i,j);

			vol mVar;
			mVar=kk3;
			System.out.println("Values are, len: "+ mVar.len+ "\n"+"wid: "+ mVar.wid+"\n"+"hieght: "+ mVar.het+"\n"+ "mass: "+ mVar.mass);				
			
			kk3.display_vol();
			break;
		}
		
		
	}
}

2. box.java
public class box {

	//declaring instance variable of box l,b,h,w,v
	int len,wid,het;
	int mass,vol;
	
	// declaring constructor with no inputs(default configuration
	box(){
		this.len=10;
		this.wid=20;
		this.het=30;
	}
	
	//declaring constructor with all inputs mentioned
	box(int len,int wid,int het){
		this.len=len;
		this.wid=wid;
		this.het=het;
	}
	
	//declaring a constructor with cubical configuration
	box(int len){
		this.len=len;
		this.wid=len;
		this.het=len;
	}
}

3. vol.java:
class vol extends packing{

	//no inputs configuration
	vol(){
		super();
		vol= super.len * super.het * super.wid * super.mass;
	}
	
	// if all inputs given
	vol(int len,int wid, int het, int mas)
	{
		super(len,wid,het,mas);
		vol= super.len * super.het * super.wid * super.mass;
	}
	
	// constructor if cube
	vol(int len,int mas){
		super(len,mas);
		vol = super.len * super.len * super.len * super.mass;
	}
	
	void display_vol(){
		
		System.out.println("THE VOLUME o box is : "+ vol);
	}
}

4. packing.java:
class packing extends box {
	
	// construcor if no parameter mention(default values)
	packing(){
		
		super();
		mass=50;
	}

	//constructor if all values given
	packing(int len,int wid, int het,int mas){
		super(len,wid,het);
		mass =mas;
	}
	
	//constructor if cube 
	packing(int len,int mas){
		super(len);
		mass=mas;
	}
}

output:
Starting multi level hierachy code

Enter you choice of box variables
1. if no variable
2. if all variables
3. if cube
1
No variable mode selected. Default values loaded
Values are, len: 10
wid: 20
hieght: 30
mass: 50
THE VOLUME o box is : 300000



Related Tutorials