Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java overriding methods when creating new instance of a class

Tags:

java

oop

android

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 protected 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.

like image 475
blessanm86 Avatar asked Sep 08 '11 11:09

blessanm86


People also ask

What methods can be overriding in Java?

Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden.

Is instance methods are overridden?

Instance Methods An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

How do you override a method from another class in Java?

Rules for method overriding: In java, a method can only be written in Subclass, not in same class. The argument list should be exactly the same as that of the overridden method. The return type should be the same or a subtype of the return type declared in the original overridden method in the super class.

Why would you override a method of a base class?

The main advantage of method overriding is that the class can give its own specific implementation to a inherited method without even modifying the parent class code.


2 Answers

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.

like image 180
aioobe Avatar answered Sep 21 '22 15:09

aioobe


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.

like image 38
Guido Avatar answered Sep 21 '22 15:09

Guido