Abstract Classes vs Private Constructor

Hopefully this doesn't make me sound to naive
If we have a super class called food, and subclasses chocolate, chicken, cereal and so on then its clear we dont want to make instances of the super class food correct? So we can make this class an abstract class
What are the benefits of making a class abstract over making a constructor private and in what circumstance would it be beneficial to do one over the other?
Thanks, and sorry for a poor example!
Edited by: compSciUndergrad on Apr 22, 2009 4:53 AM

wrybread wrote:
Also, Corlettk -
Excellent point regarding prudent use of interfaces; thank you. If you read this, I'd appreciate any comment you have on how conservative programmers need to be in designing an interface. It seems to me that if I'm uncertain whether or not to include a certain member in the interface, I should omit it or include it elsewhere (perhaps in an interface all its own, or in an abstract super if that's applicable).
Let me know if you want me to open this as a new thread; I'm just looking for your take on this topic. -- Thank you!Obviously, there's no right answer. Each deisgn must be assessed on a case-by-case basis. Every design presents new and interesting problems.
I'm no OO guru... what I do know is that you can make some very nasty mistakes, thanks to experience of maintaining a fairly poorly written (first in the organisation) large java project.
I'm of the opinion that interface should define an aspect of behaviour... If the Sun GODS had there time again they'd do java.io would be full of interfaces... Closeable, Openable, Readable, Writable, Printable... etc etc etc.
Please remember that a class (or group of classes) can implement multiple interfaces... and an interface can extend multiple interfaces... So java supports "multiple type inheritance" as apposed to C++'s "multiple implementation inheritance".
If you need an "aglomerate" interface as the type for a collection then assemble one from your premade "set of aspects"... eg: Input extends Openable, Closeable, Readable...
package forums;
interface AnInterface
  public void a();
interface AnotherInterface
  public void b();
interface AglomerateInterface extends AnInterface, AnotherInterface
  public void c();
class AglomerateImpl implements AglomerateInterface
  public static void main(String[] args) {
    try {
      System.out.println("Hello World!");
    } catch (Exception e) {
      e.printStackTrace();
  public void a() {}
  public void b() {}
  public void c() {}
}PS: You wouldn't normally define multiple interfaces and an implementation all in one file... sort of defeats the whole purpose really... and in fact you can't as soon as you make the interfaces public, which they need to be to be any use.
Make sense?

Similar Messages

  • Why can't classes with private constructors be subclassed?

    Why can't classes with private constructors be subclassed?
    I know specifying a private nullary constructor means you dont want the class to be instantiated or the class is a factory or a singleton pattern. I know the workaround is to just wrap all the methods of the intended superclass, but that just seems less wizardly.
    Example:
    I really, really want to be able to subclass java.util.Arrays, like so:
    package com.tassajara.util;
    import java.util.LinkedList;
    import java.util.List;
    public class Arrays extends java.util.Arrays {
        public static List asList(boolean[] array) {
            List result = new LinkedList();
            for (int i = 0; i < array.length; i++)
                result.add(new Boolean(array));
    return result;
    public static List asList( char[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Character(array[i]));
    return result;
    public static List asList( byte[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Byte(array[i]));
    return result;
    public static List asList( short[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Short(array[i]));
    return result;
    public static List asList( int[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Integer(array[i]));
    return result;
    public static List asList( long[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Long(array[i]));
    return result;
    public static List asList( float[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Float(array[i]));
    return result;
    public static List asList( double[] array) {
    List result = new LinkedList();
    for (int i = 0; i < array.length; i++)
    result.add(new Double(array[i]));
    return result;
    // Now that we extend java.util.Arrays this method is not needed.
    // /**JCF already does this so just wrap their implementation
    // public static List asList(Object[] array) {
    // return java.util.Arrays.asList(array);
    public static List asList(Object object) {
    List result;
    Class type = object.getClass().getComponentType();
    if (type != null && type.isPrimitive()) {
    if (type == Boolean.TYPE)
    result = asList((boolean[])object);
    else if (type == Character.TYPE)
    result = asList(( char[])object);
    else if (type == Byte.TYPE)
    result = asList(( byte[])object);
    else if (type == Short.TYPE)
    result = asList(( short[])object);
    else if (type == Integer.TYPE)
    result = asList(( int[])object);
    else if (type == Long.TYPE)
    result = asList(( long[])object);
    else if (type == Float.TYPE)
    result = asList(( float[])object);
    else if (type == Double.TYPE)
    result = asList(( double[])object);
    } else {
    result = java.util.Arrays.asList((Object[])object);
    return result;
    I do not intend to instantiate com.tassajara.util.Arrays as all my methods are static just like java.util.Arrays. You can see where I started to wrap asList(Object[] o). I could continue and wrap all of java.util.Arrays methods, but thats annoying and much less elegant.

    Why can't classes with private constructors be
    subclassed?Because the subclass can't access the superclass constructor.
    I really, really want to be able to subclass
    java.util.Arrays, like so:Why? It only contains static methods, so why don't you just create a separate class?
    I do not intend to instantiate
    com.tassajara.util.Arrays as all my methods are static
    just like java.util.Arrays. You can see where I
    started to wrap asList(Object[] o). I could continue
    and wrap all of java.util.Arrays methods, but thats
    annoying and much less elegant.There's no need to duplicate all the methods - just call them when you want to use them.
    It really does sound like you're barking up the wrong tree here. I can see no good reason to want to subclass java.util.Arrays. Could you could explain why you want to do that? - perhaps you are misunderstanding static methods.
    Precisely as you said, if they didn't want me to
    subclass it they would have declared it final.Classes with no non-private constructors are implicitly final.
    But they didn't. There has to be a way for an API
    developer to indicate that a class is merely not to be
    instantiated, and not both uninstantiable and
    unextendable.There is - declare it abstract. Since that isn't what was done here, I would assume the writers don't want you to be able to subclass java.util.Arrays

  • Class with private constructor can be extended or not

    Hi All,
    I have a doubt.
    if a class has private constructor and there are some methods in this class.Can this class be extended and if yes how can we call its method in subclass?
    Thanks
    Sumit

    Karanjit wrote:
    If a class contains only private constructors, then it cannot be extended.Err... not the whole story!
    public class Sabre20090603a
        static class Fred extends Sabre20090603a
            Fred()
                super();
        private Sabre20090603a()
    }

  • Help: Factory Class using Inner Class and Private Constructor?

    The situation is as follows:
    I want a GamesCollection class that instantiates Game objects by looking up the information needed from a database. I would like to use Game outside of GamesCollection, but only have it instantiated by GamesCollection to ensure the game actually exist. Each Game object is linked to a database record. If a Game object exist, it must also exist in the database. Game objects can never be removed from the database.
    I thought about making the Game object an inner class of GamesCollection, but this means that Game class constructor is still visible outside. So what if I made Game constructor private? Well, now I can't create Game objects without a static method inside Game class (static Object factory).
    Basically what I need is a constructor for the inner Game class accessible to GamesCollection, but not to the rest of the world (including packages). Is there a way to do this?

    leesiulung wrote:
    As a second look, I was initially confused about your first implementation, but it now makes more sense.
    Let me make sure I understand this:
    - the interface is needed to make the class accessible outside the outer classBetter: it is necessary to have a type that is accessible outside of GameCollection -- what else could be the return type of instance?
    - the instance() method is the object factory
    - the private modifier for the inner class is to prevent outside classes to instantiate this objectRight.
    However, is a private inner class accessible in the outer class? Try it and see.
    How does this affect private/public modifiers on inner classes?Take about five minutes and write a few tests. That should answer any questions you may have.
    How do instantiate a GameImpl object? This basically goes back to the first question.Filling out the initial solution:
    public interface Game {
        String method();
    public class GameCollection {
        private static  class GameImpl implements Game {
            public String method() {
                return "GameImpl";
        public Game instance() {
            return new GameImpl();
        public static void main(String[] args) {
            GameCollection app = new GameCollection();
            Game game = app.instance();
            System.out.println(game.method());
    }Even if you were not interested in controlling game creation, defining interfaces for key concepts like Game is always going to be a good idea. Consider how you will write testing code, for example. How will you mock Game?

  • Private inner class with private constructor

    I read that if constructor is public then you need a static method to create the object of that class.
    But in the following scenario why I am able to get the object of PrivateStuff whereas it has private constructor.
    I am messing with this concept.
    public class Test {
          public static void main(String[] args) {          
               Test t = new Test();
               PrivateStuff p = t.new PrivateStuff();
          private class PrivateStuff{
               private PrivateStuff(){
                    System.out.println("You stuff is very private");
    }

    A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:
    * Otherwise, if the member or constructor is declared private, then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. [Java Language Specification|http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.1]
    Your main method is within the body of the top level class, so the type (the inner class) and method are accessible.
    eg:
    class ImInTheSameSourceFileAsInnerAccessTest {
        public static void main(String[] args) {          
            InnerAccessTest t = new InnerAccessTest();
            InnerAccessTest.PrivateStuff p = t.new PrivateStuff();
    public class InnerAccessTest {
          public static void main(String[] args) {          
               InnerAccessTest t = new InnerAccessTest();
               PrivateStuff p = t.new PrivateStuff();
          private class PrivateStuff{
               private PrivateStuff(){
                    System.out.println("You stuff is very private");
    }Result:
    $ javac -d bin src/InnerAccessTest.java
    src/InnerAccessTest.java:4: InnerAccessTest.PrivateStuff has private access in InnerAccessTest
    InnerAccessTest.PrivateStuff p = t.new PrivateStuff();
    ^
    src/InnerAccessTest.java:4: InnerAccessTest.PrivateStuff has private access in InnerAccessTest
    InnerAccessTest.PrivateStuff p = t.new PrivateStuff();
    ^
    2 errors
    Edited by: pm_kirkham on 20-Jan-2009 10:54 added example of 'in the same source file'

  • Enclosing class calling private constructor of private member class

    Hi all,
    I have this question concerning member classes and privtae constructors.
    public class MyTest {
        private class Inner {
            private Inner() {
                System.out.println("Why Am I here!??");
        public MyTest() {
            Inner a = new Inner();
        public static void main(String[] s) {
            MyTest z = new MyTest();
    }It doesn't work for my JDK SE 1.3.3, build 1.3.1-b24.
    It works for many other versions.
    Can somebody kindly enlighten me, should this code work?
    I really didn't think that it should, but it did!

    I am sorry. It was actually my jikes 1.15 that was causing the problem.
    After some research, I found out that my problem arose out of my understanding of OO concepts, or rather, the meaning of access modifiers in Java.
    I had thought that nobody can access a private variable/method of class except the class itself. Apparently, this is not so. The access modifiers apply to the class themselves and not the object. Thus explaining why an object can access the private variables/methods of another object of the same class.
    Actually, it's not really the case here. The Java language specs states that the inner class has total access to the enclosing class, but I could not find any word on enclosing class access to inner classes in the specs.
    As for Jikes, I really hope they will fix it soon. I like it a lot as it is significantly faster than javac for everything I have done so far.
    cheers!

  • Final class and private constructor

    Whats the difference in a final class and a class with private constructot?
    If we can make a class non-extendable by just giving private constructor then whats the advantage of final class? (I know final is very useful for many other things but just want to get info in this contaxt)

    You can extend a class with a private constructor,I'm not sure about that. The compiler will complain.
    KajThat depends on the signature of the private constructor.
    If it's the no-arg constructor and you don't declare another constructor which you explicitly call from your derived class you will indeed get an error.
    If however you never call it (and there's no way it can implicitly get called) from a derived class there should be no problem.
    class Private1 {
         private int q;
         private Private1() {
         public Private1(int i) {
              q = i;
         public void print() {
              System.out.println(q);
    public class Public1 extends Private1 {
         public Public1() {
              super(1);
         public static void main(String[] args) {
              new Public1().print();
    }for example compiles and runs perfectly.
    But remove the call "super(1);" from the constructor and it will fail to compile.

  • Abstract classes and Constructors

    I am wiondering how can an Abstract class have a constructor?
    Won't the constructor of the derived class
    automatically invoke the constructor of Shape
    by calling super()implicitly
    That means that an object of type Shape has been created
    Or have I overlooked a certain point?
    abstract class Shape {
         public String color;
         public Shape() {
         public void setColor(String c) {
              color = c;
         public String getColor() {
              return color;
         abstract public double area();
    public class Point extends Shape {
         static int x, y;
            // ?? Wont this constructor invoke the super class's constructor?
         public Point() {
              x = 0;
              y = 0;
         public double area() {
              return 0;
         public static void print() {
              System.out.println("point: " + x + "," + y);
         public static void main(String args[]) {
              Point p = new Point();
              p.print();
    }

    bhuru_luthria wrote:
    I am wiondering how can an Abstract class have a constructor?
    Won't the constructor of the derived class
    automatically invoke the constructor of Shape
    by calling super()implicitly If you don't explicitly invoke a superclass c'tor, super() will be called, but you can also explicitly invoke any non-private parent constructor from the child's constructor.
    That means that an object of type Shape has been createdC'tors don't create objects. They initialize newly created objects to valid state.

  • Abstract classes: Why do they need constructors?

    As we cannot instantiate an abstract class. Why does an abstract class need a constructor?.can anyone please help me out..Thnq in advance
    OBELISK

    import java.util.*;
    import static java.lang.System.out;
    interface LineItem{
        public int getCost();
    abstract class AbstractLineItem implements LineItem{
      protected String name;
      AbstractLineItem(String name){
         this.name = name;
      public String toString(){
        return this.name+" "+getCost();
      public abstract int getCost();
    class ProductItem extends AbstractLineItem{
       private int quantity, peritem;
       ProductItem(String name, int quantity, int peritem){
         super(quantity+" "+name);
         this.quantity = quantity;
         this.peritem = peritem;
       public int getCost(){
         return this.quantity*this.peritem;
    class FlatFeeItem extends AbstractLineItem{
       private int fee;
       FlatFeeItem(String name, int fee){
         super(name);
         this.fee = fee;
       public int getCost(){
         return this.fee;
    public class Test{
      public static void main(String ... args){
        List<LineItem> items = new ArrayList<LineItem>();
        items.add(new ProductItem("Banana",6,1));
        items.add(new FlatFeeItem("Some boring thing or another",35));
        items.add(new ProductItem("Kiwi",3,3));
        for(LineItem item : items)
          out.println(item);
    }

  • Final in abstract class, again

    Hello,
    Consider an abstract class with a final variable:abstract class RingRing {
      private final int m;
    } upon compiling, you will find a compiler error.
    Couldn't this error be delayed and placed on the implementors of this class?
    Why was it designed this way?
    The solution to this problem is to set the variable via a constructor,
    but it's a bit of a 'hack' to pass everything in the constructor. specially
    if we would like to pass it, say, an array of some yet unknown size, or ...

    Hello,
    Consider an abstract class with a final
    variable:abstract class RingRing {
    private final int m;
    } upon compiling, you will find a compiler
    error.
    Couldn't this error be delayed and placed on the
    implementors of this class?Well, considering it's private, implementors don't
    have access to it.oops, i did not mean for it to be private, i meant protected or public.
    Does it compile if it's protected? At first blush,
    what seems logical to me is that if it's protected or
    public, then the compiler would complain about any
    implementor that doesn't initialize it in all its
    constructors or in an instance initializer. I've no
    clue whether it actually works that way though, and
    since I'm assigning homework anyway... :-)and yes, the problem still exists with protected :)

  • Abstract class/ method calls

    Hi, in an
    abstract class Report
    private int mat
    private string add
    private int mat
    private double creduced = 0.125;
    private double sreduced = 0.25;
    public Property(String location, int material)
    add = location;
    mat = material;
    public double getActualPrice()
    double base = getPrice();
    double money = base;
    if (mat == 1)
    money -= base * creduced;
    return money;
    etc and two abstract classes called getPrice() , and getInsurance()- which i have both coded in the extended class
    in a class called "class Info extends Report" I want to use the getActualPrice() method since it will anyway cause of inheritence but i want to add a new if line to it. How do I do this and code it in the extended class?
    Can i super call the instance variables and local variables ? I have tried super calls on the instance variables but since none of them are included in any of the constructors it wont work.

    public double getActualPrice()
    double actual=super.getActuelPrice();
    if ( something )
       actual=dosomethingwith(actual){
    return actual;
    }

  • Abstract classes

    Hi all,
    I have just read a section on abstract classes and have gone through an example using a shape class and square sub class. Ok, so I undertstand the concept that a findArea function i.e. its an abstract method as it is generic so should be defined under the superclass BUT for this demo it doesn't actually seem to be used, am I correct? this can be seen by simply removing the "public abstract double findArea();" and the code still works.........am finding it diffcult to apply this concept to reality
    public abstract class Shape {
        private String colour="red";
        Shape() {
            System.out.println("Shape's constructor called");
        public String getColour() {
            return colour;
        public void setColur(String c) {
            colour=c;
        public String toString(){
            return "Colour is:"+ colour;
        public abstract double findArea();
    public class Square extends Shape{
        private double length;
        /** Creates a new instance of Square */
        public Square(double l) {
            length=l;
            System.out.println("Square's constructor is called");
        public String toString(){
            String tmp=super.toString();
            //return the information using the super class method
            tmp=tmp+"\nShape is square";//add more information
            tmp=tmp+"\nArea is "+findArea();
            return tmp;
        public double findArea(){
            return length*length;
        public static void main(String [] args){
            Square sq = new Square(5);
            System.out.println(sq.toString());
    }

    Yes you are right when I remove this there is no overriding anymore ForumKid2 and to kayman when I replace sq.toString with sq.findArea in the print statement this behaviour is also true but when I wish to inherit the abstract method, it seems this is not allowed as I still get the same message as left on a previous post in that the Square is not abstract! So I guess this is the whole idea behind abstract inheritance.
    Also, a slightly different question, I noticed that both constructors are called for Shape and Square even though the object instantiated for Square has an argument of 5 BUT the constructor of Shape is a no-arg constructor? Surely they should match for it to print i.e. there should be a:
    public Shape (double l) rather than just a
    public Shape() ??

  • Abstract classes and recursive data

    I am trying to create my own mini "RPG" game and right now I'm starting off with the basics; potions for players. However, when I attempt to define my classes I get the following error when compiling compound.java
    *\compound.java:8: cannot find symbol*
    symbol  : constructor mixture()
    location: class mixture
    *^*
    *1 error*
    Basically a MIXTURE can be one of three things:
    Water
    A Potion
    Compound (two mixtures)
    Here are my two classes:
    abstract class mixture
         private water h2O = null;
         private potion potn = null;
         private compound cmpnd = null;
         mixture(water w)
              this.h2O = w;
         mixture(potion p)
              this.potn = p;
         mixture(mixture m1, mixture m2)
              this.cmpnd = new compound(m1, m2);
         public void getContents()
              if(this.h2O != null) { getWater(); }
              if(this.potn != null) { getPotion(); }
              if(this.cmpnd != null) { getCompound(); }     
         public water getWater() { return this.h2O; }
         public potion getPotion() { return this.potn; }
         public compound getCompound() { return this.cmpnd; }
    public class compound extends mixture
         // A compound consists of two mixtures
         private mixture mix1;
         private mixture mix2;
         compound(mixture m1, mixture m2)
              this.mix1 = m1;
              this.mix2 = m2;
         public mixture getMix1() { return this.mix1; }
         public mixture getMix2() { return this.mix2; }
    }Thank you for any help you can provide!
    Edited by: blacksaibot on Nov 16, 2008 10:03 AM

    The error message is telling you exactly what's wrong. As per the rules below, you're trying to call a constructor on you mixture class (should be Mixture, by the way--classes start with uppercase by convention) which doesn't exist.
    Also, I strongly recommend you get a firm grounding in the basics before attempting anything with even a fraction of the complexity of an RPG. You're just setting yourself up for frustration otherwise.
    Constructor rules:
    1) Every class has at least one ctor.
    1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().
    1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a public MyClass() {...} if you want one.
    1.3) Constructors are not inherited.
    2) The first statement in the body of any ctor is either a call to a superclass ctor super(...) or a call to another ctor of this class this(...) 2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor super() as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.
    2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.
    Edited by: jverd on Nov 16, 2008 10:25 AM

  • Extending abstract classes

    I just have a simple question. I have one class which is abstract
    public abstract class Person
         private String firstName;
         private String lastName;
         private String title;
         private String dateOfBirth;
         private String homeAddress;
         private String phoneNumber;
         public static final String MR = "Mr";
         public static final String MISS = "Miss";
         public static final String MS = "Ms";
         public static final String MRS = "Mrs";
         public static final String DR = "DR";
         public static final String PROF = "Prof";
         public Person(String firstName, String lastName, String title, String dateOfBirth,
                         String homeAddress, String phoneNumber){
              this.firstName=firstName;
              this.lastName=lastName;
              this.title=title;
              this.dateOfBirth=dateOfBirth;
              this.homeAddress=homeAddress;
              this.phoneNumber=phoneNumber;
         And i have another class which extends this class
    public abstract class Borrower extends Person{
      public LibraryItem [] itemsBorrowed;
      private double currentFine;
      private int barCode;
      public LibraryItem [] getItemsBorrowed()
           return itemsBorrowed;
      public double getCurrentFine()
           return currentFine;
      public int getBarCode()
           return barCode;
      }When i try to compile these two classes, i get the error
    Cannot find symbal constructor Person(). The problem is that the tutor hates us providing default constructors, and i presume that this is what the error is asking for. Is there any way around this or does a default constructor need to be povided?
    cheers

    codingMonkey wrote:
    georgemc wrote:
    nick2price wrote:
    Ok, i get you, better call the superclass constructor as tutor hates me using no param constructorsChallenge him on that. Not only are they perfectly acceptable, they're a mandatory part of the JavaBeans spec.Personally, even though there are nothing wrong with them, I prefer to avoid no-arg constructors in a lot of cases. Usually when I feel that a class should always have certain attributes. I.e. instead of having a no-arg constructor for something like a Person class, I'd rather have a constructor that takes at least a name. It is true that the name could always be initialized in the no-arg constructor, but I would think that it makes more sense for a person to always have a name, rather than being called "null" or "N/A".If you plan on using any framework that uses the Beans spec (and while the inexperienced, self-professed "purist" might balk at that, you'll find it virtually impossible to avoid as a commercial coder) you won't be able to with those classes. Your argument about always needing a sensible starting point falls down where you have multiple disparate types that you are re-constructing. While it's quite nice to say "a Person should always have a Name", if you have, for example, a persistence framework that will generically map the results of various database queries back onto Java objects, you have to write some special case code for each class in your domain model, that says "in order to contruct this object, you first need to perform this query, then invoke this constructor with this part of the result of that query, then populate the rest of the properties using setter methods". For every class in your domain model. That's a fair amount of overhead. The no-args constructor model completely circumvents that, since everything's populated by the same generic code - beans introspection. Think about it. For every class in your model, you have to have separate chunks of code that are aware of - and hence, coupled to - a specific query, and a specific constructor of a specific class. Far from ideal.
    Even if you decide to roll your own code to manage peristence, configuration, remoting and the like, you'll still eventually settle on no-args constructors as extremely handy tools. Note that I'm all for the presence of other constructors as well, that do allow sensible creation of objects with certain values. Just that the no-args constructor is so infinitely useful for writing a huge amount of generic code
    I would think that it makes more sense for a person to always have a name, rather than being called "null" or "N/A".Me too. And there's nothing in any of my points that gainsays, or opposes that. You create a Person instance, and he's called "null" for a few clock cycles before you populate him from a database query - nothing wrong with that

  • Calling a method from an abstract class in a seperate class

    I am trying to call the getID() method from the Chat class in the getIDs() method in the Outputter class. I would usually instantiate with a normal class but I know you cant instantiate the method when using abstract classes. I've been going over and over my theory and have just become more confused??
    Package Chatroom
    public abstract class Chat
       private String id;
       public String getID()
          return id;
       protected void setId(String s)
          id = s;
       public abstract void sendMessageToUser(String msg);
    Package Chatroom
    public class Outputter
    public String[] getIDs()
         // This is where I get confused. I know you can't instantiate the object like:
            Chat users=new Chat();
            users.getID();
    I have the two classes in the package and you need to that to be able to use a class' methods in another class.
    Please help me :(                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    I have just looked over my program and realised my class names are not the most discriptive, so I have renamed them to give you a clearer picture.
    package Chatroom
    public abstract class Chatter
    private String id;
    public String getID()
    return id;
    protected void setId(String s)
    id = s;
    I am trying to slowly build a chatroom on my own. The Chatter class is a class that will be used to represent a single logged in user and the user is given an ID when he logs in through the setId and getID() methods.
    package Chatroom;
    import java.util.Vector;
    public class Broadcaster
    private Vector<Chatter> chatters = new Vector<Chatter>();
    public String[] getIDs()
    // code here
    The Broadcaster class will keep a list of all logged-in users keeps a list of all the chats representing logged-in users, which it stores in a Vector.I am trying to use the getIDs() method to return an array of Strings comprising the IDs of all logged-in users, which is why im trying to use the getID() method from the Chat class.
    I apologise if I come across as clueless, it's just I have been going through books for about 4 hours now and I have just totally lossed all my bearings

Maybe you are looking for

  • Routing issue

    hi, can someone please tell me what i am doing wrong. here is my config: CRURT1#sho run Building configuration... Current configuration : 3142 bytes version 12.3 service timestamps debug datetime msec service timestamps log datetime msec service pass

  • Urgent : Error while creating PL/SQL web service in JDeveloper 11R2

    Hi, I am new to web services and I have a requirement to create web services in PL/SQL. I installed JDeveloper 11R2 in my machine and connected it to my local schema. My database is Oracle 10g. I am able to see my package in JDeveloper. But when I tr

  • GE620DX - new Nvidia driver breaks steam games

    Hi, Yesterday I upgraded the graphics driver on my GE620DX to the one just released (285.98) downloaded from here: http://www.msi.com/service/download/nbdriver-17287.html After doing this all my Steam games that are configured to use the NV graphics

  • Using the External Content Type as a column lookup

    Hi. I am working on a solution that will get data from a web service (third party) and create a list in SharePoint Online (Office365 E3 subscription). The use a column from that list as a lookup column for another list. The reason for this is to allo

  • Font conversion_PC-PDF opened in Mac Illustrator

    Working on a project that I've done before created on a PC based Pull-down menu driven system and saved in a PDF format. I download those PDFs for editing in Illustrator. This time the programmer used PC Adobe Folio 11 fonts and now when I open the P