Saturday, March 1, 2014

Suppose I have a class A:
public class A {
    public A(){....}
    public void method1() {...}
};
And an instance of that class:
A anA = new A();
Is there any way to override the method1() only for anA? This question arises when I write a small painting program in which I have to extend the JPanel class several times just to make minor changes to the different panels that have slightly different characteristics.
share|improve this question
 
why not a method2? –  Senthil Kumar Aug 13 '12 at 6:34
add comment

2 Answers

up vote43down voteaccepted
You can do the following:
A anA = new A() {
    public void method1() {
        ...
    }
};
This is the same as:
private static class myA extends A {
    public void method1() {
        ...
    }
}

A anA = new myA();
Only with the exception that in this case myA can be reused. That's not possible with anonymous classes.
share|improve this answer
13 
True, however technically this is creating an anonymous class that extends the class A. –  Stephen C Aug 13 '12 at 6:37
 
@StephenC Indeed. I have updated the answer. Thanks for pointing out. –  jensgram Aug 13 '12 at 6:38
 
@jensgram This might be late but thank you very much. –  Tran Son Hai Mar 16 '13 at 23:25
add comment
You can create a an new anonymous class on the fly, as long as you are using the no-arg constructor of your class A:
A anA = new A() {

  @Override
  public void method1() {
    ...
  }
};
Note that what you want to do is very close to what is known as closure, which should come along with next release 8 of Java SE.

5 comments:

oakleyses said...

jordan pas cher, chanel handbags, nike outlet, michael kors pas cher, kate spade outlet, replica watches, longchamp pas cher, nike free, jordan shoes, christian louboutin shoes, nike free run, louis vuitton outlet, oakley sunglasses, ray ban sunglasses, polo ralph lauren, ugg boots, christian louboutin uk, air max, louis vuitton outlet, ugg boots, tiffany jewelry, polo ralph lauren outlet online, burberry pas cher, prada outlet, nike air max, gucci handbags, sac longchamp pas cher, ray ban sunglasses, louboutin pas cher, louis vuitton, uggs on sale, tiffany and co, oakley sunglasses, louis vuitton outlet, ray ban sunglasses, longchamp outlet, louis vuitton, longchamp outlet, replica watches, nike roshe, polo outlet, oakley sunglasses, cheap oakley sunglasses, oakley sunglasses wholesale, christian louboutin, christian louboutin outlet, tory burch outlet

oakleyses said...

lululemon canada, nike air max, burberry outlet, oakley pas cher, burberry handbags, coach outlet store online, kate spade, michael kors outlet, michael kors, nike air force, true religion jeans, true religion outlet, michael kors, polo lacoste, nike tn, new balance, abercrombie and fitch uk, michael kors outlet, uggs outlet, michael kors outlet, ralph lauren uk, michael kors outlet online, replica handbags, coach outlet, true religion outlet, coach purses, nike free uk, sac vanessa bruno, mulberry uk, michael kors outlet online, michael kors outlet online, michael kors outlet online, north face, uggs outlet, converse pas cher, hogan outlet, nike air max uk, hollister pas cher, sac hermes, nike roshe run uk, hollister uk, nike air max uk, true religion outlet, timberland pas cher, vans pas cher, ray ban pas cher, guess pas cher, ray ban uk

oakleyses said...

ipad cases, beats by dre, iphone 6 cases, ferragamo shoes, wedding dresses, hollister clothing, p90x workout, instyler, asics running shoes, nike huaraches, hermes belt, north face outlet, s6 case, babyliss, nfl jerseys, hollister, iphone 6 plus cases, insanity workout, ralph lauren, iphone cases, iphone 6s plus cases, bottega veneta, new balance shoes, longchamp uk, iphone 6s cases, giuseppe zanotti outlet, nike roshe run, mont blanc pens, herve leger, oakley, lululemon, timberland boots, nike air max, iphone 5s cases, ghd hair, valentino shoes, louboutin, nike trainers uk, celine handbags, north face outlet, baseball bats, vans outlet, abercrombie and fitch, chi flat iron, soccer jerseys, mac cosmetics, mcm handbags, soccer shoes, jimmy choo outlet, reebok outlet

oakleyses said...

supra shoes, marc jacobs, canada goose, converse outlet, toms shoes, ugg uk, ugg pas cher, canada goose outlet, canada goose, nike air max, louis vuitton, karen millen uk, doudoune moncler, converse, louis vuitton, vans, gucci, montre pas cher, moncler outlet, canada goose uk, canada goose outlet, moncler, pandora jewelry, barbour uk, moncler outlet, juicy couture outlet, louis vuitton, canada goose, pandora uk, coach outlet, swarovski crystal, ugg,uggs,uggs canada, juicy couture outlet, links of london, louis vuitton, louis vuitton, hollister, swarovski, canada goose outlet, ray ban, moncler, ugg, barbour, ugg,ugg australia,ugg italia, replica watches, pandora charms, moncler uk, pandora jewelry, wedding dresses, canada goose jackets, hollister, lancel

Zheng junxai5 said...

zhengjx20160721
hermes outlet
burberry outlet online
louis vuitton outlet
true religion jeans
oakley vault
celine outlet
kids lebron shoes
jordan retro 8
ray ban sunglasses
air jordan femme
timberland outlet
longchamp outlet
polo ralph lauren
coach outlet online
cheap oakley sunglasses
true religion outlet
hollister clothing
ralph lauren clearance outlet
nike running shoes for men
coach factory outlet
ghd hair straighteners
ghd flat iron
north face jackets
lebron 13
kate spade handbags
louis vuitton outlet
louis vuitton bags
gucci belts
michael kors handbags
louis vuitton purses
jordan retro 13
louis vuitton outlet
coach outlet
coach factory outlet online
michael kors outlet online
true religion outlet
replica watches
true religion jeans