Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: extending Object class

I'm writing (well, completing) an "extension" of Java which will help role programming.
I translate my code to Java code with javacc. My compilers add to every declared class some code. Here's an example to be clearer:

MyClass extends String implements ObjectWithRoles { //implements... is added
    /*Added by me */
    public setRole(...){...}
    public ...
    /*Ends of stuff added*/
    ...//myClass stuff
}

It adds Implements.. and the necessary methods to EVERY SINGLE CLASS you declare. Quite rough, isnt'it?

It will be better if I write my methods in one class and all class extends that.. but.. if class already extends another class (just like the example)?

I don't want to create a sort of wrapper that manage roles because i don't want that the programmer has to know much more than Java, few new reserved words and their use.

My idea was to extends java.lang.Object.. but you can't. (right?)
Other ideas?

I'm new here, but I follow this site so thank you for reading and all the answers you give! (I apologize for english, I'm italian)

like image 615
fabiofili2pi Avatar asked May 18 '10 08:05

fabiofili2pi


People also ask

Can a class extend an Object?

Extending Object: An exampleA class can explicitly extend Object , as demonstrated in Listing 1.

Does interface extend Object class?

Interfaces in java don't inherit from Object class. They don't have default parent like classes in java.

What is the use of extending a class in Java?

Extends: In Java, the extends keyword is used to indicate that the class which is being defined is derived from the base class using inheritance. So basically, extends keyword is used to extend the functionality of the parent class to the subclass.

Does String class extend Object?

The String class doesn't have extends Object in the source code. However, the API declaration always indicates that every class extends Object implicitly.


2 Answers

If it is only like a "research" project in which you want to explore how such extension would work, you could provide your own implementation of the Object class. Simply copy the existing object implementation, add your setRole method etc, and give -Xbootclasspath:.:/usr/lib/jvm/java-6-sun/jre/lib/rt.jar as parameter to the java command. (I will look for api-classes in . before looking in the real rt.jar.)

like image 97
aioobe Avatar answered Sep 26 '22 06:09

aioobe


You should consider using composition rather than inheritence to solve this problem; that way you can provide the functionality you need without using up your "one-shot" at inheritence.

For example, the JDK provides a class PropertyChangeSupport, which can be used to manage PropertyChangeListeners and the firing of PropertyChangeEvents. In situations where you wish to write a class that fires PropertyChangeEvents you could embed a PropertyChangeSupport instance variable and delegate all method calls to that. This avoids the need for inheritence and means you can supplement an existing class hierarchy with new functionality.

public class MyClass extends MySuperClass {
  private final PropertyChangeSupport support;

  public MyClass() {
    this.support = new PropertyChangeSupport(this);
  }

  public void addPropertyChangeListener(PropertyChangeListener l) {
    support.addPropertyChangeListener(l);
  }

  protected void firePropertyChangeEvent() {
    PropertyChangeEvent evt = new ...
    support.firePropertyChangeEvent(evt);
  }
}
like image 37
Adamski Avatar answered Sep 26 '22 06:09

Adamski