Sunday, 11 September 2016

Method Overriding

Method Overriding:-
It is the process of providing replacement for existed method functionality with new functionality.
To perform method overriding, we must provide inheritance, where we must provide same method prototype for both super class method and sub class method.

Procedure to perform method overriding:-
a.       Declare a super class with a method, which we want to override.
b.      Declare a sub class and declare a method with the same super class method prototype and with different implementation.
c.       In main class, in main() method, create object for sub class and declare reference variable for super class.
d.      Access super class method and get output from sub class method.
Note:-
In java application, when we create object for sub class when JVM will override super class method with sub class method, here to provide method overriding we must access only super class method, but JVM must execute sub class method as per overriding. To active this requirements only, we have to create object for sub class and we have to declare reference variable for super class.
 Ex code 1:- 
class A
{
                void m1()
                {
                                System.out.println("m1-B");
                }
}
class B extends A
{
                void m1()
                {
                                System.out.println("m1-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                /*
                                A a=new A();
                                a.m1();
                                status:- Method overriding required sub class object
                                */
                                /*
                                B b=new B();
                                b.m1();
                                status: To provide method overriding, we must access super class method for this super class reference variable is required
                                */
                                A a=new B();
                                a.m1();                
                }
}
Output:-
m1-B

Ex code 2:-
class Loan
{
                public float getIR()
                {
                                return 5.0f;
                }
}
class Craft_Loan extends Loan
{
                public float getIR()
                {
                                return 3.0f;
                }
}
class Study_Loan extends Loan
{
                public float getIR()
                {
                                return 12.5f;
                }
}
class Gold_Loan extends Loan
{

}
class Test
{
                public Static void main(String[] args)
                {
                                Loan cl=new Craft_Loan();
                                System.out.println("Craft Loan IR:"+cl.getIR());
                                Loan sl=new Study_Loan();
                                System.out.println("Study Loan IR:"+sl.getIR());
                                Loan gl=new Gold_Loan();
                                System.out.println("Gold Loan IR :"+gl.getIR());
                }
}
Output:-


Rules to perform method overriding:-
1.       To override super class method with sub class method, super class method must not be declare as “private”.

Ex code:-

class A
{
                private void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                void m1()
                {
                                System.out.println("m1-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new B();
                                a.m1();
                }
}

Status: - Compilation Error.

2.       To override super class method with sub class method return type must be same as super class method return type otherwise compilation error will come.

Ex code:-

class A
{
                void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                int m1()
                {
                                System.out.println("m1-B");
                                return 10;
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new B();
                                a.m1();
                }
}
Output:- Compilation Error.

Ex code 2:-

class A
{
                int m1()
                {
                                System.out.println("m1-A");
                                return 20;
                }
}
class B extends A
{
                int m1()
                {
                                System.out.println("m1-B");
                                return 10;
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new B();
                                a.m1();
                }
}

Status:- No Compilation Error.
Output:- m2-B


3.       If we want to override super class method with sub class method then super class method must not be declared as “final” irrespective of sub class method “final”.

Ex code 1 :-

class A
{
                final void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                void m1()
                {
                                System.out.println("m1-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new A();
                                a.m1();
                }
}

Output:- Compilation Error.

Ex code 2:-

class A
{
                final void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                final void m1()
                {
                                System.out.println("m1-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new B();
                                a.m2();
                }
}

Status:- Compilation Error.

Ex code 3:-
class A
{
                void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                final void m1()
                {
                                System.out.println("m1-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new B();
                                a.m1();
                }
}
Output:-
m1-B
4.       In method overriding, either super class method or sub class method must not be declared as static, if we declare either super class method or sub class method as static then compiler will rise an error. If we declare both the method as static then compiler will not rise any Error, but JVM will execute super class method instead of executing sub class method, this process is not method overriding, this process in “method Hiding”.  

Ex code 1:-
class A
{
                static void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                void m1()
                {
                                System.out.println("m1-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new B();
                                a.m1();
                }
}
Status:- Compilation Error.

Ex code 2:-
class A
{
                void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                static void m1()
                {
                                System.out.println("m1-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new B();
                                a.m1();
                }
}
Status:- Compilation Error.

Ex code 3:-
class A
{
                static void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                static void m1()
                {
                                System.out.println("m1-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new B();
                                a.m1();
                }
}
Output:-
m1-A

5.       To override the super class method with sub class method then sub class method scope must be same as super class method scope or at least more then super class method scope.
6.       If we want to override super class method with sub class method then sub class method must have either some access privileges or weaker access privileges when compared with super class method access privileges.

Ex code 1:-
class A
{
                public void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                protected void m1()
                {
                                System.out.println("m1-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new B();
                                a.m1();
                }
}
Status:- Compilation Error.

Ex code2 :-
class A
{
                protected void m1()
                {
                                System.out.println("m1-A");
                }
}
class B extends A
{
                 public void m1()
                {
                                System.out.println("m1-B");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new B();
                                a.m1();
                }
}
Status:-  No Compilation Error.

Output:-  m1-B

No comments:

Post a Comment