Method Overriding in JAVA all test Case in One and Covariant Return Type
Method Overriding in JAVA all Test Case by Changing Return Type or Method overriding all test cases using Covariant Return Type.
Method Overriding:
Method overriding in Java is a concept based on polymorphism OOPS concept which allows the
programmer to create two methods with the same name and method signature in the base and derived class and the actual method is called at runtime depending upon the type of an object at runtime.
Some Rules of Method Overriding and Advantages of Method Overriding and How to Overcome Method Overriding.
1:) You can not override the method in the same class.
2:) In Method overriding name and signature of a method must be the same in Super class and Sub class or an interface and its implementation.
3:)Overridden method is called using dynamic binding in Java at runtime based upon the type of Object
4:)If you are extending an abstract class or implementing interface than you need to override all abstract methods unless your class is not Abstract.
5:)Always use @Override annotation while the overriding method in Java for the best Java coding practice.
6:)Advantages of Method Overriding is that the class can give its own specific implementation to a Child Class method without any modifying the Parent class method.
7:)We overcome this by use of 'SUPER' keyword.
Simple Example of Method Overriding Example in Java from below Test Case you can choose your return type and check whether it's working or not.
Method Overriding:
Method overriding in Java is a concept based on polymorphism OOPS concept which allows the
Method Overriding in java in Interview question |
Some Rules of Method Overriding and Advantages of Method Overriding and How to Overcome Method Overriding.
1:) You can not override the method in the same class.
2:) In Method overriding name and signature of a method must be the same in Super class and Sub class or an interface and its implementation.
3:)Overridden method is called using dynamic binding in Java at runtime based upon the type of Object
4:)If you are extending an abstract class or implementing interface than you need to override all abstract methods unless your class is not Abstract.
5:)Always use @Override annotation while the overriding method in Java for the best Java coding practice.
6:)Advantages of Method Overriding is that the class can give its own specific implementation to a Child Class method without any modifying the Parent class method.
7:)We overcome this by use of 'SUPER' keyword.
Simple Example of Method Overriding Example in Java from below Test Case you can choose your return type and check whether it's working or not.
// Method Overriding class Parent { // parent Class overridden methods yourReturntype methodName() { System.out.println("In Parent Class Method"); } } class Child extends Parent { // child Class overriding method @Override yourReturntype methodName() { System.out.println("In child Class Method"); } } // Driver class class CallingClass { public static void main(String[] args) { Parent obj = new Child(); obj.methodName(); } }
This some TestCase of Method Overriding in Java.
Parent Method | Child Method | Compile and Run | Description |
---|---|---|---|
Object | String | YES | An object is a parent of String(Relationship) |
Number | Integer | YES | Relationship |
Integer | Number | NO | An Integer is a child class of Number. |
Object | Boolean, Double, String All wapper class StringBuilder, StringBuffer |
YES | An Object is a Parent Class of ALL |
String | StringBuffer Or StringBuilder | NO | No Relationship |
StringBuffer | StringBuffer | YES | Both Class is Same |
StringBuilder | StringBuilder | YES | Both Class is Same |
Object | Primitive Type(int,float,double,etc) | NO | No Relationship |
All Wrapper Class(Integer,Double,etc) | Primitive Type | NO | No Relationship |
Primitive Type | Primitive Type | YES | Both Should Primitive Type |
Wrapper Class(Boolean) | Wrapper Class(Boolean) | YES | Both Class is Same |
Protected | Either Public or Protected | YES | Access Specifier |
Public | Public | YES | Access Specifier |
Public | Protected | NO | Access Specifier |
Default | Public, Protected, Default | YES | Must Not Be Private |
Static | Static | NO | No Overriding but its Method Hiding |
Static | Non Static | NO | Compile Time Error |
This is some test case of method Overriding which interview ask to you if you prepare all this test case you will easily crack method overriding interview questions if you like this please do comment
Post a Comment