Created
October 6, 2015 06:49
-
-
Save smamran/17e3d3253d829e2716be to your computer and use it in GitHub Desktop.
Typecasting between SuperClass and SubClass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) p3; // works | |
| System.out.println(s3); | |
| } | |
| } | |
| 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