aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes q on package Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "q on package" Watch "q on package" New topic
Author

q on package

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
// Super.java

package p1 ;
public class Super {
protected void say ( String s ) {
System.out.println ( s ) ;
}
}

// SubOne.java

package p1 ;
public class SubOne extends p1.Super { }

// SubTwo.java

1. package p2 ;
2. public class SubTwo extends p1.Super {
3. public static void main ( String [ ] args ) {
4. p1.Super s = new p1.Super() ;
5. p1.SubOne s1 = new p1.SubOne () ;
6. SubTwo s2 = new SubTwo() ;
7. s.say (" Super ") ;
8. s1.say ("SubOne") ;
9. s2.say ("SubTwo") ;
10. }
11. }

Assuming tht the classes r defined in three separate files,
wht is the outcome of the program?

Line 2 does not compile, becuse u must import package p1.
line 7 does not compile
line 8 does not compile
line 9 does not compile
there s nothing wrong witht he code.

my answer was the first option.
however, the second and third options are right.

could someone plz explain?



SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Anurag Saksena
Greenhorn

Joined: Jul 20, 2004
Posts: 28
method say() is protected in Super class. This method is not visible outside package p1 unless a Class tries to override it.

In the following line say() is being used like any other method.

7. s.say (" Super ") ;
8. s1.say ("SubOne") ;

It will definitely fail to compile. You'll need to make say() public in Super to compile it.

-a.
==========
SCJP 1.4
SCWCD 1.4
JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
hi,

if so, s is an instance of super.
so s.say() shd be legal?
line 8 and line 9 shd be illegal.

how is it in overriding?

public methods can be overridden?
static methods not overridden, only hidden?
protected methods- how is the concept?
final methods cannot be overridden, so again hidden like in static?
abstract methods can be overridden?
JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
how is it for synchronized methods?
Anurag Saksena
Greenhorn

Joined: Jul 20, 2004
Posts: 28
That is how it is..
private = visible only inside class
friendly (default) = visible only inside package
protected = visible inside package and can only be overriden outside
public = visible everywhere



-a.
Atul Chandran
Greenhorn

Joined: Oct 24, 2004
Posts: 22
Protected methods are accessible from the subclasses in other packages. The method is inherited by the subclass and it becomes a private method in the subclass. But we cannot access the method using a reference of a superclass the compiler will complain.

In this program the 7th line try to access the say() using superclass reference and the 8th line try to access the private method in the class SubOne. That's why these lines fail to compile. In line 9 we are calling the private method say() from within the class and therefore its legal.

[ October 27, 2004: Message edited by: Atul Chandran ]
[ October 27, 2004: Message edited by: Atul Chandran ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: q on package
 
Similar Threads
Access Modifier/Package
Access Modifier question
about protected methods
protected access
Why can this not Compile ??????