Wednesday, June 3, 2015

Polymorphism II








// All reference can be converted to the type Object
// When a superclass variable contains a reference to a subclass object,
// and that reference is used to call method, the subclass version of the method is called.

// why anyone want it --- This can happen if you want to reuse code
// that knows about the superclass but not the subclass

public class PolymorphismDemo2
{
public static void main(String[] args)
{
//Student s = (Student) new Person();
// though above this statement do not shows the compile errot it will shows the runTime error later

Person1 p = new Student1();
Student1 s = (Student1) p;
System.out.println(s);
}
}


class Person1
{
String name, address ;
int age ;
}


class Student1 extends Person1
{
int roll ;
int grade;
}


class GraduateStudent extends Student1
{
int graduateYear;
}



No comments:

Post a Comment