|
return to computer science and java
Section 1: Declarations and Access Control
Section 2: Flow Control, Assertions, and Exception Handling
Section 3: Garbage Collection
Section 4: Language Fundamentals
Section 5: Operators and Assignments
Section 6: Overloading, Overriding, Runtime Type and Object Orientation
Section 7: Threads
Section 8: Fundamental Classes in the JAVA.LANG Package
Section 9: The Collections Framework
|
|
- State the benefits of encapsulation in object oriented design and write code that implements tightly encapsulated classes and the relationships "is a" and "has a".
- Write code to invoke overridden or overloaded methods and parental or overloaded constructors; and describe the effect of invoking these methods.
- Write code to construct instances of any concrete class including normal top level classes and nested classes.
Objects and Classes
- Object Oriented Design
- abstract data types
- encapsulatin - private member/instance variables, access with getter and setter methods only, so hide implementation
- re-use:
- has a - composition - new member variables
- is a - inheritance - extends...
- Overloading Method Names
- in a class or subclass
- argument list must change, return type, accessability and exception list may or may not change
- Method Overriding
- Overriding Methods
- have same name, argument list and return type.
- their accessability may not be more restricted than the original method (can't override a method to be more private)
- can only throw a subset (including all or none) of the checked exceptions thrown by the overriden method (i.e. can't throw any new checked exceptions)
- the method of the actual (as created by
new) object is used when called, not of the referring object
| |
e.g. |
ParentClass obj = new ChildClass();
obj.someMethod(); //the overridden method
the method executed will be that in ChildClass, not in ParentClass |
should be able to perform all methods on obj that we can on any instance of ParentClass, this is why overriding methods can't be more restrictive
- late binding or virtual method invocation - the runtime system (the Java Virtual Machine) determines from a method invocation and the object calling it (not the variable type) which method to run
- to invoke overriden methods in a sub-classes method use
super.someMethod() (can call immediate parent's method only)
useful when overriding a method only requires an addition to the original method
- Constructors and Subclassing
- Constructors
- default constructor
- takes no arguments
- created by compiler in absence of any defined constructor
- can overload constructors
- constructors can have any level of access, but cannot be declared final, abstract, static, native or synchronized
- can call an overriden constructer from another with
this(<other constructors argument list); as the first statement
- Constructors and Subclassing
- constructors are not inherited so must be explicitly defined else get default constructor
- if subclass constructor wants to pass control to parent, use
super(<parents constructor argument list); as the first statement (must be) (if no call t super(...) or this(...) then compiler inserts an empty super() as first statement)
- if call another constructor first with
this(...), then super() is not implicitly called till the otehr constructor runs (other constructor itself can start with a call to super(...) or this(...))
- so constructors are called top-down in the hierarchy
- consequence: if extend a class without a no-argument constructor, must explicitly call
super(...)
- Inner Classes (Nested Classes)
- fully qualified name:
OuterClass.InnerClass (illegal for a package and class to have the same name so no ambiguity)
- from file systems point of view it is
OuterClass$InnerClass (so innerClassInstance.getClass().getName() will return the $-separated version)
- have access to all of the outer class's members (even private ones)
- creating an instance of th einner class
- requires an instance of the outer class first
- so, if from outside class, or in a static method, can use
e.g. OuterClass.InnerClass obj = new OuterClass().new InnerClass()
- if don't use instance of OuterClass, assume an implicit '
this.' before the inner class name - this is not present in a static context, and would refer to the wrong class if invoking from a completely different class
Member Classes
- defined inside another class but outside any methods
- access modifiers -
private|(default)|protected|public (as per other members: variables and methods)
static inner classes - don't have access to any of the outer classes non-static variables or methods
- non-static inner classes can't have static members
- can create an instance of a
static inner class without an instance of the outer class
e.g. from a static context of the OuterClass:
StaticInnerClass instance = new OuterClass.StaticInnerClass();
Classes Defined Inside Methods
- local, therefor can't be marked with
static or access modifiers (but can be marked with abstract or final)
- can only access the methods local variables marked
final (i.e. constants), and the class's member variables which the method can access
- can declare an argument variable to a method as
final
- Anonymous Classes
- either explicitly implement a single interface or extend a class
- construct/create with either
new InterfaceName() { <overriden methods>}
new ClassName() { <overriden methods>}
as an assignment to an object, or an argument to a method
- can pass in variables to the superclasses constructor
e.g. new ClassName(anArgument) { ...}
and the compiler will call super(anArgument)
- initialization - as can't create own constructor, but if want to do something special on initialization, simply put code in an unnamed block
e.g. new ClassName(anArgument) {
{//initialization code
System.out.println("initializing my anonymous class");
}
}
|