• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Importing Packages (was Urgent)

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers
I am taking the exam tommorow and i have one query
My pc isnt working and so i cannot compile and see the behaviour of the following java file
Please Help me out right now i have logged in from cyber cafe
Without wasting the time i am posting the question
[code]

//Test.java

package foo;
class Test
{
protected void methodA()
{
}
}
------------
//XYZ.java
package bar;
import package foo.*;
class XYZ extends Test
{
public static void main(String ar[]){
XYZ obj=new XYZ();
obj.methodA();
}
}
what will be the output .
I guess The output would be a compile error
but its using the dot reference on the subclass so i am a bit messed up please clear guys
Thanks
[code]


One more Question
//Test.java
package foo;
class Test
{
char methodA()
{
}
}

======
//XYZ.java
package bar;
import foo.*;
class XYZ extends Test
{
public static void main(String ar[])
{
Test t=new Test();
t.methodA();
}
}
What is the output i guess again compile error because the methodA is having the default please just clarify thanks a lot
[code]

Edited title:like Bert wrote below (it may be urgent to you, but it is not to the majority)
[ June 03, 2007: Message edited by: Barry Gaunt ]
 
Ravi Ahuja
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import package foo.*;
sorry for this one
it should be import foo.*;
actually i was in a hurry sorry
Waiting for your quick response thanks everyone once again
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!


In both the cases compiler error because Test class is not public and
can't be seen outside the package. For exam remember to see the class
accessibility before member.


No hurry!
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other tips:

In case Test class is public:


1- You can't access the protected member of the Test class in the extending
class in another package using the ref variable of Test class and dot operator.

2- It is legal to access the protected member of the Test class using
the ref variable of the extending class and dot operator.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Remember, JavaRanch is entirely populated by volunteers. So a thread title like "URGENT" is discouraged for at least two reasons:

1 - It doesn't help volunteers know whether this is a question they can help with. It's much friendlier to mention the topic of the question in the thread heading - that saves the precious time of all the volunteers who are scanning titles looking for places where they can help.

2 - In my experience, using the word "URGENT" or anything similar in a thread title actually discourages volunteers from looking at that thread - it's very likely that an "URGENT"-ly titled thread will come off looking selfish and unpleasant to deal with.
 
Ravi Ahuja
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks chandra
So a subclass can use a dot operator to access a protected member of the superclass
fine thanks a lot relief now

Sorry Barry
will follow it next time
Thanks Chandra
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic