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");

}

}

For abstract methods you inherit from a superclass, you have no choice. You must implement the method in the subclass unless the subclass is also abstract. Abstract methods must be implemented by the concrete subclass, but this is a lot like saying that the concrete subclass overrides the abstract methods of the superclass. So you could think of abstract methods as methods you're forced to override.

The Car class creator might have decided that for the purposes of polymorphism, all Car subtypes should have an drive() method defined in a unique, specific way. Polymorphically, when someone has an Car reference that refers not to an Car instance, but to an Car subclass instance, the caller should be able to invoke drive() on the Car reference, but the actual runtime object (say, a Porsche instance) will run its own specific drive() method. Marking the drive() method abstract is the Car programmer's way of saying to all subclass developers, "It doesn't make any sense for your new subtype to use a generic drive() method, so you have to come up with your own drive() method implementation!" A (non-abstract), example of using polymorphism looks like this:

public class TestCars {

public static void main (String [] args) {

Car a = new Car();

Car b = new Porsche(); //Car ref, but a Porsche object

a.drive(); // Runs the Car version of drive()

b.drive(); // Runs the Porsche version of drive()

}

}

class Car {

public void drive() {

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

}

}

class Porsche extends Car {

public void drive() {

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

}

public void brake() { }

}

In the preceding code, the test class uses a Car reference to invoke a method on a Porsche object. Remember, the compiler will allow only methods in class Car to be invoked when using a reference to a Car. The following would not be legal given the preceding code:

Car c = new Porsche();

c.brake(); // Can't invoke brake();

// Car class doesn't have that method

To reiterate, the compiler looks only at the reference type, not the instance type. Polymorphism lets you use a more abstract supertype (including an interface) reference to refer to one of its subtypes (including interface implementers).

User Avatar

Wiki User

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

Wiki User

14y ago

The advantage of overriding methods in Java is to allow those methods to handle multiple data types. This is significant in a few ways:

For the user of the method:

  • The user will feel like their data is being handled in a more accurate way. If a method accepts an Object type, how that method will react when given an ArrayList or a Line2D object can be unclear. More specific types reduce confusion (and errors).
  • The user will not have to convert their arguments before passing them in. For instance, if you have written a method that adds 5 numbers, and is overloaded to accept int and double parameters, the user will never have to convert their doubles to ints.

For the programmer of the method:

  • The guesswork of exactly what type of data you are working with is taken out of the equation. For instance, if your method accepts a general Collection, you could be dealing with an ArrayList or a LinkedList, which can result in a huge difference in performance.
  • The programmer can adapt the method to return different types of data. Math.abs() will return an int if passed in an int, and a double if passed in a double.

There are times however when overloading is unnecessary or even detrimental. For instance, it doesn't make sense to have a method that calculates P(n,r) (# permutations) accept floating point numbers.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the advantages of overriding method in java class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When do you declare a method or class abstract in java?

when overriding of a class or a method is necessary, they can be declared as abstract


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.


What is function overriding in Java?

Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding. In situations where you may have to explicitly call the parent class method you can use the "super" keyword and for explicitly calling the current objects method you can use the "this" keyword.


It is an error to have a method with the same signature in both the super class and its subclass. True or false?

False. A method with the same signature in both the superclass and its subclass is known as method overriding, and is a valid concept in Java.


What are overriding objectives?

The objective of overriding in Java is to provide features for a class to define its own behavior even for cases where the super class that it extends has already defined one. There might be cases where we want a specific behavior in our class but if the super class already has a method that does the same thing we wont be able to implement our behavior. Hence this overriding concept is available which lets us write our own implementation which would mask the code in the super class and let us run our logic.

Related questions

When do you declare a method or class abstract in java?

when overriding of a class or a method is necessary, they can be declared as abstract


What is method overriding and overloading in java?

Overloading is the means by which we can provide two or more different definitions of the same method in the same namespace. Overriding is the means by which a derived class may redefine the meaning of a base class method.


How you compare and contrast overloading and overriding methods in java?

Method overloading is when you have multiple methods in a class that have the same name but a different signature. Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding. In situations where you may have to explicitly call the parent class method you can use the "super" keyword and for explicitly calling the current objects method you can use the "this" keyword.


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.


What is function overriding in Java?

Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding. In situations where you may have to explicitly call the parent class method you can use the "super" keyword and for explicitly calling the current objects method you can use the "this" keyword.


It is an error to have a method with the same signature in both the super class and its subclass. True or false?

False. A method with the same signature in both the superclass and its subclass is known as method overriding, and is a valid concept in Java.


What is overriding method in java with simple example?

ye bohut mushkil sawa lhai


Why use function overriding in Java?

You use function overriding in Java when you inherit a bunch of features from a class and for a few particular cases alone, you do not wish to use the functionality of the parent class and wish to implement a custom feature in your class. In such cases, you create a method in your class with the same name and signature as in your parent class, thereby overloading it. this way only your current class will be used by the JVM unless specifically invoked by using the super keyword.


What is the difference between polymorphism and method overloading in java?

The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.Overriding and Overloading are two techiques to achive polymorphism in Java.Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading. Method overriding : When a method in a class having the same method name with same arguments is said to be method overriding.


What is the basic need of method overwritting in JAVA?

Maybe what you mean is method over loading. And that basically means that you can write different methods with the same name, which for example will allow you to call a method with few parameters, and let tat method fill the default values for the real thing. Sample private String my_method (String a, Integer b, SomethingElse c) { do your stuff... return "I did it"; } private String my_method (String a) { Integer defaultB = new Integer (5); SomethingElse defaultC = new SomethingElse (); return (my_method (a, defaultB, defaultC)); } .... // You can call: System.out (my_method("Who did that?")); There is no concept of Method Overwriting in Java. We have Method overloading & method Overriding. Overloading is already explained. If you are asking about Method Overriding pls read on... Overriding is a feature in Java where you can override (mask) the features of the parent class in the child class. Lets say you have a method printName() in the parent class which prints the name of that class. When you extend this class in a child class if you call the method printName() the name of the parent would be displayed. Which you may want to change. If you want to display only the name of the child class then you can write the method printName() in the child class. On invocation the name of the child class would be printed. Tip: If you want to display the name of the parent class somewhere you can invoke it by using super.printName() because once you override that method in the child class you cannot access the parent class method directly.


What are overriding objectives?

The objective of overriding in Java is to provide features for a class to define its own behavior even for cases where the super class that it extends has already defined one. There might be cases where we want a specific behavior in our class but if the super class already has a method that does the same thing we wont be able to implement our behavior. Hence this overriding concept is available which lets us write our own implementation which would mask the code in the super class and let us run our logic.


What will happen if a Java Class has no Main Method?

Nothing will happen. There is no restriction that every Java class must have a main method. The only program is that, this class cannot be executed as a standalone java program.