Skip to content

Instantly share code, notes, and snippets.

@smamran
Created October 6, 2015 06:38
Show Gist options
  • Select an option

  • Save smamran/8eca10d6974c1eeebc7a to your computer and use it in GitHub Desktop.

Select an option

Save smamran/8eca10d6974c1eeebc7a to your computer and use it in GitHub Desktop.
SuperClass SubClass Compatibility
package slidenerd.javaoop;
/**
* Created by Microsoft on 10/6/2015.
*/
public class SuperClassReferenceVariable {
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();
Student s1 = new Student();
Student s2 = new Student();
// All info of Person present in Student
Person p3 = new Student();
// Without typecast
// But all info of Student needed is
// not present in Person class
Student s3 = (Student) new Person();
}
}
class Person {
String name;
int age;
String address;
}
class Student extends Person {
int rollnumber;
int marks;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment