Skip to main content
Rollback to Revision 2
Link
Termininja
  • 7.1k
  • 12
  • 51
  • 50

How Java Polymorphism: Please Help Me to Understand Java Polymorphism?

edited title
Link
Termininja
  • 7.1k
  • 12
  • 51
  • 50

Java Polymorphism: Please Help Me How to Understand Java Polymorphism?

added 195 characters in body
Source Link
Akash
  • 199
  • 1
  • 4
  • 12

Polymorphism is working perfectly fine for the following code:

class ClassA { String whoAmI() { return "ClassA"; } } class ClassB extends ClassA{ String whoAmI() { return "ClassB"; } } class Main { public static void main(String[] args) { ClassA obj1 = new ClassA(); ClassA obj2 = new ClassB(); System.out.println(obj1.whoAmI()); System.out.println(obj2.whoAmI()); } } 

Output of the code above is:

ClassA ClassB 

ButIn the code above output is as expected that I can see the methods of class "ClassB" when I am running following code then Compilercreating a reference variable of class "ClassA" and instantiating with "new ClassB();", but why it is giving exceptionnot same in case when I creating the reference variable of interface "List" and instantiating it by using its implementing class "ArrayList" (I know that we can't create objects of interfaces directly)?

import java.util.ArrayList; import java.util.List; public class ClassAMain { Stringpublic whoAmIstatic void main(String[] args) {    return "ClassA";  List } }objList  class= Mainnew {ArrayList();   public static void main(String[] argsobjList.add("One") {;    objList.add("Two");  Object obj1 = new ClassAobjList.add("Three");    obj1 System.whoAmIout.println(objList.get(1));   // Only able to }call methods of  } 

Output of the code above:

Exceptioninterface in"List", threadbut "main"not java.lang.RuntimeException:methods Uncompilableof sourceits codeimplementing -class Erroneous"ArrayList. symWHY?"  type: java.lang.Object.whoAmI } } 

My question is when "ClassA" is implicitly extendingIf class "Object""ArrayList" is implementing interface "List" then why methods of class "ClassA" isI am not availableable to object "obj1" by following the principles of Polymorphism? Onlycall methods of class "Object" are available for object "obj1"."ArrayList"?

I know that there is some misunderstanding in my mind about the concept of Polymorphism. Please help me to clear that misunderstanding!

Polymorphism is working perfectly fine for the following code:

class ClassA { String whoAmI() { return "ClassA"; } } class ClassB extends ClassA{ String whoAmI() { return "ClassB"; } } class Main { public static void main(String[] args) { ClassA obj1 = new ClassA(); ClassA obj2 = new ClassB(); System.out.println(obj1.whoAmI()); System.out.println(obj2.whoAmI()); } } 

Output of the code above is:

ClassA ClassB 

But when I am running following code then Compiler is giving exception

class ClassA { String whoAmI() { return "ClassA";  } }  class Main { public static void main(String[] args) {    Object obj1 = new ClassA(); obj1.whoAmI();    }  } 

Output of the code above:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.lang.Object.whoAmI 

My question is when "ClassA" is implicitly extending class "Object" then why methods of class "ClassA" is not available to object "obj1" by following the principles of Polymorphism? Only methods of class "Object" are available for object "obj1".

I know that there is some misunderstanding in my mind about the concept of Polymorphism. Please help me to clear that misunderstanding!

class ClassA { String whoAmI() { return "ClassA"; } } class ClassB extends ClassA{ String whoAmI() { return "ClassB"; } } class Main { public static void main(String[] args) { ClassA obj1 = new ClassA(); ClassA obj2 = new ClassB(); System.out.println(obj1.whoAmI()); System.out.println(obj2.whoAmI()); } } 

Output of the code above is:

ClassA ClassB 

In the code above output is as expected that I can see the methods of class "ClassB" when I am creating a reference variable of class "ClassA" and instantiating with "new ClassB();", but why it is not same in case when I creating the reference variable of interface "List" and instantiating it by using its implementing class "ArrayList" (I know that we can't create objects of interfaces directly)?

import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) {    List objList = new ArrayList();   objList.add("One"); objList.add("Two");  objList.add("Three");     System.out.println(objList.get(1)); // Only able to call methods of interface "List", but not methods of its implementing class "ArrayList. WHY?"   } } 

If class "ArrayList" is implementing interface "List" then why I am not able to call methods of class "ArrayList"?

I know that there is some misunderstanding in my mind about the concept of Polymorphism. Please help me to clear that misunderstanding!

Post Undeleted by Akash
Post Deleted by Akash
Source Link
Akash
  • 199
  • 1
  • 4
  • 12
Loading