Maduranga Liyanage

Ranch Hand
+ Follow
since May 25, 2005
Merit badge: grant badges
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Maduranga Liyanage

I'm in a bit of a hurry so I will give a code I have tried later.
But basically, if I remember correctly, for protected static methods to be accessed outside the package you must extend the super class. And I am not quite sure whether you can access a non-static method by using a superclass instance. I think it must be a subclass instance. In the case of the static methods, you still must extend the class, but you can access the super class static method via both superclass and subclass instances. I will confirm this later.
You\re welcome Bazilo.
But there some differences when you use the protected modifier. The behavior is different for static and non-static methods. should make that clear too.
Yes. In this context, it doesn;t matter if the method was static or not. The behavior is the same.
Thank you very much Stephen and Sara.

When I said private modifier doesnt add anything special, I meant it doesnt add anything special when it is used in a static method in contrast to a non-static method.
For example, the 'protected' modifier has different behavior for static and non-static method. The protected methods can only accessed through a subclass instance if the class is in a different package if the method is non-static. But need not so when the method is static. That is what I meant by that the private modifier doesn't add anything special between the static and non-static methods.
It seems for me the behavior is the same whether private modifier is used on a non-static or a static method. am I wrong ?

Thank you very much.
Thank you very much for the clarification Stephen..
But what I gather is that the private modifier does not add anything special whether it is used in a static method or not, am I right?
Thank you Himanshu.. that is what I wanted to know..
Thank you for the replies.

My question is not regarding classes. It is about the modifiers of static methods.

Basically, static methods are invoked on reference type.
Suppose I have two classes Parent and Child (extending Parent) and both have static methods by the same name. Static methods are not overridden but hidden. So when I invoke the static method from a Parent reference, I can invoke the Parent class method and vice versa.

On the other hand, 'private' modifier hides (static or non-static) the method from the subclasses. And there is no way the subclass can access the super class method (static or non-static).

The question asks:What class modifier(s) can be inserted at line 1 to prevent method() from being overridden (hidden ) without causing compile time errors ?

I am not sure what the question is trying to ask exactly. Static methods are 'hidden' when they are overridden in the subclass. You dont need the private modifier to do that. The only reason you would add private modifier imo is to prevent the subclass from invoking it. So I think the problem is, what is the question really asking for?
Thank you Stephen.
Having a read now..
Thank you very much.

So I can define the reference with the <? super> so that the actual collection type (right hand side) can be left hand side or its super type.. but when I add, I can only add whatever I defined on the right hand side.

but any particular use of this?
I know the use super in a method argument, so I can 'add' any supertype of the reference type safely, but why define a reference in this way?
If for example, I say,


But I can only add Animal objects into this, and I even cannot safely call Dog methods on the items inside the dog list.
SO what purpose does this serve?
Isn't this risky? because at compile time, dog list can be a list of Dogs or Animals.
But I cannot invoke Dog methods on the items inside dog list. Because the items can be Dogs or Animals?

What am I missing here.


Thank you.
I cannot understand the use of 'super' keyword in this case.

Suppose I have dog extending Animal and the following code:


This doesn't let me add its superclass Animal.
So my problem is, what is the use of 'super'.
what wouldn't I just use dog without super?

But when I do this:


It compiles fine, BUT still doesn't let me add an Animal()?
Intuitively Animals (superclass) shouldnt be allowed to get added in to dog reference List, but my problem is why use super in this case.
what gives?

what is the reason that I have a dog type reference and an Animal type List, but I can ONLY add dogs?

Any help is appreciated.
Thank you.

Thank you.
But I am wondering what difference it would make by adding the 'private' modifier when accessing those methods by their respective references?
Sorry.

Found this in the Inquisition question set (John Meyer).

Thank you.
Found this in a practice exam and quite confused.



The answer is 'private'.

But my confusion is, this will work just fine without the private modifier and I cannot understand what is added by using the private modifier.
Without the modifier, the method is sup class will be hidden when a reference of type test is used to invoke the method.
Because static methods are always invoked from reference type, I'm not sure what can be achieved by using the private modifier.

Thank you.
Thank you Henry.

I think I got it now. and sorry for hijacking this thread.

Cheers.
Thank you.

But doesnt q1 (<? extends Number>) has the possibility of being of type Byte or any of the other wrapper classes?
And q2 can be Integer or thereof. So assigning q1=q2 is OK?

I'm sorry if I mistaking something gravely here.