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...
Code Delphi:
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,
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)
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. :(
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.
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?
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.
Code Java:
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
}