Overriding Concept In Java
Case :1:-Overriding
public class Parent {
public void property()
{
System.out.println("Cash+Land+Gold");
}
public void marry()
{
System.out.println("Arrange Marriage");
}
}
class Child extends Parent
{
public void marry()
{
System.out.println("Love Marriage");
}
}
class Test
{
public static void main(String[] args) {
Parent p=new Parent();
p.marry();//Arrange Marriage
Child c=new Child();
c.marry();//Love Marriage
Parent p1=new Child();
p1.marry();//Love Marriage
}
}
Case :2:-Overriding(return type May be different
–Covariant Type)
public class Parent
{
public Object
m1()
{
System.out.println("Object
");
return null;
}
}
class Child extends Parent
{
public String
m1()
{
System.out.println("String");
return null;
}
}
class Test
{
public static void
main(String[] args) {
Parent
p=new Parent();
p.m1();//Object
Child
c=new Child();
c.m1();//String
Parent
p1=new Child();
p1.m1();//String
}
}
Case :3:-Overriding(private method cannot Override)
public class Parent {
private void m1()
{
System.out.println("Parent Class Method");
}
}
class Child extends Parent
{
private void m1()///Compile
time Error(Overriding Concept not available for private method)
{
System.out.println("Child Class Method");
}
}
class Test
{
public static void main(String[] args)
{
Parent p=new Parent();
p.m1();//Compile
time Error(Overriding Concept not available for private method)
Child c=new Child();
c.m1();//Compile
time Error
Parent p1=new Child();
p1.m1();//Compile
time Error
}
}
Case :4:-Overriding(final method cannot Override)
public class Parent {
public final void m1()
{
System.out.println("Parent Class Final Method");
}
}
class Child extends Parent
{
public void m1()
{
System.out.println("Child Class Method");
}
}
Case :5:-Overriding(Must Be Override Bcz method is
abstract)
abstract public class Parent
{
public abstract void m1();
}
class Child extends Parent
{
public void m1()
{
System.out.println("Must be Override bcz Method is abstract");
}
}
class Test
{
public static void main(String[] args) {
//Parent p=new Parent();//Cannot Create Object bcz it isAbstract Class
Child
c=new Child();
c.m1();//Must be Override bcz Method is abstract
Parent
p1=new Child();
p1.m1();//Must be Override bcz Method is abstract
}
}
Case :6:-Overriding(We can override non abstract
method as abstract)
abstract public
class Parent
{
public void m1()
{
System.out.println(" We can stop the availability of parent method implementation to the next level child
classes ");
}
}
abstract class
Child extends Parent
{
public abstract
void m1();
}
Case :7:-Overriding(We can’t reduce the scope of
access modifier but we can increase the
scope.)
public class Parent
{
public void m1()
{
System.out.println(" Method Modifier is public");
}
}
class Child extends Parent
{
void m1()//compile time error
{
System.out.println("Method Modifier is Default");
}
}
Case :8:-Overriding(if child class method throws any
Checked Exception compulsory parent class method should throw the same checked
Exception or its parents otherwise we will get compile time error)
import
java.io.*;
public class
Parent
{
public void
m1() throws IOException
{
System.out.println(" ");
}
}
class Child extends
Parent
{
public void
m1() throws
EOFException,InterruptedException//compile time error
{
System.out.println(" ");
}
}
Case :9:-Overriding:- we cannot override static method
as non static otherwise we will get compile time error.
public class Parent
{
public static void m1()
{
System.out.println(" ");
}
}
class Child extends Parent
{
public void m1() //compile
time error
{
System.out.println(" ");
}
}
Case :10:-Overriding:- we cannot override non static
method as static otherwise we will get compile time error.
public class Parent
{
public void m1()
{
System.out.println(" ");
}
}
class Child extends Parent
{
public static void m1()
{
System.out.println(" ");
}
}
Case :11:-Overriding:- (Method Hiding)
public class Parent
{
public static void m1()
{
System.out.println(" Method Hiding Parent ");
}
}
class Child extends Parent
{
public static void m1()
{
System.out.println(" Method Hiding Child ");
}
}
class Test
{
public
static void
main(String[] args) {
Parent p=new Parent();
p.m1();// Method Hiding Parent
Child c=new Child();
c.m1();// Method Hiding Child
Parent p1=new Child();
p1.m1();// Method Hiding
Parent(Method resolution take care
by compiler on //the basis on reference
type
}
}
Case :12:-Overriding:- (Overriding With Respect to var
args method)
public class Parent
{
public void m1(int ...x)
{
System.out.println(" Parent
");
}
}
class Child extends Parent
{
public void m1(int x)
{
System.out.println("Child ");
}
}
class Test
{
public
static void
main(String[] args) {
Parent p=new Parent();
p.m1(10);// Parent
Child c=new Child();
c.m1(10);// Child
Parent p1=new Child();
p1.m1(10);// Parent it is
overloading but not overriding
}
}
Case :13:-Overriding:- (Overriding With Respect to
variable)
public class Parent
{
int x=888;
}
class Child extends Parent
{
int x=999;
}
class Test
{
public static void main(String[] args) {
Parent p=new Parent();
System.out.println(p.x);// 888
Child c=new Child();
System.out.println(c.x);//999
Parent p1=new Child();
System.out.println(p1.x);//888
}
}
//variable resolution always
takes care by compiler based on
Reference type.(Overriding concept aapplicable only for methods but not for
variables)
No comments:
Post a Comment