Advertisement
JeffGrigg

Vishal Reddy's code

Sep 3rd, 2017
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.10 KB | None | 0 0
  1. ::::::::::::::
  2. Doctor.java
  3. ::::::::::::::
  4.  
  5. public class Doctor extends Person {
  6.     int id;
  7.     String name;
  8.  
  9.     Doctor(int i, String s, long l) {
  10.         id = i;
  11.         name = s;
  12.     }
  13. }
  14.  
  15. ::::::::::::::
  16. Employee.java
  17. ::::::::::::::
  18.  
  19. public class Employee extends Person {
  20.     int id;
  21.     String name;
  22.  
  23.     Employee(int i, String s, long l) {
  24.         id = i;
  25.         name = s;
  26.     }
  27. }
  28.  
  29. ::::::::::::::
  30. Logic.java
  31. ::::::::::::::
  32.  
  33. public class Logic {
  34.     Person[] pp = new Person[10];
  35.     private int i = 0, j = 0;
  36.  
  37.     public void setPerson(Person p) {
  38.         pp[i++] = p;
  39.     }
  40.  
  41.     public Person getPerson() {
  42.         return pp[j++];
  43.     }
  44. }
  45.  
  46. ::::::::::::::
  47. Person.java
  48. ::::::::::::::
  49.  
  50. public class Person {
  51.     int eyes = 2;
  52.     int legs = 2;
  53.  
  54.     static void eat() {
  55.         System.out.println("all are vegtarinan");
  56.     }
  57. }
  58.  
  59. ::::::::::::::
  60. Student.java
  61. ::::::::::::::
  62.  
  63. public class Student extends Person {
  64.     int roolno;
  65.     String name;
  66.  
  67.     Student(int i, String s, long l) {
  68.         roolno = i;
  69.         name = s;
  70.     }
  71. }
  72.  
  73. ::::::::::::::
  74. test.java
  75. ::::::::::::::
  76.  
  77. public class test {
  78.     public static void main(String[] ars) {
  79.         Logic p = new Logic();
  80.         p.setPerson(new Student(101, "rajesh", 9989889856l));
  81.         p.setPerson(new Student(102, "vishal", 888888888888l));
  82.         p.setPerson(new Employee(10, "subhodh", 6666666666l));
  83.         p.setPerson(new Employee(11, "laxmi", 8989898989l));
  84.         p.setPerson(new Doctor(12, "varun", 99898988558l));
  85.         Person p1 = p.getPerson();
  86.         if (p1 instanceof Student) {
  87.             Student s1 = (Student) p1;
  88.             System.out.println(s1.roolno);
  89.             System.out.println(s1.name);//here what how 2 obtain 2nd student of same class?
  90.         } else if (p1 instanceof Employee) {
  91.             Employee e1 = (Employee) p1;
  92.             System.out.println(e1.id);//here also 2nd employee object how to get?
  93.         } else if (p1 instanceof Doctor) {
  94.             Doctor d = (Doctor) p1;
  95.             System.out.println(d.name);//here also the same..
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement