?
Solved

User Defined Data Types

Posted on 2003-03-06
8
Medium Priority
?
162 Views
Last Modified: 2010-03-31
Is there a way to created a user defined data type in Java WITHOUT inheriting it from a superclass? Something like a data struct in C OR a user defined type in VB?
0
Comment
Question by:freejaso
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8083577
No. Every object is a subclass of Object
0
 

Expert Comment

by:Lavendermpl
ID: 8086035
You may use the class to define your data structure.
in C:
typedef struct test {
   int a;
   char d;
};

in Java:
class test {
   int a;
   char d;
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8087322
What CEHJ has quoted is correct. Every class you create will automatically be a subclass of Object.

Mayank.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:doddjames
ID: 8087568

Interestingly, I wonder what the relationship between an interface and Object is...  You can't tell what the implementation class will extend until it's instantiated..

public interface Bar {
    Object foo = null;
    public Object bar = foo;
    public static final String FOOBAR = "wizz";
}

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8087602
>>WITHOUT inheriting it from a superclass?

Why would you want to do that btw?

>>You can't tell what the implementation class will extend until it's instantiated..

You can certainly tell that it'll be a subclass of Object ;-)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8087697
Dear doddjames,

The interface will not have anything to do with Object but when the interface is implemented by a class, that class will automatically be a subclass of Object. And without an implementing class, your interface is useless. Hope you got that.

Mayank.
0
 

Expert Comment

by:doddjames
ID: 8087955

Hi mayankeagle,

Your (vociferous) assertion is false.

I am perfectly capable of using an interface without implementing it with a class.
 
public interface Constants {
   public static final String MY_CONSTANT = "blah";
}

Constants.MY_CONSTANT is perfectly usable.  I don't agree with this style myself, but it is legal and certainly conflicts with your idea of an interface without an implementing object being 'useless'.

I too hope you "get" it. :-)
0
 

Author Comment

by:freejaso
ID: 8106288
Thanks for your comments.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question