Overriding Rules - rahul00773/JavaConcepts GitHub Wiki

Rules for OverRiding

  1. In Overriding Method names and arguments, types must be matched. That is method signatures must be the same.
  2. In Overriding Return types must be the same but this rule is applicable until 1.4 version only. From 1.5 version onwards we can take co-variant return types. According to this child class method return type need not be the same as parent method return type. Its child type also allowed.

Example:

class P{

public Object m1(){

return null; } }

class C extends P{

public String m1(){

return null; }

}

// It is invalid in 1.4 version but from 1.5 version onwards it is valid.

ParentClass and Child Class. Parent Class. Parent Class Now Child Class is in Parent Primitive Data type
Parent Class Method Return Type Number Object String Double
Child Class Method return Type Number/Integer. String/String Buffer/.. Object Int
Result Valid Valid Invalid (Compile time error) Invalid (Compile time error)

# Co-variant return type concept applicable only for objects types but not for primitive types

  1. Parent class private methods not available to the child. And hence overriding concepts not applicable for private methods. Based on our requirement we can define exactly the same private method in child class. It is valid but not overriding.

class P{ private String m1(){

return null; }

}

Class C extends P{

private String m1(){ /// It is valid but not overriding

return null; }

}

  1. We can’t override parent class final method in child classes. If we are trying to override we will get a compile-time error.

class P{ public void final m1(){

return null; }

}

class C extends P{

public void m1(){ /// Compile time error will come: m1() in C cannot override m1() in P;

			`//// overridden method is final`

}

}

  1. Parent class abstract method we should override in a child class to provide the implementation.

abstract class P{

public abstract void m1(){

}

}

public class C extends P{

public void m1(){

} }

  1. We can override the Nonabstract method as abstract.

class P{

public void m1(){

} }

abstract class C extends P{

public abstract m1(){ }

}

The main advantage of this approach is that we can stop the availability of parent method implementation to the next level child classes.

  1. In overriding, the following modifiers won’t keep any restrictions

  2. Synchron

  3. Native

  4. StrictFp

Parent Child Method Type Method Type . Method Type Method Type. Method Type . Method Type
Parent Method Final Non-Final NonAbstract/Abstract Sync/NonSyb Native/Non-native. StrictFp/NonStrictFp.
Child Method. Non/Final Final Abstract/NonAbstract NonSyn/Sync Nonnative/Native NonStrictFp/StrictFp
Status Not Valid Valid Valid Valid Valid Valid
7. When overriding we can’t reduce scope of access modifier. But we can increase the scope 

class P{

public void m1(){

} }

class C extends P{

Void m1(){. /// CE time error : m1() in C can’t override m1() in P. Attempting to assign weaker access privileges was public }

}

private<default<protected<public

Parent Child Access Modifier. Access Modifier. Access Modifier. Access Modifier
Parent Class. Public Protected. Default. Private
Child Class. Public Protected/Public Default/Protected/Public Overriding concept is not applicable for private method
Status Invalid