www.000webhost.com
/** * Created by PhpStorm. * User: Rahul * Date: 12/10/2015 * Time: 2:54 AM */ OOP's Concept



Overriding

Core Java

Overriding in Java in Java


All the methods that parent class has are by default available to child class because of inheritance. If the child class method isn’t satisfied with its inherited method it is free to redefine based on its requirement. This is known as “OVERRIDING”.

Let us take an example to understand overriding better.


		class Parent
        {
                public void property()
                {
                        System.out.println("Cash + Land + Gold");
                }

                public void marry()
                {
                        System.out.println("Subbalaxmi");
                }
		}

		class Child extends Parent
        {
                public void marry()
                {
                        System.out.println("Samantha/Sonam/Emma");
                }
		}

		class Test
        {
                public static void main (String[] args)
                {
                        Parent p = new Parent();
                        p.marry();  //parent method: Subbalaxmi

                        Child c = new Child();
                        c.marry(); //Child method : Samantha/Sonam/Emma

                        Parent p1 = new Child();
                        p1.marry(); //Child method : Samantha/Sonam/Emma
                }
		}

Important poing to remember regarding Overriding in Java


1:- In overriding method names and argument types must be matched


i.e., method signatures must be same. The return types must be same in overriding upto java 1.4V but from 1.5v co-variant return types are allowed, that means child class method return type need not be same as parent class return type, its child types are also allowed.

Example :-

		class Parent
        {
        public double m1()
        {
        return null;
        }
		}

		class Child extends Parent
        {
        public int m1()
        {
        return null;
        }
		}

		class Test
        {
        public static void main(String[] args)
        {
        Child c = new Child();
        c.m1();
        }
		}
		//This is valid.

Note :- covariant return types are only applicable for object types but not for primitive types.
Example :-


		class Parent
        {
        public double m1()
        {
        return 11.5;
        }
		}

		class Child extends Parent
        {
        public int m1()
        {
        return 11;
        }
		}

		class Test
        {
        public static void main(String[] args)
        {
        Child c = new Child();
        c.m1();
        }
		}
		//This is invalid.
		/*Output would be :   error: m1() in Child cannot override m1() in Parent
		        public int m1()
		                   ^
		  return type int is not compatible with double */



2 :- Parent class private methods are not visible to child classes


Hence overriding concept isn’t applicable for private methods. But based on our requirement, we can define same parent class private method in child class, its valid but its not overriding.
Example :-

		class Parent
        {
        private void m1()
        {
            //any code
        }
		}

		class Child extends Parent
        {
        private  void m1();

        }
		// This is valid.


3 :- We cannot override parent class final methods.

		class Parent
        {
        public void property()
        {
        System.out.println("Cash + Land + Gold");
        }

			public final void marry()
{
System.out.println("Subbalaxmi");
			}
		}
		class Child extends Parent
        {
        public void marry()
        {
        System.out.println("Samantha/Sonam/Emma");
        }
		}

		class Test
        {
        public static void main (String[] args)
        {
        Parent p = new Parent();
        p.marry();  //parent method: Subbalaxmi

        Child c = new Child();
        c.marry(); //Child method : Samantha/Sonam/Emma

        }
		}
		/* OverridingDemo5.java:15: error: marry() in Child cannot override marry() in Pare
		nt
		                public void marry()
		                            ^
		  overridden method is final */


4 :- We can override parent class non-abstract method as abstract


We should override parent class abstract method in child class to provide implementation.

		class Parent
        {
        void m1()
        {
            //any code
        }
		}

		class Child extends Parent
        {
        abstract void m1();

        }
		// This is valid.

“synchronized”, “native”, “strictfp”, “abstract” modifiers won’t keep any restriction on overriding.




5 :- While overriding, we can increase scope of access-modifier but we can’t reduce


class Parent
{
public void m1()
{

}
}

 class Child extends Parent
 {
 protected void m1();
 }

//This is invalid
/* OverridingDemo7.java:11: error: m1() in Child cannot override m1() in Parent
        protected void m1();
                       ^
  attempting to assign weaker access privileges; was public
1 error  */


6:- Overriding concept is applicable only for methods but not for variables.


Variable resolution is always taken care by compiler based on “reference type” irrespective of the variables being static or non-static.

		class Parent
        {
        int x =777;
        }
		class Child extends Parent
        {
        int x =888;
        }
		class Test
        {
        public static void main(String []args)
        {
        Parent p = new Parent(); //777
        System.out.println(p.x);

        Child c= new Child();
        System.out.println(c.x); //888

        Parent p1= new Child();
        System.out.println(c.x); //888

        }
		}


7 :- We cannot override static method as non-static, vice-versa


		class P
        {
        public static void m1()
        {
        System.out.println("Hello!");
        }
		}
		class C extends P
        {
        public void m1()
        {
        System.out.println("Hello!");
        }
		}

		//Not valid
 

The overriding method can throw any unchecked exceptions regardless of whether the overridden method declares exception. The overriding method must not throw checked exceptions that are new or broader than those declared by the overridden method. The overriding method can throw narrower or fewer exceptions. Just because an overridden method “takes risks” that doesn’t mean that the overriding subclass exception takes same risks.i.e “an overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares”.



Coupling





Collections in Java
Previous Page




feel free to ask any of your doubts