Java method overriding what is method overriding? how to achieve it?

 

Java method overriding

Q.1  what is method overriding? how to achieve it?

1.       Different concept with method overriding:-

2.       Return type of method overriding.

3.       Overriding and access –modifiers

4.       Overriding and Exception –handling

5.       Overriding and abstract –method

6.       Invoking  overridden  method from subclass

7.       Which method cannot override

8.       Overriding and synchronized (strictfp method|)

Example:-

class   Test   {

void  show()  {

System.out.println(“one”);

}
}

class Test1  extends Test  {

void show()  {

System.out.println(“two”);

}

public static void main(String  args[])   {

Test1   t1= new  Test1();

t1.show();

Test   t= new  Test();

t.show();

}

}

Example 2:-

class   Test   {

void  show(String  a)  {

System.out.println(“one”);

}
}

class Test1  extends Test  {

void show(String  a)  {

System.out.println(“two”);

}

public static void main(String  args[])   {

Test1   t1= new  Test1();

t1.show(“programming khata1”);

Test   t= new  Test(“programming khata”);

t.show();

}

}

Type of arguments same.
Example 3:-

class   Test   {

void  show(int  a,  String  b)  {

System.out.println(“one”);

}
}

class Test1  extends Test  {

void show(int a ,  float  b)  {

System.out.println(“two”);

}

public static void main(String  args[])   {

Test1   t1= new  Test1();

t1.show(45, “programming khata1”);

Test   t= new  Test(45, 45.76f);

t.show();

}

}
a sequence of arguments same.
use of method overriding:-
   method overriding allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclass or parent classes. the implementations in the sub-class override(replaces) the implementation in the superclass by providing a method that has the same name, same parameters or same signature, and same return type as the method in the parent class.



Post a Comment

0 Comments