Oveririding rules
Rules for overriding
- The access level can't be more restrictive than the overridden method's.
- class Car {
String message = "Car";
<T> void horn() throws Exception {
System.out.println(message);
}
}
class Mercedez extends Car {
String message = "child";
void horn() throws IOException {
System.out.println(message);
}
} - Compiles fine
- class Car {
String message = "Car";
public void horn() throws Exception {
System.out.println(message);
}
}
class Mercedez extends Car {
String message = "child";
private void horn() throws IOException {
System.out.println(message);
}
} - Not Allowed
- class Car {
String message = "Car";
void horn(){
System.out.println(message);
}
}
class Mercedez extends Car {
String message = "child";
<T> void horn(){
System.out.println(message);
}
} - Not Allowed
- class Car {
String message = "Car";
Number horn(){
return 1;
}
}
class Mercedez extends Car {
String message = "child";
Integer horn(){
return 2;
}
}
public class OverridingWithException {
public static void main(String[] args) {
Car c=new Mercedez();
System.out.println(c.horn());
}
} - Output: :2
- The access level CAN be less restrictive than that of the overridden method.
- private, static and final methods can't be overridden
- Return type of method is never part of method signature, so only changing the return type of method does not mean overloading
- Overriding method can not throw checked Exception higher in hierarchy than thrown by overridden method.
- class Car {
String message = "Car"; //Overriden method
void horn() throws NoHornException {
System.out.println(message);
}
}
class Mercedez extends Car {
String message = "child";//Overriding method
void horn() throws NoBreakException {
System.out.println(message);
}
}
class NoHornException extends Exception {
}
class NoBreakException extends NoHornException {
}
public class OverridingWithException {
public static void main(String[] args) {
try {
new Mercedez().horn();
} catch (NoBreakException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}- which overridden method to call based on object type
- import java.io.FileNotFoundException;
import java.io.IOException;
class Parent {
String message = "parent";
void say() {
System.out.println(message);
}
}
class Child extends Parent {
String message = "child";
void say() {
System.out.println(message);
}
}
public class OverridingWithException {
public static void main(String[] args) {
Parent p=new Child();
p.say();
}
}Output is CHILD
- class Car {
String message = "Car";
void horn() throws Exception{
System.out.println(message);
}
}
class Mercedez extends Car {
String message = "child";
void horn() throws IOException,FileNotFoundException{
System.out.println(message);
}
}
Compiles fine
Imagine Base class car references Object of derived class
and invokes the method in base class then the call is made
and base class method invoked.
class Car { String message = "Car"; void horn() { System.out.println(message); } } class Mercedez extends Car { String message = "Mercedez "; /*void horn() { System.out.println(message); }*/ } public class OverridingWithException { public static void main(String[] args) { Car yo = new Mercedez(); yo.horn(); } } Output:CarBut if i uncomment methodin Mercedez then
Output:Mercedez
_________________________________________________________________________
class Car {
String message = "Car";
static void horn() {//NOT ALLOWED
System.out.println(message);
}
}
class Mercedez extends Car {
String message = "Mercedez ";
/*void horn() {
System.out.println(message);
}*/
}
public class OverridingWithException {
public static void main(String[] args) {
Car yo = new Mercedez();
yo.horn();
}
}
___________________________class Animal {
public void eat() {
System.out.println("Generic Animal Eating Generically");
}
}
class Horse extends Animal {
public void eat() {
System.out.println("Horse eating hay, oats, "
+ "and horse treats");
}
public void fight() { }
}
}
In the preceding code, the test class uses an Animal reference to invoke a method on a Horse object. Remember, the compiler will allow only methods in class Animal to be invoked when using a reference to an Animal. The following will not be alllowed:
Animal c = new Horse(); c.fight(); // Can't invoke fight(); // Animal class doesn't have that method ****The overriding method can throw any unchecked excpetion regardless of what exception overridden method is throwing.
Overriding Puzzle
class Animal {
public void eat() {
System.out.println("Generic Animal Eating Generically");
}
}
class Horse extends Animal {
public void eat() {
System.out.println("Horse eating hay, oats, "
+ "and horse treats");
}
public void fight(String weapon) {
}
}
public class TestA {
public static void main(String[] args) {
Animal a=new Animal();
a.eat();
Animal aObj=new Horse();
aObj.fight("Nails");//Compiler Error it looks for the reference Animal for definition of fight() and it wont get So Error
Horse h=new Horse();
h.fight("Nails");//Allowed
}
}
Overriding method can not throw checked Exception higher in hierarchy than thrown by overridden method.
Lets take one example
class Car
{
public void accelarate() throws Exception
{
//Throw exception
}
}
class Mercedez extends Car
{
public void accelarate(){ //No exceptions
}
}
public class OverridingWithException extends Animal{
public static void main(String[] args) {
Car c=new Mercedez();
Mercedez carObj=new Mercedez();
carObj.accelarate();//THis is fine
c.accelarate();//Will throw exception.
//since the derived class Mercedez does not thorws exception it is not valid
}
}
If class Mercedez defines a method throws
checked Exception not defined by class Car
Then it is not allowed.
import java.io.FileNotFoundException;
import java.io.IOException;
class Car
{//Overriden method
public void accelarate() throws FileNotFoundException
{
//Throw exception
}
}
class Mercedez extends Car
{
//Overriding method
public void accelarate() throws IOException{ //No exceptions
}
}

0 comments: