Mike G
Mike G

Reputation: 4959

Extending class while retaining the name in java

Is it possible to extend a class in java while retaining the original name? if now can i add to that class extra functions(note this class is provided by the android sdk, its the view class)

Upvotes: 1

Views: 621

Answers (4)

jambriz
jambriz

Reputation: 1303

i assume you mean the fully qualified name (FQN = package + class name). so, no, you can´t, and you really shouldn´t. Thats the point of having a package name in the first place.

If you insist in doing it, however, you can create a new class with the same package and the same name, starting with the original source code (assuming you have it), and change whatever functionality you need. Depending on the classpath order (usually your source code has precedence over any included libraries, but you must make sure it is so) the classloader will pick your class instead of the original one. but this is more of a hack and i strongly suggest to avoid it unless you know exactly what you're doing, because you could end up with all sorts of unexpected behavior.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

You can do this only if it's in a different package. (That is, it can have the same simple name, but not the same qualified name.)

package com.me;

class Number extends java.lang.Number {
    // ...
}

Upvotes: 3

Mr. Will
Mr. Will

Reputation: 2308

When you extend a class in java, you do so by creating a new class with a new name. ex:

public class MySpecialView extends View

The extended class is not modified.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160181

No, classes in Java are not "re-openable". An extension is a new class.

(With a caveat that through treachery you might be able to add bytecode, but nothing would be able to access it at compile-time.)

Upvotes: 4

Related Questions