Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: can Java create a user define type of classes?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default can Java create a user define type of classes?

    Hi,

    FYI I am very new to Java programming from a long time Delphi programmer...

    Does java allow a mechanism to create a user defined type for a "class type"?

    I am looking for a java equivalent of delphi "class of" declaration.

    Delphi example...
    type   // define types for the following block of codes...
        TMyClasses = class of TMyClass;
        TMyClass = class(TObject)
        private
            FMyOtherClass: TMyClasses
        public
            class function TableName: string; virtual; abstract;  // defines virtual method that must be overridden
            property MyOtherClass: TMyClasses  read  FMyOtherClass  write  FMyOtherClass;
            .... my class properties and methods...
            end;
     
        TMyClass2 = class(TMyClass)  // inheritance
        public
            class function TableName: string; override;  // ie. implemented as "Items" in source
            end ...
        TMyClass3 = class(TMyClass)
        public
            class function TableName: string; override;  // ie. implemented as "Employee" in source
            end 
        TMyClass21 = class(TMyClass2); ...
        public
            class function TableName: string; override;  // ie. implemented as "Junk" in source
            end 
        TMyClass31 = class(TMyClass3); ...
        public
            class function TableName: string; override;  // ie. implemented as "Foo" in source
            end 
     
        TBroClass = class(TObject)...
     
    var
        MyClass: TMyClass;
        OtherClass: TMyClass;
     
    ...
    MyClass := TMyClass.Create;  // instantiate an TMyClass object
     
    // and I can do the following
    MyClass.MyOtherClass := TMyClass2;   // assignment of just the class type
    ShowMessage(MyClass.MyOtherClass.TableName);  // this will display a dialog box with message "Items"
    OtherClass := MyClass.MyOtherClass.Create; // this line will instantiate TMyClass2
     
    MyClass.MyOtherClass := TMyClass31;
    ShowMessage(MyClass.MyOtherClass.TableName);  // this will display a dialog box with message "foo"
    OtherClass := MyClass.MyOtherClass.Create; // this line will instantiate TMyClass31
     
    MyClass.MyOtherClass := TBroClass;  // this will have a compiler error for type incompatability

    So, does java has something similar?

    Thank you,
    Last edited by helloworld922; September 30th, 2011 at 10:36 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: can Java create a user define type of classes?

    I think you're just talking about simple instantiation. I think that's implied in Java. Recommended reading: Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: can Java create a user define type of classes?

    Hi. Thank you for the reference link... but... I actually am not.

    I am looking for a mechanism to instantiate an inherited class via a class reference to the same class inheritance tree, from other instance of a [different] class.

    This way, I can create a framework where I can have a base sql [table] data manipulator UI that takes a Business Object class reference, instantiate BO, load it, map the values to UI, retrieves values from UI and map it to BO, then update it, etc.

    Perhaps, I am my life harder going this route... Or, Java framework already has that kind of mechanism and I just don't know of it.

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: can Java create a user define type of classes?

    Did you read inheritance???? If not then read it first and if you still got problems, show us your code and describe the part, your problem resides. Hopefully, someone will help you.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: can Java create a user define type of classes?

    You also might want to check out the Class API: Class (Java Platform SE 6)

    But I'm still not really sure what you're actually looking for. Maybe post what you think the Java code might look like, if it existed?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: can Java create a user define type of classes?

    Sounds like you're looking for is covariance in polymorphism. There are two main ways to achieve this, either through generics or via reflection.

    Without a more concrete example it's difficult to determine which you'll want, but it sounds like you're looking for generics.

    public class A<T extends Number> // some generic parameter T of class A. T is some sub-class of Number
    {
        T data; // class A holds some data of type T
    }

Similar Threads

  1. Create an Array wich represent Classes
    By Sjaakie91 in forum Collections and Generics
    Replies: 3
    Last Post: September 21st, 2011, 07:54 AM
  2. Replies: 5
    Last Post: August 11th, 2011, 12:39 PM
  3. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:37 AM
  4. How to create a pointer type of effect for objects
    By zelalem in forum Object Oriented Programming
    Replies: 6
    Last Post: October 20th, 2010, 02:53 AM
  5. ERROR, I CANT DEFINE IT
    By chronoz13 in forum AWT / Java Swing
    Replies: 7
    Last Post: December 2nd, 2009, 08:47 AM