File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding" Watch "Overriding" New topic
Author

Overriding

Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Given the code below, and making no other changes, which access modifiers (public, protected or private) can legally be placed before myMethod() on line 3?
If line 3 is left as it is, which keywords can legally be placed before myMethod on line 8?


OPTION A:
private or nothing(i.e. leaving it as it is) on line 3.
Nothing(i.e. leaving it as it is) or protected or public on line 8.
OPTION B:
public or protected on line 3. private or nothing(i.e. leaving it as it is) on line 8.
OPTION C:
nothing(i.e. leaving it as it is) or protected or public on line 3. private or nothing(i.e. leaving it as it is) on line 8.
OPTION D:
None of the above

I know about overriding and overloading..but i dont know what is being asked in the question?


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Shafi mia
Greenhorn

Joined: Feb 18, 2008
Posts: 10
I think the answer is A. Because you can make an overridden method less restrictive.


SCJP 5.0
Ian Edwards
Ranch Hand

Joined: Aug 14, 2006
Posts: 107
What you need to consider here is what are the valid access modifiers that the sub-class can use when inheriting a method from the super-class. For example, if you have inherited a method with public access you can't use a modifier that reduces the access visibility. In other words you can't use default access, protected or private.

If the method in the super-class is protected then you can't use default or private access modifiers. Protected is considered to have wider access than default.

Note however that you can make a method more visible. If you've inherited a method with protected access then the sub-class can specify public access.

The easiest way to test this out is by entering code into an editor like IntelliJ or Eclipse and observing whether the editor complains or not about the different modifiers.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Ohh Yes..I got your point..!!
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
class HumptyDumpty
{
void myMethod()
{
System.out.println("hi");
}
}
class HankyPanky extends HumptyDumpty
{
void myMethod()
{
System.out.println("hello");
}

public static void main(String[] args)
{
-- line1HankyPanky p = new HumptyDumpty();
p.myMethod();
}
}

The line 1 is giving error it is telling to cast me to HankyPanky
As far as i know we use the reference value of subclass and create object of superclass and than called the method on subclass method.
Is this related to overloading or overriding.
Can some one shed some lights on it.
One more where to i test the access modifiers in superclass/subclass?
Can someone help me out


Thanks<br />Dinesh
Siphiwe Madi
Ranch Hand

Joined: Aug 16, 2007
Posts: 70
Hi Dinesh,

The line 1 is giving error it is telling to cast me to HankyPanky
As far as i know we use the reference value of subclass and create object of superclass and than called the method on subclass method.
Is this related to overloading or overriding.


Not exactly, but the opposite to what you saying is true.
i.e.

we use the reference value of the superclass (HumptyDumpty) and create an object of the subclass (HankyPanky) and then call the method of the subclass
object (HankyPanky) using a reference variable of the superclass (HumptyDumpty).


See if you can understand the explanation below:

You can't use a HankyPanky reference variable p to refer to an object of superclass HumptyDumpty.
instead you need to do the reference the other way round.

i.e. HumptyDumty d = new HankyPanky();

what this above statement says is "a HankyPanky is a HumptyDumpty".

The easier way i understood the inheritance hierarchy is thinking of the analogy of "Employees" and "Managers".
here you know that an Employee is an Employee, thus you can instantiate like this:

Employee e = new Employee();

you also know that every Manager is also an Employee, thus it is legal to say:

Employee e = new Manager();

but, not every Employee is a Manager, thus you cannot say:

Manager m = new Employee();

if you do this for an employee instance that is not a manager and try to access operations (methods) that are only
applicable to Managers, you get an error. that's why you cannot instantiate an Employee object and assign it to a Manager reference. Which is what you have done on your code (substitute Employee class for HumptyDumpty and Manager class for HankyPanky).

remember one of the reasons to inherit from a class is to override its methods or define additional operations.

Is this related to overloading or overriding.


Your question here is related to Overriding, because you are overriding the superclass method myMethod to print "hello" in the subclass.

Hope this shed's some light

Regards,
Siphiwe Madi
[ February 19, 2008: Message edited by: Siphiwe Madi ]

Regards, Siphiwe Madi
[SCJP, SCWCD, __] Next ... scjwsd
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding