Instantiate Inheritance?

Hey I'm writing a shape inheritance program where I'm supposed to create a main class, a shape class, a 4 different dimension classes, and 4 different shape classes. I wrote a similar program in C# and so tried transferring over some of the code but the instantiate process doesn't seem to be working. Specifically every line of code below has the same error except the last one.
The first 5 lines
"Cannot find symbol
Symbol: Class Shape (Exchanging shape with, Point, Line, Circle, etc depending on the line)
Location: Class Shapes.Main"
The last line gives the error "Shapes.Shape is abstract; cannot be instantiated"
Shape s = new Shape();
I assume this is the real cause to the problem, but I'm unaware of what to do..
        Shape[] s = new Shape[4];
        s[0] = new Point(1, 2, 3);
        s[1] = new Line(new Point(2, 3, 4), new Point(4, 5, 6));
        s[2] = new Circle(new Point(5, 2, 8), 20);
        s[3] = new Sphere(new Point(4, 2, 3), 45);
Shape s = new Shape();I provided a 4 classes, the main, the shapes class, 1 of the dimensions, and 1 of the shapes.
package Shapes;
@author student
*public class Main {*
*@param args the command line arguments*
    public static void main(String[] args) {
        Shape[] s = new Shape[4];
        s[0] = new Point(1, 2, 3);
        s[1] = new Line(new Point(2, 3, 4), new Point(4, 5, 6));
        s[2] = new Circle(new Point(5, 2, 8), 20);
        s[3] = new Sphere(new Point(4, 2, 3), 45);
        Shape s = new Shape();
        //For Loop with if statements in order
        for (int i = 0; i < s.Length; i++) //to check identifier type
            if (s[i] instanceof Point) {
                Point P = (Point) s;
if (s[i] instanceof Line) {
Line L = (Line) s[i];
L.GetLength();
if (s[i] instanceof Circle) {
Circle C = (Circle) s[i];
if (s[i] instanceof Sphere) {
Sphere B = (Sphere) s[i];
} //Calling Print and Move statements
s[i].Print();
s[i].Move(2, 2, 2);
s[i].Print();
System.out.println("\n");
package shapes;
@author Student
*abstract class Shape {*
* protected int ShapeID;*
* static private int X = 1000;*
* //Shape Constructor*
* public Shape() {*
* ShapeID = X;*
* X++;*
* //Get Method for ID*
* public int GetID() {*
* return ShapeID;*
* //toString
+    public abstract void Print();+
+    abstract public void Move(int a, int b, int c);+
*+}+* package shapes;
abstract class ZeroDShape extends Shape {
abstract public void Print();
abstract public void Move(int a, int b, int c);
}package shapes;
* @author Student
class Sphere extends ThreeDShape {
private double Volume;
private Point Loc;
//Constructor
public Sphere(Point A, double R) {
Loc = A;
Volume = 4 / 3 * Math.PI * (R * R * R);
//Get Volume Method
public double GetVolume() {
return Volume;
//Print Method
public void Print() {
System.out.println("Sphere:");
System.out.println(super.toString());
System.out.println("Volume = " + Volume);
System.out.println("Location Sphere: " + Loc + "\n");
//Move Method
public void Move(int a, int b, int c) {
Loc.Move(a, b, c);
}Any advice or feedback would be much appreciated, thank you in advance.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Wolverine72 wrote:
The last line gives the error "Shapes.Shape is abstract; cannot be instantiated"Absolutely correct.
Shape s = new Shape();Absolutely wrong. You never use new with an abstract class. They are written specifically for extension, and you use it with the subclass.
I assume this is the real cause to the problem, but I'm unaware of what to do..Well I guess the question is: why are you doing it? You've already set up your shapes.
Plus, the following looks very suspicious:
static private int X = 1000;
//Get Method for ID
public int GetID() {
   return ShapeID;
}The whole point of polymorphism is to avoid exactly this kind of dispatch-type logic.
Don't "get a method for the ID"; override it correctly in your subclass. Java will make sure that the correct method is called.
HIH
Winston

Similar Messages

  • Entity framework code first inheritance table per type problem

    hello
    i am using ODP.NET version 11.2.0.3.20. i am trying to get entity framework code first to work. one problem i have encountered is with inheritance. i have a "table per type" inheritance scenario. i have 3 tables involved. the base table is called "S_PERIOD" which maps to the base "Period" class. i have a derived table called "S_SEASON_QUARTER" that maps to the derived "Quarter" class. And I have another derived table called "S_SEASON_PRICE_PERIOD" that maps to the derived "PriceBreak" class. for testing purposes i am trying to load all records. the problem is that every class instance ends up being of type "Quarter", which is incorrect. for whatever reason the provider thinks that every "S_PERIOD" record has a corresponding "S_SEASON_QUARTER" record. i took a look at the generated SQL and posted it down below. looking at this SQL it's clear to me why every object ends up being of type "Quarter". It looks like the provider is checking the "C2" field to determine the concrete type to instantiate. the problem however is that the "C2" field value provided by each derived table is ALWAYS equal to "1", which is a problem since this will result in the "case" statement always hitting the second "when" condition. does anyone have any idea how to fix this? thank you
    SELECT
    CASE WHEN (( NOT (("Project1"."C2" = 1) AND ("Project1"."C2" IS NOT NULL))) AND ( NOT (("Project2"."C2" = 1) AND ("Project2"."C2" IS NOT NULL)))) THEN '0X' WHEN (("Project1"."C2" = 1) AND ("Project1"."C2" IS NOT NULL)) THEN '0X0X' ELSE '0X1X' END AS "C1",
    CAST( "Extent1"."PERIOD_ID" AS number(10,0)) AS "C2",
    "Extent1"."START_DATE" AS "START_DATE",
    "Extent1"."END_DATE" AS "END_DATE",
    "Extent1"."NAME" AS "NAME",
    "Extent1"."TYPE_CODE" AS "TYPE_CODE",
    CAST( "Extent1"."CREATE_USER_ID" AS number(10,0)) AS "C3",
    "Extent1"."CREATE_DATE" AS "CREATE_DATE",
    CAST( "Extent1"."MODIFY_USER_ID" AS number(10,0)) AS "C4",
    "Extent1"."MODIFY_DATE" AS "MODIFY_DATE",
    CASE WHEN (( NOT (("Project1"."C2" = 1) AND ("Project1"."C2" IS NOT NULL))) AND ( NOT (("Project2"."C2" = 1) AND ("Project2"."C2" IS NOT NULL)))) THEN NULL WHEN (("Project1"."C2" = 1) AND ("Project1"."C2" IS NOT NULL)) THEN "Project1"."QUARTER_NAME" END AS "C5"
    FROM "DBO_SPACE_DEV"."S_PERIOD" "Extent1"
    LEFT OUTER JOIN (SELECT
         "Extent2"."QUARTER_NAME" AS "QUARTER_NAME",
         CAST( "Extent2"."QUARTER_ID" AS number(10,0)) AS "C1",
         1 AS "C2"
         FROM "DBO_SPACE_DEV"."S_SEASON_QUARTER" "Extent2" ) "Project1" ON ( CAST( "Extent1"."PERIOD_ID" AS number(10,0))) = "Project1"."C1"
    LEFT OUTER JOIN (SELECT
         CAST( "Extent3"."PRICE_PERIOD_ID" AS number(10,0)) AS "C1",
         1 AS "C2"
         FROM "DBO_SPACE_DEV"."S_SEASON_PRICE_PERIOD" "Extent3" ) "Project2" ON ( CAST( "Extent1"."PERIOD_ID" AS number(10,0))) = "Project2"."C1"
    Edited by: 997830 on Apr 3, 2013 8:40 AM

    An update:
    I tried again following this example to the letter:
    Using NuGet to Install and Configure Oracle Data Provider for .NET
    This time I used a console application as described in the example. Yes, I rebuilt the project after the NuGet install.
    I made the appropriate mods to App.config. I get the same error message as with the MVC example above.
    Does the ODP.Net driver really work with EF? If so, can anyone provide me with a working sample project?

  • Inheritance, class indicator field and subclasses

    Hi
    Ok, I have an inheritance model with a base class (let's call it BaseClass), which is abstract. Every subclasse of BaseClass have a different implementation, but no additional attribute. I don't want to map all subclasses in Mapping Workbench, so I mapped BaseClass and configured inheritance to use a field as a class indicator (using the java class name).
    Now, to read all subclasses in one single query, I do this:
    Collection result = session.readAllObjects(BaseClass.class);
    Now the problem is that TopLink generates the following SQL:
    SELECT JAVA_CLASS_NAME, [...] FROM BASE_CLASS_TABLE WHERE JAVA_CLASS_NAME = 'com.[...].BaseClass'
    Well, obviously this is not what I expected, since this will not return anything! I tried to use the "Read Subclasses on Query' option, to no avail. Am I missing something here? I thought this would pretty staight forward... :-(
    TIA
    Regards
    Eric Morin

    You must always map all subclasses that you wish to persist/read. If TopLink does not have a descriptor for a class it will persist/instantiate it as its superclass descriptor class.
    If you explictly configured your descriptor to read subclasses on queries in code (descriptor.getInheritancePolicy().readSubclassesOnQueries()) it would generate the correct SQL that you want, however it would return you all instances of the root class, not the subclasses. The only way to read subclasses is to map them.
    If you don't want to have to map all of the classes in the Mapping Workbench, you could just add the descriptor's for the subclasses in code.
    i.e.
    List allSublcasses = getAllMySubclasses();
    for (Iterator iterator = allSublcasses.iterator(); iterator.hasNext(); ) {
    Descriptor descriptor = new Descriptor();
    Class subclass = (Class) iterator.next();
    descriptor.setJavaClass(subclass);
    descriptor.getInheritancePolicy().setParentClass(subclass.getSuperClass());

  • JDeveloper UI editor problems with inheritance (?!)

    Hi,
    I've created a superclass which extends JPanel and adds a setPanelBinding method, a getPanelBinding method and a member variable named panelBinding. This class looks similar to the dataPanel class BC4J provides but the set and get methods are protected as is the panelBinding member variable.
    When I subclass this class and reference the panelBinding variable in the jbInit method of this class everything compiles fine. But when I open this class in the UI editor I get an error message in the console telling me the panelBinding variable can't be references in the jbInit method. Why can't JDeveloper reference this variable? When I try to use the subclass on for example a Form I get to see a red placeholder in the UI editor (probably because of the same error). My class compiles fine so it has to be some kind of limitation of the UI editor, or am I doing something wrong here?
    Regards,
    Peter

    Currently the UI Editor cannot reference inherited members, regardless of scope. An enhancement request has already been logged for this: #2222848.
    However, if your subclass has successfully compiled, you should be able to instantiate an instance of it in a second file (i.e. inside a JFrame). If you are seeing a red place holder object it is because the UI Editor was unable to instantiate an instance using reflection. The most common cases for this are: 1) Class does not have a public default constructor 2) UI Editor is unable to evaluate the arguments being passed into a non default constructor 3) Class is abstract and thus uninstantiable

  • Interface inheritance and Generics

    Hello!
    For an application I need several 'stores' (classes which contain some elements) which share certain characteristics. I would like to create a general StoreInterface, and some specialized interfaces which inherit from this, e.g. a 'KwicStoreInterface'.
    Also I would like to specify that the Store takes elements of a general type 'Storable', but the KwicStore only takes objects of the type 'KwicInterface' which inherits from 'Storable'.
    I thought that specifying these restrictions using Generics would be a good idea, but I can't get the hang of it.
    This is a simplyfied version of what I tried:
    // interfaces for the stores
    public interface StoreInterface<T extends Storable>{
        public T getElem(Long id);
        public void setElem(T elem);
        public void setElems(Map<Long, T> elems);
    public interface KwicStoreInterface<T extends KwicInterface> extends StoreInterface{
        public void SomeKwicStoreSpecificMethod();
    // Interfaces for the elements
    public interface Storable {
        public Long getID();
        public void setID(Long id);
    public interface KwicInterface extends Storable{
        public void SomeKwicSpecificMethod();
    }When I try to instantiate KwicStoreInterface, this code doesn't compile, because the methods are not recognized as valid implementations of the interface's methods.
    public KwicStore<T extends KwicInterface> implements KwicStoreInterface {
         private Map<Long, T> elements = new HashMap<Long, T>();
       // Only his method seems to be recognized correctly:
        public T getElem(Long id){
              return elements.get(id);
       // not recognized as interface method
        public void setElem(T elem){
               elements.put(elem.getID(), elem);
        // not recognized as interface method
        public void setElems(Map<Long, T> elems){
                elements = elems;
       // this one seems okay, too, of course:
       public void SomeKwicStoreSpecificMethod() {}
    }If I allow the Netbeans IDE to automatically instantiate the methods, all the type information is reverted to Storable or is lost completely (in case of the Map), resulting in methods like:
    @Override
        public void setElem(Storable elem) {
            throw new UnsupportedOperationException("Not supported yet.");
        @Override
        public void setElems(Map elems) {
            throw new UnsupportedOperationException("Not supported yet.");
        @Override
        public Storable getElem(Long id) {
            throw new UnsupportedOperationException("Not supported yet.");
        }What am I doing wrong and how can I achieve the functionality I want?
    Thanks in advance for any help!

    Thanks, that was really helpful!
    However, I ran into new problem now.
    The KwicStore should be built using a Reader object (which parses an XML file) and the reader contains a Map with concrete types.
    However, it seems impossible to initialize a 'variable parameter' with a concrete type.
    public class KwicStore<T extends KwicInterface> implements KwicStoreInterface<T>{
        Map<Long, T> elements;
        // doesn't work:
        KwicStore() {
            KwicStoreReader reader = new KwicStoreReader();
            reader.read();
            this.elements = reader.readerElems;
       // other methods
    // the very simplified reader
    public class KwicStoreReader {
         public Map<Long, KwicInterface> readerElems = new HashMap<Long, KwicInterface>();
        // do something to fill readerElems with Kwic objects (Kwic instantiates KwicInterface)
        public void read() {
             KwicInterface kwic = new Kwic();
             readerElems.put(kwic.getID(), kwic);
    }Is there any way around this?
    I already tried to use Generics and variables within the reader, too, but then I couldn't put Kwic objects into the readerElems map.
    public class KwicStoreReader<T extends KwicInterface> {
         public Map<Long, T> readerElems = new HashMap<Long, T>();
        // do something to fill elems with Kwic objects (Kwic instantiates KwicInterface)
        public void read() {
             T kwic = new Kwic(); // this doesn't work
             readerElems.put(kwic.getID(), kwic);
    }Actually, I would also be fine with restricting KwicStoreInterface to take KwicInterface objects only (without the extends), but I don't know how to declare that correctly.

  • Constructor Inheritance Question

    Here's a quote from the Java Tutorials at http://java.sun.com/docs/books/tutorial/java/javaOO/objectcreation.html :
    "All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler will reject the program."
    In order to fully understand this concept, I created two classes: a ClassParent and a ClassChild.
    public class ClassParent
        public static void main(String[] args) {
           ClassParent tester = new ClassParent();
    public class ClassChild extends ClassParent
        public static void main(String[] args) {
            ClassChild child = new ClassChild();
    }Both classes compiled successfully, which raised the following question:
    I understand that the ClassParent default constructor calls the Object's no-argument constructor.
    Does the ClassChild also call the Object's constructor once it realizes that the ClassParent does not have a no-argument constructor?
    And a somewhat non-related question:
    Seeing how ClassParent calls Object's no-argument constructor if it does not have one of its own, does it also extend the Object class?
    Edit: After running the following code, I realized that the answer to my last question is yes:
    public class ClassParent
        public static void main(String[] args) {
           ClassParent tester = new ClassParent();
           boolean test = tester instanceof Object;
           System.out.println(test);
    }Edited by: youmefriend722 on May 26, 2008 1:54 PM

    youmefriend722 wrote:
    I think I'm getting a basic grasp now but want to make sure that I'm not misunderstanding anything.
    Constructor inheritance:
    If a no-argument constructor is invoked but one isn't declared in that class, the superclass's no-argument constructor will be invoked. Well, sort of. If you invoke a constructor that doesn't exist, you get an error. Keep in mind that the invocation and the constructor may both be automatically supplied by the compiler, and that the compiler won't automatically create a no-arg constructor for a class if you define any constructors for that class.
    So if you don't define any constructors in a class, then a no-arg one is created automatically (at compile time) and (at runtime) when you instantiate that class, that no-arg constructor will try to invoke the superclass's no-arg constructor.
    But suppose you do define a constructor, one that takes an argument. Then if you try to invoke a no-arg constructor on that class, you'll get an error, and the superclass's no-arg constructor won't be invoked (because the error happens first).
    If the superclass does not have a constructor, then the superclass's superclass's constructor will be invoked.No. That never happens. Every class has a constructor (although it might have permissions which make it inaccessible, which is a whole other issue we'll worry about later).
    If the superclass does have a constructor but doesn't have a no-argument constructor, then there will be a compile-time error.Only if you try to invoke the no-arg constructor, which might happen implicitly. For example, if you write a superclass with a 2-arg constructor, and you write a subclass with a 1-arg constructor, and the 1-arg subclass's constructor invokes the superclass's 2-arg constructor, then there's no problem, even though in this example, the superclass doesn't have a no-arg constructor.
    Constructors in general:
    In every constructor, the superclass's no-argument constructor will be invoked if the none of the superclass's constructors are explicitly invoked.Yeah, I think that's right.

  • Appropriate use of inheritance

    We have many Database Access Classes (D1, D2, D3...) for each table in our application. All of these classes need to access some common functions such as getConnection and getResultSet.
    I suggested keeping these functions in a separate Utility Class. The concern in the team is if we do so we will instantiate several objects of the Utility class as all D1, D2, D3 etc. will access these.
    Another team member suggested that we should have a baseclass DBase for all the Database Access classes i.e. have D1, D2,D3 extend DBase and that will allow us to access the common functions and we will creating several instances as in the case of the Utility class.
    I find it odd that we should use inheritance in this manner where there is really no "is-a" relationship whatsoever betwen the base class and the derived classes!
    Is my concern valid and Can someone come up with any other alternatives?
    Thanks

    We have many Database Access Classes (D1, D2, D3...) for each table in our application. All of these classes need to access some common functions such as getConnection and getResultSet.
    I suggested keeping these functions in a separate Utility Class. The concern in the team is if we do so we will instantiate several objects of the Utility class as all D1, D2, D3 etc. will access these.
    Another team member suggested that we should have a baseclass DBase for all the Database Access classes i.e. have D1, D2,D3 extend DBase and that will allow us to access the common functions and we will avoid creating several instances as in the case of the Utility class.
    I find it odd that we should use inheritance in this manner where there is really no "is-a" relationship whatsoever betwen the base class and the derived classes!
    Is my concern valid and Can someone come up with any other alternatives?

  • Inheritance Issue - constructor

    Okay, I hope you guys can help me out of this. I currently have an interface designed, with a class inheriting its information. Here is the sample code for reference:
    ArrayStack
    public class ArrayStack
        implements AnotherInterface
        public ArrayStack(int stacksize)
            stack = new String[stacksize];
        public boolean isFull()
            return top == stack.length;
    }Now the problem arises when I want to create a new class which inherits the ArrayStack. Here is a sample code of what I have wrote up:
    public class checkSize extends ArrayStack
        public checkSize()
    }I'm just trying to get my head around inheritance, but this code keeps giving me the error "cannot find symbol - constructor". Can anyone point me to the right direction or tell me what I can do? Any help would be greatly appreciated!

    Connex007 wrote:
    Melanie_Green wrote:
    So as suggested you can either call another constructor or you can create a default constructor in your superclass,Now that's where I'm getting confused on - what other constructor can I call?There is only one other constructor to call in the super class.
    Remember that the first line of every constructor is either an explicit or implicit call to another constructor. If you do not explicitly call another constructor then super() is inserted during compile time, and the default constructor that has no parameters of the superclass is called.
    Furthermore any class that does not explicitly extend another class, extends Object. So as I stated previously if the first line of the any constructor in this class is not a call to another constructor, then the compiler will insert super() which will invoke the default constructor of Object.
    Now in your scenario you are using inheritance, you have a subclass extending a superclass. Note that you have not defined a default constructor in the super class, which for example is defined in Object. So inside your subclass, if you do not call another constructor on the first line, then again the compiler will insert a call to super(). The call to super() from the subclass is dependent on the superclass having defined a default no parameter constructor. However in your case the superclass does not have such a constructor defined. Therefore you are still required to instantiate every class in the inheritance tree, either explicitly or implicitly.
    If you are still weary at this point, think of it this way. If A extends B extends C. When an object of type A is instantiated, a constructor of A then B then C is called so that C is instantiated first, then B, then A. This is reflective of instantiated in a top down method so that each subclass is instantiated correctly. Since A extends B, and B can potentially be instantiated in one or more ways, you still need to define how both A and B are instantiated.
    So relating this back to your problem, what are all the possible ways in which your superclass can be instantiated? We can eliminate the default constructor and assume that we must specify on the first line of the constructor contained in the subclass that a call to a constructor in the subclass or superclass occurs. If we invoke a constructor in the same class, in the subclass, then either this constructor calls a constructor in the super class, or it calls a constructor that in turn will tinkle its way up to calling a constructor in the superclass.
    Mel

  • Abstract classes and inheritance

    I'm trying to do some simple inheritance but I'm a little stuck. Without giving too much away here's what I have (or am trying to have). I have an abstract "Test" class and a number of concrete classes that extend that abstract class. In my main method I have an array of Test objects. Each object in the array is a certain kind of "Test" and each one has its own way of doing most things but it doesn't make sense to have just a plain Test object because it is an abstraction. In my code I go through the array and execute a method, runTest(), for each Test in the array. However, my abstract Test class doesn't have a runTest() method b/c it will never get used and java complains. At runtime shouldn't java figure out, OK this is TestA, so go to TestA's runTest method (polymorphism?). What should I put in my runTest method in the abstract class? I know I can put anything there but what is standard?

    JFactor2004 wrote:
    ... Each object in the array is a certain kind of "Test" and each one has its own way of doing most things but it doesn't make sense to have just a plain Test object because it is an abstraction. In my code I go through the array and execute a method, runTest(), for each Test in the array. However, my abstract Test class doesn't have a runTest() method b/c it will never get used and java complains. If you want to instantiate the Test class
    you need to provide an implementation for the runTest() method,
    something like/*type*/ runTest() { throw new UnsupportedOperationException(); }

  • Python inheritance issue

    I'm trying to expand the functionality of the ConfigParser class and it is not working as I would expect.  I wrote my own class, inherited ConfigParser.ConfigParser, and expected to have all of the ConfigParser methods available to routines that instantiate objects of my new class.    It is not working this way.
    I've done this with other classes with no issues.  Is there something about ConfigParser that could be causing an issue?
    I'd post code but there are a lot of lines that are somewhat relevant.  I'm looking for some insight at this point.
    Thanks!
    Last edited by TomB17 (2009-09-08 20:21:12)

    hard to say without some code/example
    (edit: maybe use a nopaste if you want to paste a sizable chunk of code)
    Last edited by cactus (2009-09-08 22:03:28)

  • Trouble with  instantiating inherited classes in a switch statement

    Hi
    I have few inherited classes which I need to instantiate in a switch statement based on command line argument supplied by user. I am ble to do that but I cannot access any methods once I come out of the switch scope. Please suggest a way to resolve this problem. The error I am getting is in the end of code in comments // //. Would really appreciate your help.
    Thanks
    Here is the code.
    package assignment2;
    import java.util.*;
    import java.io.*;
    abstract class Parent
    abstract void childclassDescription();
    void generalDescriptionToAllChilds()
    System.out.println("general to all child classes");
    childclassDescription();
    class Child1 extends Parent
    public void childclassDescription()
    System.out.println("i'm child1");
    class Child2 extends Parent
    public void childclassDescription()
    System.out.println("i'm child2");
    class Child3 extends Parent
    public void childclassDescription()
    System.out.println("i'm child3");
    public class Demo
    public static void main(String[] args)
    int option;
    Parent p;
    System.out.println("Pick from one of the following:");
    option=1; // supplying
    switch(option)
    case 1:
    p=new Child1();
    break;
    case 2:
    p=new Child2();
    break;
    case 3:
    p=new Child3();
    default:
    break;
    p.generalDescriptionToAllChilds();
    //error variable p might not have been initialized at line //
    }

    Well I also think that in Java, it is possible for a reference type variable to refer to nothing at all. A reference that refers to nothing at all is called a null reference . By default, an uninitialized reference is null.
    We can explicitly assign the null reference to a variable like this:
    f = null;
    But still i am not quety sure why you needed to do it.

  • Overloading constructor of a child that inherits from a class that inherits from Windows.Forms

    The title might be a bit confusing so here is the layout.
    - classHForm inherits System.Windows.Forms.Form
    - frmDoStuff inherits classHForm  (written by someone else and used in several applications)
      - does not have a constructor specifically defined, so I added the following thinking it would override the parent
    Sub Public New()
    End Sub
    Sub Public New(byval data as string)
    'do stuff
    End Sub
    In my code, I want to instantiate frmDoStuff, but I need to overload it's constructor and populate fields within this form. The problem I'm running into is that when I run this, it is using the constructor from classHForm and ignores any constructor in frmDoStuff.
    I guess you can't override the parent constructor? Is that why it won't work?
    I didn't want to overload the classHForm constructor, but I will if that's the way to go.
    my code:
    dim frm as new frmDoStuff(myString)
    Note: I would love to show actual code, but not allowed. Against company rules.

    Your code is similar. The value I pass into frmDoStuff is supposed to set a value for a textfield.
    Public Class frmDoStuff
    Inherits classHForm
    Public Sub New(ByVal s As String, ByVal gridData() as String)
    txtMyTextField.text = s LoadGrid(gridData)
    End Sub
    I also have a datagridview I need to populate in frmDoStuff and thought I would have another string or string array as a second parameter to the constructor and then call a routine from the constructor to populate the datagrid.
    of course, when I run it this way, the code is not being reached. I'll build an example as COR suggested and maybe someone will see where I'm going wrong.
    [UPDATE]
    I created a simple example and it worked. So, now I need to try to understand what is different in the actual code.
    Here is my example:
    Parent Class inherits form
    Imports System.Windows.Forms
    Public Class classMyForm
    Inherits System.Windows.Forms.Form
    Sub New()
    ' This call is required by the designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
    End Sub
    End Class
    Public Class frmDoStuff
    Inherits classMyForm
    Public Sub New()
    ' This call is required by the designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
    End Sub
    Public Sub New(ByVal sStuff As String)
    MyBase.New()
    InitializeComponent()
    'Populate the textbox
    TextBox1.Text = sStuff
    End Sub
    End Class
    Public Class frmMyForm
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim frm As New frmDoStuff(TextBox1.Text)
    frm.Show()
    End Sub
    End Class
    Just to recap. The actual parent was created a few years ago and the child form that I'm calling, "frmDoStuff" was created a couple years back and is used in several applications. I need to use it in my application, but I need to populate a couple
    controls when the form is loaded. As you can see in this example, I successfully overloaded the constructor to populate the textbox in frmDoStuff.
    In my real life situation, my overloaded constructor seems to be getting ignored when i step through the code. I'll go back and do some more testing.

  • Inheriting windows from a shared library

    Hi,
    Does anyone know whether in rel 2D of Forte' there is a run time bug when
    you instantiate a window class which inherits from a window class that has
    been put into a shared library ?
    When we try to do this we always get the error:
    USER ERROR: Attempt to build a run-time class failed. The project
    'MySharedLibrary' does not have a class with id 0xbd.
    Class: qqsp_UsageException with ReasonCode: SP_ER_PARAMETERERROR
    Detected at: qqc4_Scope::CompileClass at 1
    Thanks,
    Paolo
    Paolo Sidoli
    DS Data Systems
    Parma, Italy
    Tel. ++39 521 2781
    Fax. ++39 521 272818
    [email protected]

    Alfredo Jahn wrote:
    I thought shared libraries were only read-only. She insists she is doing it. I could not see it, so I could not say for sure. I tried it, but using a shared library on another mac on my home network, and I could not drag any song from the shared library to my Music folder (the top item in the left panel of iTunes).
    Anyone have any thought on this?
    There are two kinds of sharing in iTunes.
    The one that has been there forever is "regular sharing," which allows you to look at and listen to the libraries of other iTunes users on the same network. This is the "read only" one you describe.
    iTunes Home Sharing is new in the last 1 or 2 releases. Home Sharing allows you to copy files across, and is possibly what your friend is using. Unlike regular sharing, Home Sharing only works on computers that are authorized to the same account. More information is here: iTunes: Understanding Home Sharing.

  • Inheritance usage question

    I have 2 classes, DBContainerGraphic and DBGraphic. The DBGraphic class has a static method called Create() which creates and returns a DBGraphic object. The DBContainerGraphic extends DBGraphic. DBContainerGraphic also uses a static factory method.
    Now, DBContainerGraphic is inheriting the static method from DBGraphic that instantiates and returns a DBGraphic. [note: it does have a different signature] This create method does not make sense in the context of DBContainerGraphic.
    It will work, but it is not returning a DBContainerGraphic, and thus I feel it should not be exposed on the DBContainerGraphic class.
    What should I do about this? Just ignore it? After all, it is only package scope.

    class DBGraphic
      static DBGraphic makeGraphic() { return new DBGraphic(); }
    class DBContainerGraphic
    extends DBGraphic
      static DBContainerGraphic makeContainerGraphic() { return new DBContainerGraphic(); }
      static DBGraphic makeGraphic() { return new DBContainerGraphic(); }
    }That is almost certainly NOT what you should do though. Instead of refactoring the whole set of classes, in effect introducing some zillion possible bugs, just use the existing classes and then change them slowly over time.
    The advantage of using the existing classes, is that you are finished the project as soon as you start, like right away.
    Andrew

  • Inheritance vs abstraction

    Considering the java architecture, what would be a better choice? Inheritance or abstraction? Under a condition where you might have an option.

    I believe abstraction is also inheritance...
    When you use an abstract class, you cannot instantiate the class. If the methods defined in the class as abstract method, all subclass must implement the abstract methods. But you don't need to make all the methods abstract. You can have regular method, and all subsclass will get that functionality automatically and won't need to implement that method again.
    So, yes, it all depends on your design and the functionality that you wants to provide.

Maybe you are looking for