Overloading vs. Overriding in Java

Instructor: Martin Gibbs

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

Overload or override? These similar-sounding concepts are actually quite different. This lesson will compare the two in the framework of classes and methods. Working code examples will be provided to highlight each function.

A Review of Classes and Methods.

Before we get into the detail of overloading and overriding, let's quickly cover the idea of classes, sub-classes, and methods.

A class in Java is a set of plans for a given object. For example, there could be a class called Products. All future instances of the Products class will have all the information from the main class. If you create a book product or a bicycle product, they all gain items from the main Products class.

Included within the class are methods. These are examples of methods or actions: update price, set quantity, or retrieve UPC codes. Methods are defined inside classes.

A subclass, or child class, inherits all variables and methods from the parent class. You can define your own methods and variables within the subclass, but you still have access to the parent class's methods. As an example, for the class Products, we could create a subclass called CorrosiveProducts to distinguish these items. You tell Java that the class is a child by using the extends keyword and identifying the parent class.

The following code creates both classes. Right now, the CorrosiveProducts class has no other code since it inherits everything from Products.


public class Products {
 public void updateProduct(long itemID) {
  //this is a method
 }
}
public class CorrosiveProducts extends Products {
 //this is a child class
}


Now that we've established our class and sub-class, let's go into more detail on overloading and overriding.

Overloading

When you overload a method, you're actually creating another method with the same name. Each of the new methods has different parameters. These are the values that the method can accept and work with. If you have a method to update price, it would make sense to give the method a quantity so that it knows what to give back for price.


public class Products {
 public void updateProduct(long itemID) {
  //update product only itemID
 }
 public void updateProduct(long itemID, double price) {
  //update with ID and price
 }
}


Overriding

Remember that a subclass, or child class, inherits the methods and variables from the parent. In the case of our sample classes, the CorrosiveProducts class inherits both methods that were defined in the Products class. You can always use these methods within either class.

We created the child class in order to have a slightly different twist on the parent class. The same is true for the method. We can use the same method(s) within the child class, but perform different actions. When we alter a parent class's method inside a child class, that is called overriding.

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