Menu
Topics Index
...
`

Access Control Using private Keyword2959


If you need explanation Read this topic

If you need Answer Take test on this topic

Access Control Using private Keyword2959
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  int spoon = 3;
    protected int sticks = 4;
    void print() {
        System.out.print("~" + spoon);
    }
}
class Noodles extends Maggi {
    private  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: Using private Keyword In Java For Access Control

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.

Ans is A..here we have the Concept of Access controlling means limiting the access of the properties of the class using the some keywords in java...

here we Maggi class Parent class and Noodles class Child class of the Maggi class...here we have the inheritance concept which means when class extending than Parent class properties clas will get inherited into child class except private members...

here in Maggi class we have spoon which is private so access of this variable is with in this class,we cant access this class outside this class even with obejcts also...

similar to the powder variable in Noodles class...here we protected variables...protected members will available to the its child class and within the class onlya dn classes which are in same package
Coming to the program..in main class we create the Object for the Noodles class and call the method print() there we are printing the values of powder and fire and sticks..here sticks is protected so it will give access permissions to its child class and classes which are in same package..

Next we create the instance for the maggi class and called the print() there we ae printing the spoon values which is 3...next we are printing the the sticks values in main() it wil print as 4

Posted by Uday Kumar    2014-12-03 12:57:04


Ans a.

Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:

--Visible to the package. the default. No modifiers are needed.

--Visible to the class only (private).

--Visible to the world (public).

--Visible to the package and all subclasses (protected).

A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package

The Java specification states that a subclass inherits all the instance fields and instance methods of its superclass accessible to it. If the subclass is defined in the same package as the superclass, it inherits all non-private instance fields and methods. If the subclass is defined in a different package, however, it inherits all protected and public instance fields and methods. private fields and methods are never inherited; neither are class fields or class methods. Finally, constructors are not inherited;

If class member doesn’t have any access modifier specified, then it’s treated with default access. The access rules are similar as classes and the class member with default access will be accessible to the classes in the same package only. This access is more restricted than public and protected but less restricted than private.

coming to this program here we have 3 classes. we created the object for Maggi and Noodles class .by using that reference print method get called.

Noodles class extends Maggi ,so it can inherit all the features of Maggi except private fields.

eat.print() called so Noodles class sop prints 1~2~4
here power ly accessible with the Noodles class.fire has protected access all the members and subclasses can use this value.sticks value is accessed from Maggi class it has protected access.

mag.print() called sop prints ~3

spoon declared with private access.

finally main function sop prints Maggi class's protected access variable's value ~4

Posted by Maheshwari Natarajan    2014-12-03 17:14:02


Ans A...Lol  
http://www.xyzws.com/javafaq/what-are-method-overloading-and-method-overriding/22
Ctr C+Ctrl V..

Posted by Akhila Rao    2014-12-04 13:34:44


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Varun Kumar. 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-12-05 04:56:45


© meritcampus 2019

All Rights Reserved.

Open In App