answersLogoWhite

0


Best Answer

Any time you have a class that inherits a method from a superclass, you have the opportunity to override the method (unless, as you learned in the earlier chapters, the method is marked final). The key benefit of overriding is the ability to define behavior that's specific to a particular subclass type. The following example demonstrates a Porsche subclass of Car overriding the Car version of the drive() method:

public class Car {

public void drive() {

System.out.println("Generic Car Driving Generically");

}

}

class Porsche extends Car {

public void drive() {

System.out.println("Porsche driving Full Throttle");

}

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Overloading and overriding are two different things. Only class methods can be overridden but any function, including class methods, can be overloaded.

Overrides apply to class methods. Any virtual method of a base class can be overridden by a derived class. Virtual methods ensure that the most-derived override of that method is always executed first, even when the most-derived type is unknown to the caller -- as would be the case when that method is called from the base class itself. Non-virtual methods can also be overridden in order to mask the base class method.

Overloads are simply functions that have the same name in the same namespace that differ in the number or type of argument they accept. Class constructors are a typical example since it is often useful to provide default, copy and move constructors as well as conversion constructions. The same is also true of assignment operators.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

It means you replace the inherited method by some other code.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you overload or override a method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you override an instance method and make it final?

No. Once a method is declared final in a class, no derivative of that class can override that method.


Why can't method override be used without inheritance?

Overriding a method means that you are replacing an existing or virtual method that has already been defined in the parent object class, so without using inheritance, there can be no existing method to override.


Any method in a super class can be overridden in its subclass?

False.Any method declared as final cannot be overridden by any subclasses.You also cannot technically override a private method. While your subclass can have a method with the same definition as a private method in the superclass, it does not actually override that method.


What is the difference between overloading and overriding methods in object?

Here are some of the most common differences between both of them. If you are working in Java for more than 1 year, you might be familiar with all of them but any way its good revision: 1) First and major difference between Overloading and Overriding is that former occur during compile time while later occur during runtime. 2) Second difference between Overloading and Overriding is that, you can overload method in same class but you can only override method in sub class. 3) Third difference is that you can overload static method in Java but you can not override static method in Java. In fact when you declare same method in Sub Class it's known as method hiding because it hide super class method instead of overriding it. 4) Overloaded methods are bonded using static binding and Type of reference variable is used, while Overridden method are bonded using dynamic bonding based upon actual Object. 5) Rules of Overloading and Overriding is different in Java. In order to overload a method you need to change its method signature but that is not required for overriding any method in Java.


Why must you override the hashcode method while overriding the equals method?

Because, if two objects are supposed to be equal as per the equals() method, then the value returned by the hashCode() method must also be the same. This will not be the case if you override only the equals method and this can have some confusing effects when using those objects with hash related collections. So it is always a good idea to override the hashCode() method if you are providing an implementation for the equals method.

Related questions

Can you override an instance method and make it final?

No. Once a method is declared final in a class, no derivative of that class can override that method.


Can you override the life cycle methods of a JSP?

You cannot override the jspService() method but you can override the jspInit() and jspDestroy() methods


Differene betwee constructor overloading same as method overloading?

A constructor is just a special form of a method. You can overload constructors in the exact same way as you can overload any other method.


Why can't method override be used without inheritance?

Overriding a method means that you are replacing an existing or virtual method that has already been defined in the parent object class, so without using inheritance, there can be no existing method to override.


Any method in a super class can be overridden in its subclass?

False.Any method declared as final cannot be overridden by any subclasses.You also cannot technically override a private method. While your subclass can have a method with the same definition as a private method in the superclass, it does not actually override that method.


What is the difference between overloading and overriding methods in object?

Here are some of the most common differences between both of them. If you are working in Java for more than 1 year, you might be familiar with all of them but any way its good revision: 1) First and major difference between Overloading and Overriding is that former occur during compile time while later occur during runtime. 2) Second difference between Overloading and Overriding is that, you can overload method in same class but you can only override method in sub class. 3) Third difference is that you can overload static method in Java but you can not override static method in Java. In fact when you declare same method in Sub Class it's known as method hiding because it hide super class method instead of overriding it. 4) Overloaded methods are bonded using static binding and Type of reference variable is used, while Overridden method are bonded using dynamic bonding based upon actual Object. 5) Rules of Overloading and Overriding is different in Java. In order to overload a method you need to change its method signature but that is not required for overriding any method in Java.


Why must you override the hashcode method while overriding the equals method?

Because, if two objects are supposed to be equal as per the equals() method, then the value returned by the hashCode() method must also be the same. This will not be the case if you override only the equals method and this can have some confusing effects when using those objects with hash related collections. So it is always a good idea to override the hashCode() method if you are providing an implementation for the equals method.


What method do you override to initialize instance variables in a JSP declaration?

jsp init()


When do you override the finalize method?

when there is cleanup activity needed before an object is destroyed


What is first call in struts2 default constructor or validate method?

Default Constructor will be called first . If you override Validate method , then validate method will be called .


Why do you think that the Florida constitution provides a method for the legislature to override a governors veto?

Rx


Can override thread's run method?

Yes you can very well do... the run method is nothing but a Java method and all properties applicable to Java methods are applicable to it also.