OOP Concepts

by Michael Thomas

(Tutorial Home Page)

(Hierarchy & Access Control)

by Michael Thomas

(Tutorial Home Page)

by Michael Thomas

(Tutorial Home Page)

Topic Notes
Object-Oriented Hierarchy

"Is A" vs "Has A"

"Is A" - is a subclass of a superclass (ex: extends)
"Has A" - has a reference to (ex: variable, ref to object).

Shape is abstract in design. Shape wants every child to have an area() method.

  • abstract class Shape
  • abstract double area();

Square, Circle is a Shape:

  • class Square extends Shape
  • class Circle extends Shape

Circle has a Area:

  • double dblArea;
  • double area() {return dblArea; }

Note:

  • This example is fairly easy to conceptualize. Some designs are difficult.
  • Creating class hierarchies falls somewhere between engineering and craft.
Access Control Access Control - a way to limit the access others have to your code.
  • Same package - can access each others’ variables and methods, except for private members.
  • Outside package - can access public classes. Next, can access members that are public. Also, can access protected members if the class is a subclass of that class.

Same package - use package keyword in first line of source file, or no package keyword and in same directory.

Keywords - Access Control

  1. public - outside of package access.
  2. [no keyword] - same package access only.
  3. protected - same package access. Access if class is a subclass of, even if in another package.
  4. private - same class access only.

Members (Class, Methods, & Variables) - if the "access control keyword" can be used with the member, the above concepts apply.

Role in Hierarchy Hierarchy deals with the design of classes (super classes, subclasses, methods, overriding methods, etc…). It may limit their ability to extend or inherit your code. It may force others to code certain behaviors into their classes.

Keywords - Role in Hierarchy

  1. abstract - used to force a design. Cannot instantiated.
  2. final - Cannot be changed.
  3. static - exists with the class. Exists only once!
  4. * native - platform dependent code (ex: C).
  5. *volatile - variable is not stable among threads.
  6. *synchronized - method is invoked by one thread at a time.
  7. *transient - will not be serialized to a stream when writing an object.

Members (Class, Methods, & Variables) - if the "access control keyword" can be used with the member, the above concepts apply.

Class Class Access & Hierarchy Keywords
Class-Keywords Access Control
  • no keyword - package access
  • public - outside package access.

Role in Hierarchy (design)

  • abstract - class cannot be instantiated.
  • final - class cannot be subclassed (extended).
Invalid/Compile Errors
  • Class – abstract & final (an abstract class should be extended, final cannot!)
Class-no keyword
  • Package access.
Class-public
  • If you import a package, you can only access those package classes that are declared public.
  • Can be accessed by all classes.
  • Must be in its own .java source file.
  • Can only be one public class per .java source file. If you do define a public class, the source file must be named after the class.
Class-abstract
  • Class must be abstract if it has an abstract method.
  • Class – cannot be instantiated.
  • Class can be abstract even, if no methods are declared as abstract.
  • (Ex: wrappers all subclass from Number which is abstract.)
  • You can force a certain design. (see Number class).
Class-final
  • Class can not be subclassed (extended).
  • (Ex: Double, Integer, Long, Float, String.)
  • Your design is more secure. Code is optimized for speed because the reference can be constant, not a lookup reference.
  • If a class cannot be subclassed, then none of the methods can be overlaided.
Method Method Access & Hierarchy Keywords
Method-Keywords If access to Class, then:

Access Control

  • no keyword - package access.
  • public - inside & outside package access.
  • protected - package access or access if subclassed.
  • private - this class only access.

Role in Hierarchy (design)

  • abstract - design. No code.
  • final - cannot be overloaded
  • static - belongs to class. Can be accessed without instantiation.
  • synchronized - method can be invoked by one thread at a time.
Invalid/Compile Errors
  • Method – abstract & static (why? a static method cannot be overridden, abstract must be!)
  • Method – abstract & final (a final method cannot be overridden, abstract must be!)
Method-no keyword
  • Method can be accessed by classes in the same package.
Method-public
  • Method can be accessed by any class
Method-private
  • Method can be accessed by other methods within the class.
  • No access by package or outside package.
Method-protected
  • Method can be accessed by package.
  • Method can be accessed by any subclass of this class.
Method-abstract
  • Has no defined code, therefore class must be abstract.
  • Defines the method's signature, return value, and the exceptions it throws.
  • You can force a certain design. Concrete classes must override the method.
  • The class must then be abstract.
  • Method - must be overridden, or subclass will be abstract .
  • ex: public String getVersion(); ( notice no "{ }", use ";" )
Method-final
  • No subclass can override the method.
  • If final, the method cannot be abstract!
  • Optimizes code for speed.
Method-static
  • Can be accessed without instantiation.
  • Cannot be overlayed, therefore cannot be abstract.
Method-synchronized
  • Method can only be invoked by one thread at a time.
  • Apply to class mehods, instance methods or code blocks. Variables can not be!
  • Can be static or non-static.
  • If non-static - no other thread can invoke any other synchronized instance method for that object.
  • If static - no other thread can invoke any other synchronized static method for that class
Method-native
  • Method is a platform dependent language, not Java.
Variables Variables Access & Hierarchy Keywords
Variables-Keywords If access to Variable, then:

Access Control

  • no keyword - package access.
  • public - inside & outside package access.
  • protected - package access or access if subclassed.
  • private - this class only access.

Role in Hierarchy (design)

  • final - cannot be overloaded
  • static - belongs to class. Can be accessed without instantiation.
  • volatile -may change asynchronously with threads
  • transient - will not be serialized
Invalid/Compile Errors Cannot be: (Compile errors)
  • abstract, native, synchronized.
  • local variable cannot be static
Variables-static
  • Belongs to the class
  • Exists exactly once for a class, no matter how many instances are created.
  • Can not use: this.intMyVar (this refers to a non-static (object) member)
  • A local variable cannot be declared static.  You will get some interesting compile errors including "statement expected".
  • Static Variables must be declared in the section just after the class statement and before a method or constructor clause.
Variables-abstract Illegal!
Variables-private Members – only accessed by class in which it is defined
Variables-no keyword
  • package access.
Variable-final
  • Value cannot be changed. It is constant.
Variable-volatile
  • A variable member may change asynchronously with threads
Variable-transient
  • Instance variables that are transient will not be serialized.
   
import
  • If you import a package, you can only access those package classes that are declared public.
  • If you import a package, you can only access those methods that are declared public.
package  
   

Access & Role Keyword

Legal Chart

(Y = legal to use!)

Keyword Description Class Methods Block Variables
Access Control          
[default] Access-package only Y Y   Y
Public Access-all Y Y   Y
Private Access-class only   Y   Y
Protected Access-package & subclasses   Y   Y
Role in Hierarchy          
Abstract Used for design, not instantiation. Y Y    
Final Cannot be changed. Y Y   Y
Static Exists with class, not object
(Inner class can be static)
  Y   Y
Native Platform dependent code.   Y    
Volatile Variable is not stable among threads       Y
Synchronized Method invoked by one thread at a time.   Y Y  
Transient Will not be serialized.       Y