Menu
Question Index
...

What will be the output of the following program?

public class Example {
    public static void main(String args[]) {
        Rails rails = new Rails();
        rails.i = 10;
        rails.j = 20;
        Ruby ruby = new Ruby();
        ruby.i = 50;
        ruby.j = 100;
        compareHead(ruby, rails);
    }
    
    public static void compareHead(Rails ruby, Rails rails) {
        System.out.println(ruby.getTotal() > rails.getTotal() ? "ruby" : "rails");
    }
}

class Head {
    int i, j;
    int getTotal() {
        return i + j;
    }
}

class Ruby extends Head {
    int k;
}

class Rails extends Head {
    int l;
}


ruby
rails
Compilation Error
Runtime Error

Doubts

Problems

Topic: Passing Sub Class Object As Super Class Reference

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App