Extending factory-made class

A library, for instance JDK core APIs, usually offers instances of internal concrete class. For users
of the library, the class is usually given as an abstract class. Some of them may or may not have static factory method that delivers a concrete class instance. For example, FileSystemView has
and Graphics hasn't. The difference is not important for the problem of this thread.
We can get those concrete class by calling Object#getClass() method. But how could we extend
the class and override just one or two methods for trivial, but important, customization? In other
words, we'd like to use a major part of the implementation as is, not implementing the abstract
class from scratch.

We can get those concrete class by calling
Object#getClass() method. But how could we extend
the class and override just one or two methods for
trivial, but important, customization? In other
words, we'd like to use a major part of the
implementation as is, not implementing the abstract
class from scratch.I'm not sure I fully understand what you want to do but I think you could use the idea behind the Adapter pattern. You create an adapter class with the public interface you want and then you use an object of the class you want to customize (the adptee) to implement that adapter class.
Most adapter class methods will just call the corresponding methods of the adaptee but some are implemented differently.

Similar Messages

  • Extending the Dialog Class

    Hey, I'm trying to extend the Dialog class into something called a DialogErrorBox, which is just what it sounds like: a dialog box specifically for telling the user about errors it encounters. Here's the code:
    import java.awt.*;
    import java.awt.event.*;
    class DialogErrorBox extends Dialog implements ActionListener {
         public DialogErrorBox (Frame parent) {
        ...//modified to give a vague error message
         }//end constructor
    public DialogErrorBox (Frame parent, String title, String message) {
        ...//worked as a function in my program
         }//end constructor
    public void actionPerformed (ActionEvent evt) {
              this.dispose();
    }The compiling error I recieve at both constructors is:
    ...cannot resolve symbol
    symbol  : constructor Dialog  ()
    location: class java.awt.Dialog
            public DialogErrorBox2 (Frame parent, String title, String message) {I can't figure out why this error would happen. I've made a class extending Frame(Frame and Dialog both extend Window) that looks almost exactly the same and it has no errors.
    Help would be much appreciated.

A: Extending the Dialog Class

This is a short and spotty explanation, to get the full skinny you might read pages 69 and 70 of The Java Programming Language 3rd Edition, Gosling, et. al.
If you don't use the superclass's constructor or one your own as the first executable statement , the superclass's no arg constructor gets called. This means that super() will get called automagically! I don't believe that Dialog has a no arg constructor, so you are forced to do something.
You could also do something like this:
public DialogErrorBox(Frame frame, String title) {
   this(frame, title, true);
}Now the two argument constructor invokes the three arument constructor with a default value of true.
Do you see why this must happen?

This is a short and spotty explanation, to get the full skinny you might read pages 69 and 70 of The Java Programming Language 3rd Edition, Gosling, et. al.
If you don't use the superclass's constructor or one your own as the first executable statement , the superclass's no arg constructor gets called. This means that super() will get called automagically! I don't believe that Dialog has a no arg constructor, so you are forced to do something.
You could also do something like this:
public DialogErrorBox(Frame frame, String title) {
   this(frame, title, true);
}Now the two argument constructor invokes the three arument constructor with a default value of true.
Do you see why this must happen?

  • Extending a nested class and parent access

    I want the child of the abstract inner class to have access to the inner classes parent's variables. I know that an inner class can access it's parent's variables and methods with the "[ParentClass].this.[var/method]" syntax. What's the appropriate syntax for the situation I'm describing?
    My setup is like this:
    class Canister {
      int a = 0;
      public abstract class Behavior
    class Lonely Behavior extends Behavior
      public int getParentVariable() {
        return Canister.this.a;
    }Thanks!
    -stephen

    The correction to my code is:
    class Canister
      int a = 0;
      public abstract class Behavior
    class LonelyBehavior extends Canister.Behavior
      public int getParentVariable() {
        return Canister.this.a;
    }The compiler doesn't have a problem with me extending Canister.Behavior because it's public inside of Canister. But as I explained, the "[ParentClass].this.[var/method]" syntax doesn't work.
    This is inconsistent with inheritance: you'd think that if you extend an inner class, you would inherit the abilities and characteristics of that inner class, just as you would in other cases of inheritance. It's also inconvenient: if the inner class is abstract because you want programmers to extend and upgrade it, those programmers have to change the source code of the enclosing class to include the new classes.
    Anyway, thanks for the info.

  • EXTENDING the string class

    ok, i know extending the string class is illegal because it's final, but i want to make an advanced string class that basically "extends" the string class and i've seen online this can be done through wrapper classes and/or composition, but i'm lost
    here is my sample code that is coming up with numerous compile time errors due to the fact that when i declare a new AdvString object, it doesn't inherit the basic string features (note: Add is a method that can add a character to a specified location in a string)
    class AdvString
         private String s;
         public AdvString(String s)
              this.s = s;
         public void Add(int pos, char ch)
              int this_len = (this.length()) + 1;
              int i;
              for(i=0;i<(this_len);i++)
                   if(pos == i)
                        this = this + ch;
                   else if(pos < i)
                        this = this + this.charAt(i-1);
                   else
                        this = this + this.charAt(i);
         public static void main(String[] args)
              AdvString s1;
              s1 = new AdvString("hello");
              char c = 'x';
              int i = 3;
              s1.Add(i,c);
              //s2 = Add(s1,i,c);
              //String s2_reversed = Reverse(s2);     
              System.out.println("s1 is: " + s1);
    any tips?

    see REString at,
    http://www.geocities.com/rmlchan/mt.html
    you will have to replicate all the String methods you are interested in, and just forward it to the String instance stored in REString or the like. it is like a conduit class and just passes most processing to the 'real' string. maybe a facade pattern.

  • Can an abstract class extend another abstract class?

    Is it possible for an abstract class to extend another abstract class?
    Say
    public abstract class AComponent extends JComponent {
    Thanks.

    Yes

  • Inner Class extending the outer class

    Could anyone explain this code to me? It can compile and run. Could anyone tell me what the class Main.Inner.Inner is? What members does it consists of? How the compiler manage to build such a class?
    public class Main {
        public static class Inner extends Main {
        public static void main(String[] args) {
            System.out.println("Hello, world.");
    }

    By the way, it's not really an inner class, because it's static. It's just a nested class.
    You might want to have a nested class extend its enclosing class if, maybe, you wanted to delegate to subclasses and didn't want to create a lot of extra source code files, which might make sense if the nested subclasses were really small.
    public abstract class Animal {
      public abstract void makeSound();
      private Animal() {} // can't be directly instantiated
      private static class Dog extends Animal {
        public void makeSound() { System.out.println("woof"); }
      private static class Cat extends Animal {
        public void makeSound() { System.out.println("meow"); }
      private static class Zebra extends Animal {
        public void makeSound() { System.out.println("i am a zebra"); }
      public static Animal get(String desc) {
        if ("fetches sticks".equals(desc)) {
          return new Dog();
        if ("hunts mice".equals(desc)) {
          return new Cat();
        if ("stripey horse".equals(desc)) {
          return new Zebra();
        return null;
    public class AnimalTest {
      public static void main(String... argv) {
        Animal a = Animal.get("fetches sticks");
        a.makeSound();
    }

  • Extending an inner class (6 lines of code)

    // Mammal.java
    public class Mammal {
      public class Hand {
    // Human.java
    public class Human extends Mammal {
      public class Hand extends Mammal.Hand {  // (1)
    }This gives me compilation error at (1).
    If I change Human.java as below, the code works.
    // Human.java
    public class Human extends Mammal {
      public class HumanHand extends Mammal.Hand {
    }If I want my original class name "Human.Hand", what should I do?

    the compiler certainly doesn't accept 2 public class with the same name.

  • Confused about extending the Sprite class

    Howdy --
    I'm learning object oriented programming with ActionScript and am confused about the Sprite class and OO in general.
    My understanding is that the Sprite class allows you to group a set of objects together so that you can manipulate all of the objects simultaneously.
    I've been exploring the Open Flash Chart code and notice that the main class extends the Sprite class:
    public class Base extends Sprite {
    What does this enable you to do?
    Also, on a related note, how do I draw, say, a line once I've extended it?
    Without extending Sprite I could write:
    var graphContainer:Sprite = new Sprite();
    var newLine:Graphics = graphContainer.graphics;
    And it would work fine. Once I extend the Sprite class, I'm lost. How do I modify that code so that it still draws a line? I tried:
    var newLine:Graphics = this.graphics;
    My understanding is that since I'm extending the Sprite class, I should still be able to call its graphics method (or property? I have no idea). But, it yells at me, saying "1046: Type was not found or was not a compile-time constant: Graphics.

    Thanks -- that helped get rid of the error, I really appreciate it.
    Alas, I am still confused about the extended Sprite class.
    Here's my code so far. I want to draw an x-axis:
    package charts {
        import flash.display.Sprite;
        import flash.display.Graphics;
        public class Chart extends Sprite {
            // Attributes
            public var chartName:String;
            // Constructor
            public function Chart(width:Number, height:Number) {
                this.width = width;
                this.height = height;
            // Methods
            public function render() {
                drawAxis();
            public function drawAxis() {
                var newLine:Graphics = this.graphics;
                newLine.lineStyle(1, 0x000000);
                newLine.moveTo(0, 100);
                newLine.lineTo(100, 100);
    I instantiate Chart by saying var myChart:Chart = new Chart(); then I say myChart.render(); hoping that it will draw the axis, but nothing happens.
    I know I need the addChild method somewhere in here but I can't figure out where or what the parameter is, which goes back to my confusion regarding the extended Sprite class.
    I'll get this eventually =)

  • Can we extend the Throwable class instead of Exception Class??

    Hi all..
    Can we extend the Throwable class instead of Exception Class while creating our own custom Exception?If not Why?
    Please give your valuble advices..
    Ramesh.

    I don't want to hijack the thread here, but in a conversational tone...on a related note.. I've thought about this too a bit and wondered if there are some recommended practices about catching and handling Throwable in certain applications. Like the other day I was debugging a web application that was triggering a 500. The only way I could find the problem in an error stack was to write code to catch Throwable, log the stack, and then re-throw it. I considered it "debug" code, and once I solved the problem I took the code out because, my understanding is, we don't want to be handling runtime problems... or do we? Should I have a catch clause for Throwable in my servlet and then pass the message to ServletException?
    So along with the OP question, are there separate defined occasions when we should or should not handle Throwable? Or does it just all depend on circumstance?

  • Extending inner [nested] classes

    OK I've got a class (A) with a nested class (B).
    B accesses variables etc. in A and does its own thing, but what I'd like to do is make B abstract and have a number of classes extend it - which is used depends on program arguments.
    The problem I'm having is that the new class (C) that extends B does not seem to have access to any of the variables in A.
    I originally tried:
    class C extends B
    and
    class C extends A$B
    I also tried this, which I found on this site:
    class C extends A.B
    (thats A-dot-B) but it still doesnt seem to be working.
    Is it possible to extend [abstract] inner classes like this? If so, how? Any help appreciated. cheers.

    Here I found a solution. However, it is not so elegant. In fact, I am also having trouble in extending a nested class in an elegant way.
    Here, A is the original top-level class and A.B is the nested class of A. A2 extends A. B2 and B3 extends A.B. Main is the class where the main() method can be found.
    // A.java
    public class A {
      private B b;
      public void setB (B b) {
        this.b = b;
      public B getB () {
        return b;
      public abstract class B {
        public abstract String getName();
    // A2.java
    public class A2 extends A {
      public class B2 extends A.B {
        public String getName () {
          return "Peter";
      public class B3 extends A.B {
        public String getName () {
          return "John";
    public class Main {
      public static void main (String[] args) {
        A a = new A2 ();
        A.B b = ((A2)a).new B2 ();
        a.setB (b);
        System.out.println (a.getB().getName());  // John
        b = ((A2)a).new B3 ();
        a.setB (b);
        System.out.println (a.getB().getName());  // Peter
    }You may see here that I used "A" as the type of "a". Te reason is I want to use "a" as an "A", not "A2". If you don't want to do casting, you may simply use "A2" as the type. The class A2 can be blank. It is only a "container" of the classes B2 and B3.

  • How can I extend the Vector class?

    Hi All,
    I'm trying to extend the Vector class so I can get add a .remove(item:T) method to the class.  I've tried this:
    public class VectorCollection extends Vector.<T>
    That gives me the compile error "1017: The definition of base class Vector was not found"
    So then I tried:
    public class VectorCollection extends Vector
    Which gives me the compile error "1016: Base class is final."
    There must be some way to extend the Vector class, though, as on the Vector's official docs page it says:
    Note: To override this method in a subclass of Vector, use ...args for the parameters, as this example shows:
         public override function splice(...args) {
           // your statements here
    So there must be a way to extend the Vector class.  What am I doing wrong?

    No. AS3 doesn't currently have full support for generic types; Vector.<T> was added in an ad-hoc ("ad-hack"?) way. We are considering adding full support in a future version of ActionScript.
    Gordon Smith
    Adobe Flex SDK Team

  • How to extend factory calender to a plant ?

    Hi Ranga:
    How to extend factory calender to a plant ? ( Tcode: SCAL, The calender is not client specific)
    I check marked US factory calender, where after can you tell how to extend factory calender to plant
    Note: I am using IDES ( International Demonstration & Education System)
    Thanks

    Hi Sandeep,
    you need to use the following path
    Go to SPRO>ENTERPRISE STRUCTURE->DEFINITION> LOG GENERAL>DEFINE COPY,DELETE AND CHECK PLANT>DEFINE PLANT
    Here you need to assign the factory calendar. The assignment in work center will only applicable for capacity not for MRP and others.
    <b>For information how to create a factory calendar</b>
    Pl follow the steps
    1.Go to SCAL transaction
    2.there will be three options.
    Click first public holidays and go in change mode.
    Click create and create your holidays there and save.(Generally fixed date will be used in the pop up)
    3.Now click Holiday calendar and go in change mode.
    Click create and give holdiay cal id and description.
    Click assign public holiday and add your holidays one by one and save
    Now holiday cal is created.
    4.Now come out and choose fact calendar and go in change mode
    Click create and give Factory calendar id and description, and validity period.
    Give the holiday calendar ID.
    If you want to give special rule like any of the specific date/ day is the holiday or work day (which is different from holiday calendar you can define)
    and save.
    5.You have to assign factory calendar to PLANT
    Go to SPRO>ENTERPRISE STRUCTURE->DEFINITION> LOG GENERAL>DEFINE COPY,DELETE AND CHECK PLANT>DEFINE PLANT
    Choose your plant and go to details-
    You have to define factory calendar there
    Hope this will help you
    Regards
    Ranga

  • (Class ? extends Set String ) Class.forName("example.SetImpl").asSubclass

    Is there a way to do the following without an Unchecked cast?
    Class<? extends Set<String>> clazz;
    clazz = (Class<? extends Set<String>>) Class.forName("example.SetImpl").asSubclass(Set.class);
    Set<String> mySet = clazz.newInstance();
    cheers,
    reto

    No. The last line will always generate that warning making the second line pointless. There is no way to create a new instance of a generic class because there is (for most intents and purposes) no such thing as a generic class at runtime.

  • How to extend an AS3 class in an MXML Component

    Hello,
    I have a few MXML Components based on the Spark Group.
    They all use MXML for layouta and AS3 for logic.
    They all have the same logic and I would like all to extend an AS3 class to have the same methods and properties isteand of declaring the same exact ones for each MXML Component.
    How can this be done please?

    Hm, no, I'm not explaing this properly.
    I have an MXML component based on the Spark Group.
    It has a few form elements.
    There are many such components.
    I want all to have two public methods: getData() and setData() which get and set data from and to the input elements.
    I don't want to write the exact same method for all of them.
    I would like to at least interface the MXML components with an AS3 class forcing them to have those 2 methods.

  • Extending the Thread class

    i would like to do that
    1) One thread displays "ABC" every 2 second;
    2) The other thread displays DEF every 5 seconds;
    i need to create the threads by extending the Thread class ...
    thank you for your help ,
                public class Thread1 extends Thread {
              public Thread1(String s ) {
                   super (s);
              public void run() {
                   for ( int i=0; i<5; i++ ) {
                        System.out.println(getName());
                        try {
                           sleep ((long) 5000);
                        } catch (InterruptedException e ) {
                           /* do nothing */
              public static void main (String args[]) {
                   new Thread1 ("ABC").start();
                   new Thread1 ("DEF").start();
         }     

    I think he has been told to use the Thread class by the sounds of it.
    public class Thread1 extends Thread {
         public Thread1(String s ) {
              super (s);
         public void run() {
              for ( int i=0; i<5; i++ ) {
                   System.out.println(getName());
                   try {
                      sleep (getName().equals("ABC")? 5000 : 2000); //If you don't understand this then Google for "Java ternary operator"
                   } catch (InterruptedException e ) {
                      /* do nothing */
         public static void main (String args[]) {
              new Thread1 ("ABC").start();
              new Thread1 ("DEF").start();
    }

  • Maybe you are looking for

    • Best App to covert .wmv to .mpg

      I was wondering what is the best App out there to convert .wmv to .mpg. I have Flip4Mac but I want to actually convert the file. Thanks

    • Can't connect to wireless network that works fine for other machines

      I received my MacBook Pro last week (2.16 GHz) and I've been very happy with it so far. However, when I brought it home, I've had nothing but issues trying to connect to my wireless network. It's a Netgear, and you can read more about it at the follo

    • Update infotype only by PA40

      Hi gurues, first off, excuse me for asking so much questions, Im newbie in HR. Ihave an infotype and I want it to be updated only by menas of actions (thats to say, not in PA30, only display it but not update it), is it possible? Thanks in advance

    • Java 5 (1.5) no need for stubs?!! (codebase)

      I am trying to run my program under java 1.5, without compiling with rmic!! I still have get this exception when I try to export the server using UnicastRemoteObject.exportObject(this); Stub class not found!!!! If the answer is using codebase, how ca

    • Using Multiple Generic Info Panels

      Hi: I had an info panel that was working fine in CS6 for production metadata on Photoshop images. I then wanted to do a second panel with licensing metadata (separating it from the production stuff). I duped the production panel, changed the name of