Thursday 27 April 2017

Superclass and Subclass in Java

Superclass and Subclass in Java


The superclass of a class A is the class from which class A is derived. In programming languages like C++, allow deriving a class from multiple classes at a time. When a class inherits from multiple super classes, the concepts is known as multiple inheritance. Java doesn’t support multiple inheritance. If there is a need to implement multiple inheritance.

A class derived from the superclass is called the subclass. Sometime-superclass is also called parent class or base class and subclass is called as child class or derived class. The subclass can reuse the data member and methods of the superclass that were already implemented and it can also extend or replace the behaviour in the superclass by overriding methods. Subclass can have its own data members and member functions.
You can see in this example program, the Employee class is used for tracking the hours an employ worked for along with the hourly wages, and the “attitude” which gives you a rough measure of their activeness or for what percentage of time they are actually productive.

public class Employee
{
protected double attitude;
protected int numHoursPerWeek, wagePerHour;
public Employee(int wage, int hours, double att) // constructor
{
wagePerHour = wage;
numHoursPerWeek = hours;
attitude = att;
}
public double getProductivity()
{
return numHoursPerWeek*attitude;
}
public double getTeamProductivity()
{
return getProductivity();
}
public int WeekSalary()
{
return wagePerHour*numHoursPerWeek;
}
}

If you look closely you will observe that Employee class possesses the very basic characteristics of an employee. So think quickly about different type of employees! Of course you can think about employees with special characteristics, for example, Manager Engineer, Machine-man etc. You are right Subclass of Employee, will have properties of Employee class as well as some more properties.


Visit our site for java training in jaipur

0 comments:

Post a Comment