My Bookmarks

  • No bookmark found

Most Active Members

Java Program(dynamic dispatch+super()+object as parameter)2 min read

Example of dynamic dispatch+super()+object as parameter in Java

import java.util.Scanner;

class Student {

    String[] name, branch;
    int[] roll_no;
    int x;

    Scanner sc = new Scanner(System.in);

    Student(int n) {
        x = n;
    }

    void get() {
        int i;
        name = new String[x];
        roll_no = new int[x];
        branch = new String[x];
        for (i = 0; i < x; i++) {
            System.out.println("Enter the name,roll no and branch of student " + i);
            name[i] = sc.next();
            roll_no[i] = sc.nextInt();
            branch[i] = sc.next();
        }
    }

    void print(Student s) {// Overridden method
        System.out.println("Name        Rollno      branch");
        for (int i = 0; i < x; i++) {

            System.out.println(s.name[i] + "          " + s.roll_no[i] + "        " + s.branch[i]);

        }
    }
}

class cse extends Student {//Subclass1

    static int e;

    public cse(int n) {
        super(n);
    }

    void print(Student s) {
        for (int i = 0; i < x; i++) {
            if (s.branch[i].equalsIgnoreCase("cse")) {

                e++;

                System.out.println("Name   Rollno  branch");
                System.out.println(s.name[i] + "  " + s.roll_no[i] + " " + s.branch[i]);
            }
        }

        if (e == 0) {
            System.out.println("No CSE student");
        }
    }
}

class ece extends Student {

    static int d;

    public ece(int n) {
        super(n);
    }

    void print(Student s) {

        for (int i = 0; i < x; i++) {
            if (s.branch[i].equalsIgnoreCase("ece")) {
                d++;
                System.out.println("Name   Rollno  branch");
                System.out.println(s.name[i] + "  " + s.roll_no[i] + " " + s.branch[i]);
            }

        }
        if (d == 0) {
            System.out.println("No ECE student");
        }
    }
}

class mae extends Student {

    static int d;

    public mae(int n) {
        super(n);
    }

    void print(Student s) {

        for (int i = 0; i < x; i++) {
            if (s.branch[i].equalsIgnoreCase("mae")) {
                d++;
                System.out.println("Name   Rollno  branch");
                System.out.println(s.name[i] + "  " + s.roll_no[i] + " " + s.branch[i]);
            }

        }
        if (d == 0) {
            System.out.println("No MAE student");
        }
    }
}

class MainClass {

    public static void main(String args[]) {

        System.out.println("Enter the total no of students");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Student s1 = new Student(n);
        s1.get();
        s1.print(s1);

        Student st1 = new cse(n); // Reference of Student class(superclass) having cse class object(subclass)
        System.out.println("");

        st1.print(s1); // Dynamic method dispatch by calling print of cse class
        Student st2 = new ece(n);
        System.out.println("");
        st2.print(s1);
        Student st3 = new mae(n);
        System.out.println("");
        st3.print(s1);

    }
}

Bookmark(0)
Tags:

Add a Comment

Your email address will not be published. Required fields are marked *