Abstract Class Java

By Kunal Kapoor
19-04-2018
571

Abstract classes are those classes which are never directly called in main code. They are called by using the subclass of the abstract class. Eg if you are creating a code to define fixed variables, then you can use abstract class.


eg



1. office.java:


public class office {
public static void main(String[] args){
	
//	int i,j,k;
	
	area kk1=new area();
	area kk2=new area(50,60);
	area kk3=new area(90);
	
	area t;
	
	System.out.println("CASE 1 of default values");
	t=kk1;
	t.show_val();
	t.area_dis();
	
	System.out.println("Values manually loaded");
	t=kk2;
	t.show_val();
	t.area_dis();
	
	System.out.println("SQUARE");
	t=kk3;
	t.show_val();
	t.area_dis();
	
}
}

2. box.java:

abstract class box {

	int len,wid;
	//constructor if no values given. LOAD default values 
	box(){
		
		this.len=10;
		this.wid=20;
	}
	
	//constructor if value given
	box(int len,int wid){
		
		this.len=len;
		this.wid=wid;
	}
	
	//constructor if square
	box(int len){
		this.len=len;
		this.wid=len;
	}
	
	void show_val(){
		System.out.println("Value of constructor len and wid are : "+ this.len + " "+ this.wid);
	}
}

3. area.java:

 class area extends box{

	 int area;
	 
	 //constructor if no values case
	 area(){
		 super();
		 area=this.len*this.wid;
	 }
	 
	 //constructor if values given
	 area(int i,int j){
		 super(i,j);
		 area=this.len*this.wid;
	 }
	 
	 //constructor if square detected
	 area(int i){
		 super(i);
		 area=this.len*this.wid;
	 }
void area_dis(){
	System.out.println("Area : "+ this.area);
}
 
 }

output:
CASE 1 of default values
Value of constructor len and wid are : 10 20
Area : 200
Values manually loaded
Value of constructor len and wid are : 50 60
Area : 3000
SQUARE
Value of constructor len and wid are : 90 90
Area : 8100



Related Tutorials