Presentation is loading. Please wait.

Presentation is loading. Please wait.

Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.

Similar presentations


Presentation on theme: "Reference Types CSE301 University of Sunderland Harry R Erwin, PhD."— Presentation transcript:

1 Reference Types CSE301 University of Sunderland Harry R Erwin, PhD

2 Reference Types Summary A ‘reference type’ in Java is an object allocated in main memory (on the heap or free store in other languages). Reference types include objects, arrays, and interfaces. They all inherit from the class Object. (Test Question!) You don’t need to manage memory. That’s handled automagically. Pointers are hidden.

3 ‘Members’ Defined The ‘members’ of a reference type are fields and methods. Members of a reference type can be declared or inherited. Arrays inherit from Object and also have a (constant) length field. Interfaces also inherit from Object.

4 Classes and Objects A class is a named (or anonymous) collection of fields holding data values and methods. May contain nested inner classes (see Flanagan) Defines a data type ClassName extends the class Object by default. public class ClassName should be stored in a file named ClassName.java Compiles to ClassName.class

5 The Object Class All reference types inherit from Object. Member methods include: –public boolean equals(Object obj); –public final Class getClass(); // cannot override –public int hashCode(); –public String toString(); // should override! –protected Object clone() throws CloneNotSupportedException; // for deep copying –protected void finalize() throws Throwable; Test Question!

6 Creating an Object ClassName obj; –Declares that the name obj refers to an obj of class ClassName in the current scope. –Does not create that object. Until the object is created, obj refers to null (e.g., nothing). –To create the object, execute the following: obj = new ClassName(argument list); –Note that the constructor for ClassName may be overloaded. This may result in ‘constructor chaining’. –‘argument list’ is a list of arguments. Test Question!

7 Other Ways to Create Objects Using literals –String str = “…”; By dynamically loading a class –Class cls = typename.class; typename is the name of a datatype. By copying (cloning) an object Note that the definition of a class happens to be an object in the class Class. Guess where static fields and methods live… 8)

8 Using an Object or Class To refer to the member fields and methods of an object, use the following syntax: –obj.field; –obj.method(args); To refer to class level (‘static’) fields and methods, use the following syntax: –ClassName.field; –ClassName.method(args);

9 Access Control ‘public’ class or interface types can be accessed if their package can be accessed. If ‘public’ is not stated for a class or interface, it can only be accessed in the same package (package protected). ‘public’ class members or constructors are accessible. Members of interfaces are implicitly public, while class members are by default package protected. ‘protected’ members are accessible from subclasses or the same package. ‘private’ members are only accessible from the same class. Remind me to diagram this for you. Test Question!

10 Defining a Class [Modifiers] class ClassName [extends SuperClass] [implements list of Interfaces] { class body } // note no ‘;’! —[] means optional —modifiers may be public, abstract, and/or final. ‘abstract’ means no construction allowed. ‘final’ means no inheritance from it. —SuperClass is the name of the class it inherits from. This defaults to Object if not declared. —The list of interfaces are the interfaces (zero or more) that the class provides methods for.

11 Class Body Contents Class members include: –Inherited members from the superclass –Inherited members from interfaces –Declared members Private members are not inherited. Constructors and static initializers are not members and are not inherited. Lots of test questions!

12 Member Fields Modifiers Type Name [= initial value]; –Modifiers include one or more of public, protected, private, final, static, transient, and volatile. –‘static’ means the field is at the class-level. –‘final’ means the field is constant once set. –Initial values are applied either at class initialization if static and otherwise in order of definition. Test Questions!

13 Member Methods [Modifiers] ResultType Name([arguments]) [throws throwDeclarations] {code} (or ;) –Modifiers: public, protected, private, abstract (special), static (class-level), final (cannot be overriden), synchronized (special), and/or native (special). –ResultType is void or a type. –The list of arguments is similar to that in C. –ThrowDeclarations state the (checked) exceptions the method may throw. –{code} is required in a definition. A terminating ; is used in a declaration. –A name may be used more than once if the argument list is different. This is called overloading. Test Questions

14 Final Variables If a local variable, member field, or method argument is not supposed to change, declare it final! In C++, this is known as ‘const correctness’. (See Sutter and Alexandrescu, 2005, C++ Coding Standards, Addison-Wesley.) ‘final’ is your friend! Unchanging values are easier to understand, track, and reason about. The compiler checks that an unchanging value is explicitly given only one value in its lifetime.

15 Static Initializers static {codeBlocks} in the class will be executed at class initialization and can be used to initialize class fields declared prior to the specific static code block being executed. Test Question!

16 Constructors [Modifiers] ClassName([arguments]) [throws throwDeclarations] {code} –See description of member methods. –Constructor are not members and are not inherited. –The constructor code may begin with: ‘this(argument list);’ to call another constructor of the same class ‘super(argument list);’ to call a constructor of the superclass –That’s called ‘constructor chaining’. –If that does not happen, the code implicitly begins with ‘super();’ –A class with no constructor declarations has an implicit constructor generated for it. –Constructors can be declared private or protected to restrict access. Lots of test questions!

17 Interfaces May extend other interfaces. An interface lists the member methods that an implementing class must provide. Member fields of an interface are implicitly public, static, and final, and so must be initialized. Test Questions!

18 Abstract Classes Objects of an abstract class cannot be created in isolation. Hence, to use an abstract class, it must be extended. Member methods of an abstract class may be declared ‘abstract’ with no code. Subclasses must override those methods. Test questions!

19 Arrays Ordered collection or numbered list of values. All the values are of the same type (although they may be of different subtypes). The type of an array is ClassName[]. Multidimensional arrays are handled as in C or C++. An array can also be declared as follows: –ClassName arrayName[]; // avoid! One way to create: –ClassName[] arrayName = new ClassName[dim]; –Values are initialized to default values for class. The size of an array (the.length field) is constant. Test questions!

20 Array Literals ClassName[] arrayName = null; –Represents the absence of the object ClassName[] arrayName = {value list}; –Initializes the array entries to the value list. –These must be of type ClassName. Anonymous arrays can also be created: –new ClassName[] {value list} –Frequently used for temporary arguments. Value lists can be multidimensional as in C/C++. Test questions!

21 Copying and Comparing Reference Types The pointer is copied, not the data. Hence the same data are used. To copy the data (a deep copy), use clone(). If you want to use clone(), you have to override it in your class. Comparisons (e.g., ==) of reference types are done by comparing pointers. Hence two different objects containing the same field values are not equal unless you override equals(). To compare contents, use obj1.equals(Object obj2) You have to override the definition for equals() in your class to compare the fields. First check obj2 is in the correct class (using the ‘instanceof’ operator), then typecast it to an object of that class. Finally compare fields. If you override equals(), you also have to override hashcode(). Important test questions!


Download ppt "Reference Types CSE301 University of Sunderland Harry R Erwin, PhD."

Similar presentations


Ads by Google