2

I read the tutorial stuff about abstract classes and am a little unclear on something..

I have an abstract class

abstract class Master implements ActionListener {
    static Point p;
    static JFrame glass;

    /* code to define glass and a mouseListener to it.  
       The mouselistener essentially registers the mouseclick 
       and point P where it happened  */
}

And the I extend some others

class FirstMaster extends Master {
/* some definitions and stuff */
}

class SecondMaster extends Master {
/* some definitions and stuff */
}

My questions

  1. Is the static variable p and JFrame specific to both FirstMaster and SecondMaster, that is Master.p, FirstMaster.p and SecondMaster.p are the one and the same Point with a single memory location?
  2. Each (instance of) First and Second Master is an independent ActionListener and needs to define the associated method - correct?
  3. Without the modifiers static it would be equivalent to simply copy and paste the code in Master into the First and Second and get rid of the extends?

Sorry for the newbie question but I didnt see an example to figure it out definitively.

EDIT: Since there is a lot of info below I thought I would summarize my understanding of the answers for the benefit of subsequent readers (and please correct if I am mistaken)

  1. There is only one p. The extension does not create a new static p for each subclass and this fact is independent of the abstract modifier.
  2. the method actionPerformed() can be specified in Master or in BOTH First and Second definitions. Specifying it in First and Second allows the response to the action to be unique to each subclass.
  3. Ignoring the complication with the implement ActionListener, the answer is yes, although this would result in redundant code.

Thanks to all.

user3799584
  • 917
  • 1
  • 9
  • 18
  • Could you clarify question 1 some more? It seems like you are asking if a FirstMaster object f and SecondsMaster s have the exact same Point object p. – Simon Jul 12 '14 at 16:24
  • In contrast to some of the answers, I heavily disagree with point 3. Although inheritance is a way to "reuse" code, it is more likely a programming paradigm to define the hierarchy of objects, their polymorphism and contracts. It is not about simple "reusing" code. If you want to reuse code, you do better with composition, not with abstract classes. – Thomas Uhrig Jul 12 '14 at 16:32
  • @Simon this is an extension to question. As a static declaration it was my understanding that Master.p is the better programming practice. I was wanting to make sure FirstMaster.p and SecondMaster.p were the same (they would not be if they did not extend Master but i was unsure of how the abstract part played a role). I could also reference p via the instance f.p and s.p but the tutorials suggested this is bad practice. – user3799584 Jul 12 '14 at 18:06
  • 1
    @Thomas Uhrig. It was my question not my position. I am not asking if this is a general philosophy. I am trying to clarify conceptually what is the situation - from what I have seen the answer to the question as asked is YES. From a more general consideration simple reuse of code is not the whole purpose of the abstract class. – user3799584 Jul 12 '14 at 18:11

3 Answers3

2

Question 1: When you declare a static member object (field) in a top-level class, there is only one of that object in the entire program, period. If the object is x and it's in class Class1, Class1.x will refer to that object. If you have a subclass Class2 that extends Class1, Class2.x is another way to refer to the same object, if x isn't hidden by another declaration of some other x. But a new static object is not created for the subclass. None of this depends on whether the classes are abstract or not.

Question 2: Any methods declared in ActionListener need to be implemented, by writing bodies for the methods. You can implement them in Master or not. Whatever methods you don't implement in Master, you need to implement in both FirstMaster and SecondMaster. If you implement them in Master, you could still write overriding methods in FirstMaster or SecondMaster, but if you don't, they'll inherit the methods you wrote in Master. I'm not sure what you mean by "each instance" having to define the associated method; you do have to define it in each (non-abstract) subclass.

Question 3: Getting rid of extends would create a completely different program. The way you've written it, if you have a method that needs an ActionListener:

public void addListener(ActionListener listener);

you can pass it an instance of a FirstMaster or a SecondMaster, since instances of those classes are also indirectly instances of ActionListener. If you get rid of extends Master, you can't do that. Even if you get rid of extends Master and add implements ActionListener, you could no longer write a method like

public void addMasterListener(Master m);

In order to write a method like this and have it work, you need to be able to pass it instances of FirstMaster or SecondMaster. So (as Thomas Uhrig commented), using the extends is a way to improve your program by using polymorphism, not just a way to avoid code duplication.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • thank you. Regarding 2 it was poor wording on my part. It was not my intention to mean each instance of First and Second Master needs to define actionPerformed() but each class definition. I wanted to know if First and Second Master could each have a unique actionPerformed() method. Regarding 3 - yes the implements ActionListener muddies the question so to speak. – user3799584 Jul 12 '14 at 18:33
1

Is the static variable p and JFrame specific to both FirstMaster and SecondMaster

Right ,They will keep override value on that memory location.

Each (instance of) First and Second Master is an independent ActionListener and needs to define the associated method - correct?

If you want to say that whether both First and second master need to define associated method? If method's implementation is written in abstract class, In that case First master and Second master do't need to define that method but if that was ana abstract method(method without or incomplete implementation ) in that case both First and Second Master will have their own implemetation of that method.

Without the modifiers static it would be equivalent to simply copy and paste the code in Master into the First and Second and get rid of the extends?

Yes ,We use abstract class Abstract classes are useful when you need a class for the purpose of inheritance and polymorphism, but it makes no sense to instantiate the class itself, only its subclasses and it can implement some functionality in class.So it removes that boilerplate code in classes which extends it.

Abhijeet Panwar
  • 1,837
  • 3
  • 26
  • 48
  • thx - this helps heaps. Just to ensure my enlightenment. If I define actionPerformed() in Master then First and Second instance will have the same behaviors but I have the flexibility to have unique behaviors by specifying them in First and Second - right? – user3799584 Jul 12 '14 at 18:37
  • Yes. In Java you can override all methods not explicity declared as final in parent class , So you have flexibility to have unique behaviours :) – Abhijeet Panwar Jul 12 '14 at 18:45
1

Is the static variables p and JFrame specific to both FirstMaster and SecondMaster, that is Master.p, FirstMaster.p and SecondMaster.p are the one and the same Point with a single memory location?

There is a single p that is shared among all Masters, including FirstMaster and SecondMaster. The same is true with glass.

This is because p and glass are declared as "package protected", since you did not put private, protected, or public before them (as in public Point p). Package protected is useable by classes in the same package only (sub-classes or not). protected is usable only by sub-classes.

Each (instance of) First and Second Master is an independent ActionListener and needs to define the associated method - correct?

"The associated method"? What method are you referring to? If there is an abstract method in the Master base class, then yes, all children need to define that method.

It's more accurate to say that "each definition" of FirstMaster and SecondMaster must define abstract methods. Under normal circumstances, instances don't define methods.

Without the modifiers static it would be equivalent to simply copy and paste the code in Master into the First and Second and get rid of the extends?

Well, a potential beneficial side-effect of a super-class is to save you from having redundant code. In a tiny project, like this small example, this redundancy would be small, and copy-pasting would be easily maintained.

In an even moderately large project, redundant code can grind a project to a halt.

So I would always choose to have a super-class (or some other form or preventing redundant code, such as a utility class) when it makes sense from a design point of view. I'd rarely choose to have redundant code. This is because projects generally tend to become bigger than you realize, and you don't want to be stuck with a short-sighted design decision for the lifetime of a project.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
  • Thanks for the info on the public/private info. Associated method refers to actionPerformed() - the one and only method required by the ActionListener interface. And yes - my wording was poor - i did not mean to imply instances were defining the method. I was rather unclear concerning the difference between having the implements ActionListener with definition of the abstract Master vs attaching them to First and Second definitions independently. I think there is not correct? – user3799584 Jul 12 '14 at 18:44