Looking For Anything Specific?

ads header

Overriding in JAVA || OOPS CONCEPT

OOPS CONCEPT 
Overriding in JAVA 


Every parent works hard because have to look after their child. so they earn money and assets.

now observe, they give property like cash and land and gold. most of the parents arrange a marriage for their kids. here parents defined two methods one is the property and another one is married. let's move to write code.

class P{

    public void property()

{

        System.out.println("cash$ +Land + Gold" );

}


public void marry ()

{

        System.out.println("Mahalaxmi");

}

}


whatever method parent class has, by default available in child class through inheritance here, we are very satisfied with the property implementation but not satisfied with the marry implementation. then the child is allowed to redefine the method based on its requirements.


class C extends P {

    public void marry ()

        {

                System.out.println("anupama / pranitha");

        }

}


so child class is not satisfied with parent class implementation. then the child is allowed to redefine the method based on its requirements. this process is called Overriding.

the parent class method which is overridden is called overridden method. and the child class method which is overriding is called the overriding method.

class Test 

{

public static void main (String args [])

{

P p = new P ();

p.marry();  // Parent method 

C c = new C();

c.marry();   // Child method 

P p1= new C();

p1.marry();  // child method 

}

}


Output 


In overriding method resolution always tasks care by JVM based on runtime object and hence overriding is also considered as runtime polymorphism or dynamic polymorphism  or late binding 



Rules for overriding 


1. In the overriding method names and argument types must be matched that is method signature must be the same 


2.In overriding return types must be the same but this rule is applicable until 1.4 v only from 1.5.v onwards we can take covariant reut=rn types according to this child class method return types need not be the same parent method return type, its child type also allowed.


covariant return type concept applicable only for object types not for primitive types 
3. parent class private methods are not available to the child, hence overriding the concept of private methods.
based on our requirements  we can define an exactly same private method in chid class it is valid but not overriding 
Example
class P
    {   
            private void m1()
    {
    }                                                                //Here it is valid but not overriding
}
class C extends P {
private  void m1(){
}
}

4. We can't override parent class final methods in child classes if we try to override .we will get a compile-time error.

5. parent class abstract method we should override in child class to provide implementation.
abstract class P {
public abstract void m1();
}

class C extends P {
public void m1 ()
    {
    }
}

5.while overriding we cant reduce the scope of access of the modifier but we can increase the scope.

class P 
{
    public void m()
    {
    }
}
                                                             // This is error because here we are decreasing the scope 
class C extends P 
{
    void m()
    {
    }
}




class P 
{
        void m()
    {
    }
}
                                                            // This is Valid because here we are Increasing  the scope 
class C extends P 
{
    public void m()
    {
    }
}


ascending order of increasing the scope 

private <default <protected<public 







Post a Comment

0 Comments