Computer Science Canada Structures/Unions in Java? |
Author: | Kuntzy [ Thu Sep 23, 2004 10:01 am ] | ||
Post subject: | Structures/Unions in Java? | ||
Is there sumthing like a structure or union in Java ... compariable to
in turing? |
Author: | Martin [ Thu Sep 23, 2004 11:09 am ] |
Post subject: | |
It's called a class. |
Author: | TheZsterBunny [ Thu Sep 23, 2004 8:31 pm ] |
Post subject: | |
Martin, Can you please elaborate on that. Classes have not been extensively covered in ...class. Could you possibly give a basic explanation as to what a class really is? -Z |
Author: | wtd [ Thu Sep 23, 2004 8:47 pm ] | ||||||||||||
Post subject: | |||||||||||||
A class is more or less a blueprint for an object. It describes an advanced data structure which contains both data and operations on that data. Aside from the ability to include operations in the class definiton, the primary difference between the idea of structs and unions, and classes, is that the former is merely a data structure. It clumps several pieces of related data together and allows them to be moved around like a single piece of data. It leaves all operations on that data to the rest of the program, and trusts the programmer not to corrupt the data. A class typically encapsulates its data and makes it inaccessible to the rest of the program. All operations on that data are then carried out by an interface which the class also defines. Consider the simple example: (In both C and Pascal style programming languages suited to the different styles of programming, as well as O'Caml because it's fun)
|
Author: | Andy [ Thu Sep 23, 2004 9:16 pm ] |
Post subject: | |
basicly what it does is it describes the general properties of an object... like wtd says, its the blue print... since everything in oop r objects, a class simply stores info about the object and what it could do |
Author: | wtd [ Thu Sep 23, 2004 10:10 pm ] | ||||||
Post subject: | |||||||
Both the struct/union approach and classes can make use of composition, where part of one complex data structure is another data structure. However, structs and unions can't (in most languages at least) make use of inheritance, where one data structure is another data structure, with additional features. Let's say I want to add a PairOfInt class that allows the two integers to be switched.
|