answersLogoWhite

0

Overridden Methods

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).

The overriding method cannot have a more restrictive access modifier than the method being overridden (for example, you can't override a method marked public and make it private). Think about it: if the Car class advertises a public drive() method and someone has an Car reference (in other words, a reference declared as type Car), that someone will assume it's safe to call drive() on the Car reference regardless of the actual instance that the Car reference is referring to. If a subclass were allowed to sneak in and change the access modifier on the overriding method, then suddenly at runtime-when the JVM invokes the true object's (Porsche) version of the method rather than the reference type's (Car) version-the program would die a horrible death. Let's modify the polymorphic example we saw earlier in this section:

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 {

private void drive() { // whoa! - it's private!

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

}

}

If this code compiled (which it doesn't), the following would fail at runtime:

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

// object , so far so good

b.drive(); // Chaos at runtime!

The variable b is of type Car, which has a public drive() method. But remember that at runtime, Java uses virtual method invocation to dynamically select the actual version of the method that will run, based on the actual instance. A Car reference can always refer to a Porsche instance, because Porsche IS-A Car. What makes that superclass reference to a subclass instance possible is that the subclass is guaranteed to be able to do everything the superclass can do. Whether the Porsche instance overrides the inherited methods of Car or simply inherits them, anyone with a Car reference to a Porsche instance is free to call all accessible Car methods. For that reason, an overriding method must fulfill the contract of the superclass.

The rules for overriding a method are as follows:

• The argument list must exactly match that of the overridden method. If they don't match, you can end up with an overloaded method that you didn't intend on creating.

• The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass.

• The access level can't be more restrictive than the overridden method's. (public to private not allowed)

• The access level CAN be less restrictive than that of the overridden method. (private to public allowed)

• Instance methods can be overridden only if they are inherited by the subclass. A subclass within the same package as the instance's superclass can override any superclass method that is not marked private or final. A subclass in a different package can override only those non-final methods marked public or protected (since protected methods are inherited by the subclass).

• The overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception.

• The overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method. For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it's a subclass of FileNotFoundException.

• The overriding method can throw narrower or fewer exceptions. Just because an overridden method "takes risks" doesn't mean that the overriding subclass' exception takes the same risks. Bottom line: an overriding method doesn't have to declare any exceptions that it will never throw, regardless of what the overridden method declares.

• You cannot override a method marked final.

• You cannot override a method marked static.

• If a method can't be inherited, you cannot override it. Remember that overriding implies that you're re-implementing a method you inherited! For example, the following code is not legal, and even if you added an drive() method to Porsche, it wouldn't be an override of Car's drive() method.

public class TestCars {

public static void main (String [] args) {

Porsche h = new Porsche();

h.drive(); // Not legal because Porsche didn't inherit drive()

}

}

class Car {

private void drive() {

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

}

}

class Porsche extends Car { }

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

What is overriding method in java with simple example?

ye bohut mushkil sawa lhai


Is overriding a dynamic polymorphism in c plus plus or not?

In C++, overriding and function, method, or operator is a different thing than (dynamic) polymorphism, so overriding a polymorphic method is almost entirely possible.


Is it possible to implement method overriding in each and every type of inheritance?

Yes. Method Overriding is not possible without inheritance and it can be done in all possible types of inheritance.


How is hiding method different from overriding method in c sharp?

Hiding means a class cannot see the definition. Overriding implies that a class must see that to "override"


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 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.


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


Is it possible to do method overloading and overriding at a time?

no we cannot do both at the same time because overloading requires different arguments and overriding dosenot


What is function overriding in oop?

Function overriding in object-oriented programming (OOP) occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. This allows the subclass to customize or extend the behavior of the inherited method. The overridden method in the subclass must have the same name, return type, and parameters as the method in the superclass. Function overriding is a key mechanism for achieving polymorphism, enabling dynamic method resolution at runtime.


What is overriding in c?

Overriding relates to derived classes, where the derived class provides a new implementation for a method declared in the base class. The override is said to be a more-specialised implementation of the base class method, which is itself described as being a generic method. However, the derived class method can still call the base class method, if required.When the designer of a class can predict that their class will be derived from, they will normally provide virtual methods. These methods are expected to be overridden by the derived class. Overriding a non-virtual method can have side effects if the method is also overloaded. Overriding just one overloaded method will effectively hide all the other overloads in the base class, which may be undesirable.


Is method overridding is polymorphism?

Yes. Method Overriding is a form of Polymorphism.Overridden MethodsAny 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");}}


What is the purpose of method overriding?

Assuming class A has a method named getXXX() and class B is a sub class of class A. Now, if we write a method with the same name getXXX() in class B, with exactly the same signature as class A, it is called overriding a method. The method getXXX() in class A becomes the overridden method.