6 Replies - 235 Views - Last Post: 04 June 2012 - 09:53 AM Rate Topic: -----

#1 oha055  Icon User is online

  • D.I.C Regular

Reputation: 49
  • View blog
  • Posts: 263
  • Joined: 02-February 09

Overriding methods

Posted 04 June 2012 - 07:16 AM

Say you have an instance of a jpanel. Is it possible to override e.g. its paintComponent method without actually subclassing it?
Is This A Good Question/Topic? 0
  • +

Replies To: Overriding methods

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7806
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: Overriding methods

Posted 04 June 2012 - 07:55 AM

View Postoha055, on 04 June 2012 - 09:16 AM, said:

Say you have an instance of a jpanel. Is it possible to override e.g. its paintComponent method without actually subclassing it?



No. Methods are not first-class objects in Java, so this kind of thing isn't possible.

If you have a final class, for example a String, and you want to "override" one of its methods, the closest you can do would be to wrap it in some class of your own and write a bunch of passthrough methods for everything you didn't want to override, and write a replacement method for the one you did want to replace. This is pretty limited, though. You wouldn't have access to anything but the public fields and methods, and you wouldn't be in the inheritance hierarchy. This would not be an instanceof String.


EDIT: But pbl's answer below is probably what you're looking for.

This post has been edited by jon.kiparsky: 04 June 2012 - 09:57 AM

Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Overriding methods

Posted 04 June 2012 - 09:27 AM

:)
public class MyFrame extends JFrame {

	MyFrame() {
		super("Foo");
		JPanel p = new JPanel() {
			public void paintComponent(Graphics g) {
				g.drawString("Hello Jon", 30, 30);
			}
		};
		add(p);
	}
	
	public static void main(String[] args) {
		JFrame myFrame = new MyFrame();
		myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		myFrame.setSize(300, 200);
		myFrame.setVisible(true);
	}
}


Was This Post Helpful? 2
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7806
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: Overriding methods

Posted 04 June 2012 - 09:35 AM

Actually, you're subclassing there - there's a bit of syntactic sugar, but you're creating an anonymous class which subclasses JPanel.

Notice that you can't do this:

	  String s = new String() {
		  public int charAt(int index)
		  {
		   return 7;
		  }
	  };


Close, but no cigar. :)
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Overriding methods

Posted 04 June 2012 - 09:38 AM

You are right Jon, but I guess the OP question was not correctly formulated... and I guess that this is exactly what he wanted :)
Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7806
  • View blog
  • Posts: 13,200
  • Joined: 19-March 11

Re: Overriding methods

Posted 04 June 2012 - 09:42 AM

I think you're right - your answer is definitely what he was looking for.

But the answer to the question he actually asked... is no. :)
Was This Post Helpful? 0
  • +
  • -

#7 oha055  Icon User is online

  • D.I.C Regular

Reputation: 49
  • View blog
  • Posts: 263
  • Joined: 02-February 09

Re: Overriding methods

Posted 04 June 2012 - 09:53 AM

Thanks for the replies! Very informative :)

EDIT: Yes, this was something along the lines of what I was thinking about:
JPanel p = new JPanel() {
	            public void paintComponent(Graphics g) {
	                g.drawString("Hello Jon", 30, 30);
	            }
	        };


This post has been edited by oha055: 04 June 2012 - 09:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1