Java overriding methods when creating new instance of a class
This might be simple for seasoned java developers but I just cant seem to figure it out. I read a post from here. The code was
View v = new View(this) {
@Override
protected void onDraw(Canvas canvas) {
System.out.println("large view on draw called");
super.onDraw(canvas);
}
};
It was an Android question. Here the user creates an instance of a view and overrides a method in a single line. Is there a name for this kind of coding?
My second doubt is, he overrides a protec开发者_C百科ted method from another package. Isn't protected mean package private. I know this will work as I tried it out but I just couldn't figure out why it worked. So why is this code working?
I did try to google this and search in SO before asking but couldn't figure out an answer.
protected
means (roughly) "available to sub-classes". (See this table.) Since the new View(this) { ... }
creates a subclass, it is possible to override the method within it.
In this case it doesn't matter that you're in a different package. (See the protected
line and second column in this table.) The fact that the method is in a subclass is sufficient to "get access" to a protected method.
Potential follow-up question: What sense does it make, if I can't call the method anyway?
All methods in Java are virtual. This means that whenever the View
class performs a seemingly internal call to the onDraw
method, this call will be dispatched to the overridden method.
That is not exactly a kind of coding. That is a Java anonymous class. It is very common in Android and in general with event listeners and that kind of stuff.
For more details you can read this link (probably not the best one):
The anonymous inner classes is very useful in some situation. For example consider a situation where you need to create the instance of an object without creating subclass of a class and also performing additional tasks such as method overloading.
About your second question, the keyword protected
means that the method is only available to subclasses, so it is possible to override the method.
This is an anonymous class. You are correct that you are overriding a protected method and this is perfectly normal. A protected method is visible, and can therefore be overridden, by a sub class, which is what you have created here.
Package protected is the default scope when you do not provide a scope for your variable or method. That is different to protected.
So many answeres were there for "protected", so i am going to other one :)
@override is informing the compiler to override the base class method, and if there is no base class method of this signature then throws compilation error.
These are called annotations. You can search for annotations topic in java. You can create custom annotations as well.
Regards,
SSuman185
Just like others here are already answered this is called anonymous class, and overriding protected methods is legal since protected methods are visible to child classes and classes in same package.
精彩评论