Wednesday, June 3, 2015

Polymorphism I





// In the real world, every student is a person
// Every object of class Student is also an object of the class Peron
// An object of a class can be referenced by a variable of any ancestor type


public class PolymorphismDemo {

public static void main(String[] args)
{
Person p = new Person();
Student s = new Student();

Person pRef = new Student();          // this is possible
// Student sRep = new Person();       // this is not possible
}
}


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


class Student extends Person
{
int roll ;
int grade;
}



No comments:

Post a Comment