1

While developing java stand alone applications , some developers tend to extend JFrame class and Implement(realize) some actionlistener interface . Does it not violate The generalization concept in oops and also abstraction . Does it not violate Single responsibility principle too.

[EDIT] By violate The generalization concept in oops , i mean "is-a" relationship becomes void. Inherit a class changed to extend a class

Abdul Rahman K
  • 664
  • 5
  • 16
  • What according to you is the generalization concept and abstraction? You may be right abut the single responsibility principle violation but I need to see the code to confirm this. – Chetan Kinger Jul 18 '15 at 11:26
  • I'm not ready with the code yet. But abstraction means making the object do things and contains things ,that is relevant to the object. And to give a UI to any executor program which we do by inheriting JFrame we violate generalization. Is 'nt it? – Abdul Rahman K Jul 18 '15 at 11:32

2 Answers2

3

This frequently seen anti-pattern poses several problems:

class Application extends JFrame implements ActionListener {}

Implementing a control interface, such as ActionListener, may be convenient for a self-contained example, but a complex application will likely need more than one controller. An example using is examined here. See also Why CS teachers should stop teaching Java applets.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I didnt confuse with containment hierarchy, but was curious if IS-A relationship being violated as a result? – Abdul Rahman K Jul 18 '15 at 13:19
  • Sorry if I seemed [sententious](https://www.google.com/search?q=sententious) about potential confusion. Based on the [API](https://docs.oracle.com/javase/8/docs/api/javax/swing/JFrame.html) and [history](http://stackoverflow.com/q/2004759/230513), I would say that the `JFrame` derivation is reasonably coherent. Do you see a specific anomaly? – trashgod Jul 18 '15 at 19:34
  • It will accept the answer if you can brief about vioaltion of single responsibility principle – Abdul Rahman K Jul 19 '15 at 02:27
1

Your question is a bit confusing and hard to understand, because you're using strange terminology. These sentences are correct and mean something:

  • a class extends another class
  • a class implements an interface
  • a class inherits methods from its parent class

These sentences are inappropriate, unclear:

  • a class inherits another class
  • violate generalization concept

Especially without code, I can only guess what you're asking, but I try to answer anyway:

  • a class that extends JFrame and at the same time implements an action listener interface, violates the single responsibility principle by being 2 things at once
  • a class that extends JFrame and at the same time contains some logic not related to displaying or configuring a JFrame, violates the single responsibility principle by doing 2 things at the same time. It also violates good separation of model-view-controller
  • a class that extends JFrame, but in terms of its behavior / implementation it's not mainly a JFrame but something else, then it violates the abstraction of a JFrame, and it shouldn't extend JFrame but possibly contain it instead
janos
  • 120,954
  • 29
  • 226
  • 236