Full Transcript

CSC123 (INTRO TO PROGRAMMING II) Usman Aslam | Computer Science | Feb 27, 2023 POLYMORPHISM A Polymorphic object or thing can take more than one form or shape Polymorphism is the ability of Java object to take the “shape” of objects belonging to their parent classes Typically, a Java variable can on...

CSC123 (INTRO TO PROGRAMMING II) Usman Aslam | Computer Science | Feb 27, 2023 POLYMORPHISM A Polymorphic object or thing can take more than one form or shape Polymorphism is the ability of Java object to take the “shape” of objects belonging to their parent classes Typically, a Java variable can only take a literal value that matches the data type of the variable int x =9; x = “Hello”; will throw an error String s=“Hello World”; s=100; will throw an error POLYMORPHISM With Polymorphism, a variable declared with one data type can take literal values of another data type as long as the other data type is a subclass of this data type Example: All Java classes inherit the Object class. Therefore, the following is legal in Java: Object o1=“Hello this is a string”; //String value being allocated to Object data type Object o1=99; //Integer value being allocated to Object data type INSTANCEOF OPERATOR The instanceof operator is a conditional operator just like ==, >,

Use Quizgecko on...
Browser
Browser