05 March 2008

Object Orientation (Part II)

Encapsulation, IS-A, HAS-A

  • Encapsulation helps hide implementation behind an interface (or API).
  • Encapsulated code has two features: Instance variables are kept protected (usually with the private modifier) and Getter and setter methods provide access to instance variables.
  • IS-A refers to inheritance it is expressed with the keyword extends.
  • IS-A, "inherits from," and "is a subtype of" are all equivalent expressions.
  • HAS-A means an instance of one class "has a" reference to an instance of another class.
Inheritance
  • Inheritance is a mechanism that allows a class to be a subclass of a superclass, and thereby inherit variables and methods of the superclass.
  • Inheritance is a key concept that underlies IS-A, polymorphism, overriding, overloading, and casting.
  • All classes (except class Object), are subclasses of type Object, and therefore
  • they inherit Object's methods.
Polymorphism
  • Polymorphism means ‘many forms'.
  • A reference variable is always of a single, unchangeable type, but it can refer to a subtype object.
  • A single object can be referred to by reference variables of many different
  • types as long as they are the same type or a supertype of the object.
  • The reference variable's type (not the object's type), determines which methods can be called!
  • Polymorphic method invocations apply only to overridden instance methods.
Overriding and Overloading
  • Methods can be overridden or overloaded; constructors can be overloaded but not overridden.
  • Abstract methods must be overridden by the first concrete (nonabstract) subclass.
  • final methods cannot be overridden.
  • Only inherited methods may be overridden, and remember that private methods are not inherited.
  • Overloading means reusing amethod name, but with different arguments.
  • Methods from a superclass can be overloaded in a subclass.
  • Polymorphism applies to overriding, not to overloading
  • Object type (not the reference variable's type), determines which overridden method is used at runtime.
  • Reference type determines which overloaded method will be used at compile time.

No comments: