Copyright

Overriding Methods in Java: Definition & Example

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Overriding in Java is not a legal maneuver. It is a powerful tool that allows you to use existing classes and methods for specific purposes without posing a threat to those methods. This lesson will cover the concept and provide working code examples.

Overriding

One of the huge features of Java (and other object-oriented languages) is the concept of inheritance, or the ability to create a child class from a main class and inherit all variables and methods from the parent.

For example, if you have an 'Employee' class and want to make a 'UnionEmployee' class, you simply create a subclass that inherits all of the properties, variables, and methods within the 'Employee' class. The 'Employee' class has a method for calculating overtime and some variables; now these are all now within the new child class.

Here is a brief example of a child class.

public class Employee {
  double hoursWorked;
  public double calcOvertime() {
   return 1.5 * (hoursWorked - 40);
  }
}
public class UnionEmployee extends Employee {
}

Let's say that we like the overtime calculation method, but we want to add one just like it that subtracts the weekly candy bar allowance from overtime. The current method works just fine, so we won't mess with it.

Instead, we will override the method. When we perform an override, we are actually creating a new method with that exact same name. We'll have a different code within this method (otherwise, we'd just use the parent method). Remember: this new method MUST be in the new child class we created!

public class UnionEmployee extends Employee {
  public double calcOvertime() {
   return (1.5 * (hoursWorked - 40)) - candyBarAllowance;
  }
}

Note that the method has the same name, but it returns a different value. It's still a double, but there is different math going on.

Remember: when you override, you override the parent class method. Because you created a child class, it inherits all methods from the parent. A method with the same name as the parent overrides that parent class.

Super!

So, what if you still want to reference the method from the parent class? Now that we have an override on the method, we are free to use that new code. But we may also have an employee that doesn't have the candy bar allowance, so we need to run the original calculation method.

To reference the parent class method, use the super keyword:

//return super
return super.calcOvertime();

Putting it all Together

Let's look at a full block of code. (Yes, there is a lot going on!) The main method creates instances of each class. Each method is called, and even though the method names are the same, the results will be different because the method has been overridden.

Here is the code:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support