Polymorphism program
Polymorphism theory
Polymorphism Error Program
Compile time polymorphism program
1.Can we achieve method overloading by changing the return type of method only?
Ans:- yes ,we can have any number of main method overloading.This is because JVM always calls main() method which receives string arrays as arguments only.
MainMethodOverload1
or
Method overriding
Case 1
1.Do overring method must have same return type (or subtype)?
From java 5.0 onwords it is possible to have different return type for a overriding method in child class , but child’s return types should type of parents return type. This phenomena is known as covariant return type .
Case 2
Q2 :- overriding and Access- modifiers
Ans:- the access modifier for an overriding method can allow more ,but not less, access than the overridden method.For example, a protected instance method in the super-class can be made public .but not private ,in the subclass. Doing so , will generate compile-time error.
Method overloading
No of arguments
1.![]() |
Program No:- 1 |
![]() |
Program :-2 |
Method overriding
public class Test {void show(){System.out.println("method 1");}}class xyz extends Test{void show(){System.out.println("method 2");}public static void main(String[] args) {Test obj =new Test();obj.show();}}
Output
method 1
Program No:- 2
public class Test {
String show(){
System.out.println("method 1");
return null;
}
}
class xyz extends Test{
String show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
}
}
output
method 1
Program No:- 3
public class Test {
int show(){
System.out.println("method 1");
return 0;
}
}
class xyz extends ex9{
int show(){
System.out.println("method 2");
return 0;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
}
}
output
method 1
Program No:- 4
public class Test {
Object show(){
System.out.println("method 1");
return null;
}
}
class xyz extends Test{
String show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
}
}
output
method 1
Program No:- 5
public class Test {
Object show(){
System.out.println("method 1");
return null;
}
}
class xyz extends Test{
StringBuffer show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
}
}
output
method 1
Program No:- 6
public class Test {
Object show(){
System.out.println("method 1");
return null;
}
}
class xyz extends Test{
StringBuilder show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 7
public class Test {
Object show(){
System.out.println("method 1");
return null;
}
}
class xyz extends Test{
Number show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 8
public class Test {
Object show(){
System.out.println("method 1");
return null;
}
}
class xyz extends Test{
Character show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 9
public class Test {
Object show(){
System.out.println("method 1");
return null;
}
}
class xyz extends Test{
Boolean show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 10
public class Test {
Number show(){
System.out.println("method 1");
return 0;
}
}
class xyz extends Test{
Byte show(){
System.out.println("method 2");
return 0;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 11
public class Test {
Number show(){
System.out.println("method 1");
return 0;
}
}
class xyz extends Test{
Short show(){
System.out.println("method 2");
return 0;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 12
public class Test {
Number show(){
System.out.println("method 1");
return 0;
}
}
class xyz extends Test{
Integer show(){
System.out.println("method 2");
return 0;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 13
public class Test {
Number show(){
System.out.println("method 1");
return 0;
}
}
class xyz extends Test{
Long show(){
System.out.println("method 2");
return 0;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
Program No:- 13
public class Test {
Number show()
System.out.println("method 1");
return 0;
}
}
class xyz extends Test{
Long show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 13
public class Test {
Number show(){
System.out.println("method 1");
return 0;
}
}
class xyz extends Test{
Float show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 14
public class Test {
Number show(){
System.out.println("method 1");
return 0;
}
}
class xyz extends Test{
Double show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 15
Overriding and Access-Modifierspublic class Test {
Object show(){
System.out.println("method 1");
return null;
}
}class xyz extends Test{
public String show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 16
public class Test {
Object show(){
System.out.println("method 1");
return null;
}
}class xyz extends Test{
protected String show(){
System.out.println("method 2");
return null;
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 17
public class Test {
public void show(){
System.out.println("method 1");
}
}class xyz extends Test{
public void show(){
System.out.println("method 2");
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 17
public class Test {
protected void show(){
System.out.println("method 1");
}
}class xyz extends Test{
protected void show(){
System.out.println("method 2");
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
Program No:- 17
public class Test {
protected void show(){
System.out.println("method 1");
}
}class xyz extends Test{
public void show(){
System.out.println("method 2");
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
overriding & Exception Handling
Program No:- 18
output
method 1
method 2
Program No:- 19
Program No:- 20
public class Test {
void show() throws RuntimeException{
System.out.println("method 1");
}
}class xyz extends Test{
void show() throws RuntimeException
{
System.out.println("method 2");
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
output
method 1
method 2
Program No:- 21
public class Test {
void show() throws RuntimeException{
System.out.println("method 1");
}
}class xyz extends Test{
void show() throws ArithmeticException
{
System.out.println("method 2");
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
Program No:- 22
public class Test {
void show() throws RuntimeException{
System.out.println("method 1");
}
}class xyz extends Test{
void show()
{
System.out.println("method 2");
}
public static void main(String[] args) {
Test obj =new Test();
obj.show();
xyz obj2 =new xyz();
obj2.show();
}
}
Program No:- 22
Case 4 :- overriding and Abstract Method
Program No:- 23
Program No:- 24
output
method 2
output
method 2
output
method 2
Program No:- 29
abstract class Test {
void show()
{ System.out.println("method 1");
}
}class xyz extends Test{
void show() {
super.show();
System.out.println("method 2");
}
public static void main(String[] args) {
xyz obj2 =new xyz();
obj2.show();
}
output
method 1
method 2
Program No:- 30
Program No:- 31
Program No:- 32
synchronized and stricfp
Program No:- 33
class Test {
synchronized void show()
{ System.out.println("method 1");
}
}class xyz extends Test{
void show() {
System.out.println("method 2");
}
public static void main(String[] args) {
Test obj2 =new Test();
obj2.show();
}
}
output
method 1
Program No:- 33
class Test {
strictfp void show()
{ System.out.println("method 1");
}
}class xyz extends Test{
void show() {
System.out.println("method 2");
}
public static void main(String[] args) {
Test obj2 =new Test();
obj2.show();
}
}
output
method 1
Program No:- 1
Program No:- 2
Program No:- 3Program No:- 4
Program No:- 5
Program No:- 5
Overriding and Access-Modifiers
Comments
Post a Comment