Menu
Topics Index
...
`

Access Control Using private Keyword


If you need explanation Read this topic

If you need Answer Take test on this topic

Access Control Using private Keyword
What will be the output of the following program?
public class FoodItems {
    public static void main(String s[]) {
        Noodles eat = new Noodles();
        eat.print();
        Maggi mag = new Maggi();
        mag.print();
        System.out.println("~" + mag.sticks);
    }
}
class Maggi {
    private final int spoon = 3;
    protected int sticks = 4;
    void print() {
        System.out.print("~" + spoon);
    }
}
class Noodles extends Maggi {
    private final int powder = 1;
    protected int fire = 2;
    void print() {
        System.out.print(powder + "~" + fire + "~" + sticks);
    }
}

A. 1~2~4~3~4
B. 1~2~4~1~2~3~4
C. Compilation Error
D. Runtime Error
Topic: final Keyword In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

a.here we r usng th concpt of inheritance..

in clas fooditems..we r makng an objct of noodles clas i.e  eat...usng eat obj we r prntng th values of powder fire sticks by callng priny fnctn..wich prints
1-2-4

den we r calng mag obj of maggi cls wich prnts th value for spoon i.e 3.

n finaly th sop stmt will prnt th value for sticks i.e 4

Posted by Asma Mujtaba Khan    2014-10-30 09:06:15


Ans is C.. because when eat.print() is called child class is intiating.. child class cant access parent class properties. here we are accessing sticks (property of Maggi class).. so that leads to compilation error..

Posted by Om Prakash Om    2014-10-30 09:44:37


Ans is A
here we are having three classess named as
1.FoodItems  class       //main class

2.Maggi class               //having two variable
    1. private final variable as spoon of integer type
    2.normal variable of integer protected type     

3.Noodles class              //having two variable
    1. private final variable as spoon of integer type
    2.normal variable of integer protected type

here we are creating two objects named as eat and mag respective to their classess as Noodles and Maggi

after creating the noodles class object as eat which is extending the class Maggi (Using the concept of Inheritance i.e Sub class accessing the super class properties)
we are calling the print() method in this we are printing the values of

(Method Overriding concept is used in this programm i.e...related to INHERITANCE)
*  powder i.e....1      // local variable to noodles class
*  fire i.e............2     // local variable to noodles class
*  sticks i.e.........4       // super class variable

then again we are creating the object of Maggi class and calling the print() method and printing the value of

* spoon i.e......3
then the control comes back to main method and prints its SOP statement  relating the value of
*  "mag.sticks" i.e.....4

in between to differentiate we are printing the tilde symbols between numbers so the whole ouput looks like

1~2~4~3~4

Posted by Ramesh Velpukonda    2014-10-30 13:43:10


a.

Posted by Shubham Bansal    2014-10-30 19:07:43


This dose is now closed and the winners are Asma Mujtaba Khan,  for 'First Correct Comment', Ramesh Velpukonda,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Asma Mujtaba Khan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-10-31 06:41:07


© meritcampus 2019

All Rights Reserved.

Open In App