Advertisements
Home > Java > Java Program to show Class Inheritance – Q23

Java Program to show Class Inheritance – Q23


Q23: Java Program to create a worker, hourly worker and salaried worker. Make compute-sal method in hourly worker and salaried worker which inherit the woker class and pass number of hours worked as argument. Two variables (name and hourly-wage) are there in the class which will be initialized by constructor.

Class Inheritance is when an object or class is based on another class, using the same implementation or specifying a new implementation to maintain the same behavior.

class worker{
	String name;
	int hrs_wage;
}

class hr_worker extends worker{
	int hrs;
	hr_worker(String n, int h_w){
		name = n;
		hrs_wage = h_w;
	}
	void compute_sal(int hrs){
		System.out.println("Worker Name: " + name);
		System.out.println("Salary: " + hrs_wage*hrs);
	}
}

class sal_worker extends worker{
	sal_worker(String n, int h_w){
		name = n;
		hrs_wage = h_w;
	}
	void compute_sal(){
		System.out.println("Worker Name: " + name);
		System.out.println("Salary: " + hrs_wage*250);
	}
}

class work_pay{
	public static void main(String args[]){
		System.out.println("\n Hourly Worker Salary:-");
		hr_worker O_h_w = new hr_worker("Nitin Kapoor", 20);
		O_h_w.compute_sal(150);
		
		System.out.println("\n Salaried Worker Salary:-");
		sal_worker O_s_w = new sal_worker("Manoj Pandey", 30);
		O_s_w.compute_sal();		
	}
}

… from College notes (BCA/MCA assignments):


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: