Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding

 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Shafi mia
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer is A. Because you can make an overridden method less restrictive.
 
Ian Edwards
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh Yes..I got your point..!!
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Siphiwe Madi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic