Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8295875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:45:40+00:00 2026-06-08T14:45:40+00:00

I often wanted to be able to decorate java-classes properly, that is add behaviour

  • 0

I often wanted to be able to decorate java-classes properly, that is add behaviour to them. I know mixins from ruby, and i know that they can get terribly confusing.

I came up with the theoretical idea of having a language-construct like this:

package org.test.decorator;

public decorator ListPatch<E> extends List<E> {
    public E last() {
        return this.get(this.size() - 1);
    }
}

this would give access to public members of List and the Decorator itself.

Then in a class i could use:

package org.test.decorator;

decoration org.test.decorator.ListPatch;

public MyClass {
    public void foo() {
        List<String> list = Lists.newArrayList();
        list.add("test");
        System.out.println(list.last());
    }
}

I don’t have that much knowledge about compilers, so I wondered if something like that was possible. Also if it actually would be an improvement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:45:42+00:00Added an answer on June 8, 2026 at 2:45 pm

    Sure, it’s possible.

    The fact that Scala compiles to bytecode (and has a fairly straight forward mapping from Scala classes to bytecode classes) and supports mixins proves this.

    Here’s how your sample code looks in Scala syntax:

    class ListTest {
    
        // ...
    
    }
    
    trait ListPatch {
        def last {
            // ...
        }
    }
    
    object Main {
        def main(args: Array[String]) {
            val list = new ListTest() with ListPatch;
            list.add("test")
            println(list.last)
        }
    }
    

    Scala compiles this by adding an axillary class Main$$anon$1 which composes ListTest and ListPatch.

    The motto for the Java developers have always been (and will probably always be) “If in doubt, leave it out.” though.

    Related questions:

    • Implement Mixin In Java?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I've often wanted to search stackoverflow with SELECT whatever FROM questions WHERE
I come from a python background, where it's often said that it's easier to
This is something I've often wanted to be able to do while debugging. If
I wanted to know that if I do start excel temp.xls The os will
i often have in java projects a lot of small helper(storage) classes like 2-Tuple,
Continuing my reverse engineering education I've often wanted to be able to copy portions
I wanted to know the difference among following control and classes. Windows Browser Control
I'm using SVN (TortoiseSVN) and often came into the following situation: I wanted to
Often, when searching for answers, I have found that certain websites will allow you
Often, I would like to build up complex regexps from simpler ones. The only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.