View Latest Questions and Answers
|
Ask Questions?
|
Tutorials
Home
Java
Frameworks
Database
Technology
Web Development
Build/Test Tools
Servers
PHP
Home
Java
Java-tips
Oop
OOP
Ask Questions?
View Latest Questions
OOP
Posted on: April 17, 2011 at 12:00 AM
This page discusses - OOP
OOP
OO Analysis & Design
Classes represent domain elements.
May permit reuse.
Other classes necessary
Patterns are standard ways to relate classes
UML is most common visual modelling technique
is-a vs has-a
Java
class
members
variables
instance
One "instance" per object.
Declared out of methods.
Can not be refrenced from static context
static / class
static final used for constants.
Relatively little use outside of constants.
Can have static block initializers.
Only one value for class
methods
instance
Call from out of class qualified by object.
Call within class implicitly this.
Can access all instance and static members
class / static
Call from out of class qualified by class
Can also be qualified by object, which is ignored.
Can only access static members.
Useful if pure computation without state.
Eg, Math class.
classes - See inner classes below
constructors
purpose
Define all fields in consistent state.
defining
Same as class name, no return type
Can be overloaded.
Default
No-param constructor created if no user-defined.
If any defined, no default.
Chaining
First line always calls other contructor
If none, a call on default super is made.
Explicit
super
this
super
Refers to parent class constructor.
this
Refers to other constructors in class.
private
If all constructors private, no one can construct
Use Factory pattern (method returns new object).
finalizers
Ignore - like C++ destrurctors, but may not be called.
inner classes
Result in .class files with $
Inner class instances have implict reference to an outer class instance.
Reference to outer class instance with Outer.this.
Usually created from within outer class.
Can create from outside with eg, new Outer().new Inner()
Named, inside class
Instances need outer instance.
Can reference outer instance variables.
Reference to outer instance is OuterClass.this.
Inside method
named
Anonymous
Defined in a method.
Commonly used for listeners.
Can reference outer class's instance members.
Can reference method's final local variables.
static nested top-level
declared static inside outer class
Only used for name scope
Can not reference outer classes instance variables.
Eg, Box.Filler
abstract
A class is abstract if it doesn't implement all methods.
Must be subclassed to create an object.
Interfaces are like completely abstract classes.
final
final classes can not be extended (eg, String)
interface
Lists methods which must be defined to implement
Can define public static final constants
operations
casting
Upcasting
Casting from a subclass to a superclass.
No explicit cast is required.
No run-time check required.
Downcasting
Casting from a superclass to a subclass/interface.
Requires explicit cast notation.
Run-time check performed (ClassCastException)
== vs equals()
instanceof operator.
Reflection
scope
private
Visible only in same class.
Should be used for data variables.
Write getter/setter methods if changeable.
public
Can be seen by anyone
Commonly used for methods.
protected
can only be seen by child class.
Controversial.
package
can be seen by everyone in package
default
Objects
Allocated on heap with new.
Garbage collected when no references.
packages
Used to group classes and other packages.
Default package if none specified.
Inheritance
Terminology
Superclass - subclass
Parent - child
Base - derived
Anscestor - descendant
Can extend only one parent class.
Can implement multiple interfaces.
Object is the root of class hierarchy.
Overriding
Child class can redefine methods of anscestor class.
toString() is commonly overridden.
Concatenation calls toString()
equals(...) is commonly overridden
Polymorphism
A variable may contain an object of its class, or any subclass.
A call to a method will call the correct class's method.
A method can call super to call overridden method.
Eg, paintComponent.
Overloading
Defining methods which differ in number or types of parameters.
Wrapper classes
Eg, Integer, Float, Character, ...
"Wrap" a primitive value.
Provide a home to utility functions.
Create immutable objects.
Java 5 autoboxing - automatic conversion to/from primitive/wrapper.
Related Tags for OOP:
«Previous
Index
Next»