Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::::::::::::::
- Doctor.java
- ::::::::::::::
- public class Doctor extends Person {
- int id;
- String name;
- Doctor(int i, String s, long l) {
- id = i;
- name = s;
- }
- }
- ::::::::::::::
- Employee.java
- ::::::::::::::
- public class Employee extends Person {
- int id;
- String name;
- Employee(int i, String s, long l) {
- id = i;
- name = s;
- }
- }
- ::::::::::::::
- Logic.java
- ::::::::::::::
- public class Logic {
- Person[] pp = new Person[10];
- private int i = 0, j = 0;
- public void setPerson(Person p) {
- pp[i++] = p;
- }
- public Person getPerson() {
- return pp[j++];
- }
- }
- ::::::::::::::
- Person.java
- ::::::::::::::
- public class Person {
- int eyes = 2;
- int legs = 2;
- static void eat() {
- System.out.println("all are vegtarinan");
- }
- }
- ::::::::::::::
- Student.java
- ::::::::::::::
- public class Student extends Person {
- int roolno;
- String name;
- Student(int i, String s, long l) {
- roolno = i;
- name = s;
- }
- }
- ::::::::::::::
- test.java
- ::::::::::::::
- public class test {
- public static void main(String[] ars) {
- Logic p = new Logic();
- p.setPerson(new Student(101, "rajesh", 9989889856l));
- p.setPerson(new Student(102, "vishal", 888888888888l));
- p.setPerson(new Employee(10, "subhodh", 6666666666l));
- p.setPerson(new Employee(11, "laxmi", 8989898989l));
- p.setPerson(new Doctor(12, "varun", 99898988558l));
- Person p1 = p.getPerson();
- if (p1 instanceof Student) {
- Student s1 = (Student) p1;
- System.out.println(s1.roolno);
- System.out.println(s1.name);//here what how 2 obtain 2nd student of same class?
- } else if (p1 instanceof Employee) {
- Employee e1 = (Employee) p1;
- System.out.println(e1.id);//here also 2nd employee object how to get?
- } else if (p1 instanceof Doctor) {
- Doctor d = (Doctor) p1;
- System.out.println(d.name);//here also the same..
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement