OO Concepts - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

OO Concepts

Description:

A well-designed module hides all of its implementation details, cleanly ... Arnold, Ken, Gosling James, Holmes David. THE Java Programming Language. ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 15
Provided by: selma4
Category:
Tags: concepts | gosling

less

Transcript and Presenter's Notes

Title: OO Concepts


1
OO Concepts
  • Selma Tekir
  • selmatekir_at_iyte.edu.tr

2
Information Hiding
  • A well-designed module hides all of its
    implementation details, cleanly separating its
    API from its implementation. This concept is
    known as information hiding or encapsulation.

3
Minimize the Accessibility
  • You should always reduce accessibility as much as
    possible.
  • After carefully designing a minimal public API,
    you should prevent any stray classes, interfaces,
    or members from becoming a part of the API.
  • With the exception of public static final fields,
    public classes should have no public fields.
  • Ensure that objects referenced by public static
    final fields are immutable.

4
Inheritance
  • It is appropriate only when a genuine subtype
    relationship exists between the subclass and the
    superclass.
  • Use composition and forwarding instead of
    inheritance, especially if an appropriate
    interface to implement a wrapper class exists.

5
Interface Usage
  • An interface is generally the best way to define
    a type that permits multiple implementations.
  • If you export a nontrivial interface, you should
    strongly consider providing a skeletal
    implementation to go with it.

6
Interface Usage
  • Interfaces should be used only to define types.
  • They should not be used to export constants.

7
  • // Constant interface pattern - do not use!
  • public interface PhysicalConstants
  • // Avogadro's number (1/mol)
  • static final double AVOGADROS_NUMBER
    6.02214199e23
  • // Boltzmann constant (J/K)
  • static final double BOLTZMANN_CONSTANT
    1.3806503e-23
  • // Mass of the electron (kg)
  • static final double ELECTRON_MASS
    9.10938188e-31

8
  • //Constant utility class
  • public class PhysicalConstants
  • private PhysicalConstants()
  • //Prevents instantiation
  • public static final double AVOGADROS_NUMBER
    6.02214199e23
  • public static final double BOLTZMANN_CONSTANT
    1.3806503e-23
  • public static final double ELECTRON_MASS
    9.10938188e-31

9
Comparable
  • public interface ComparableltTgt
  • int compareTo(T obj)

10
  • class Point implements ComparableltPointgt
  • / Origin reference that never changes /
  • private static final Point ORIGIN new
    Point()
  • private int x, y
  • // ... definition of constructors, setters
    and accessors
  • public double distance(Point p)
  • int xdiff x - p.x
  • int ydiff y - p.y
  • return Math.sqrt(xdiff xdiff ydiff
    ydiff)
  • public int compareTo(Point p)
  • double pDist p.distance(ORIGIN)
  • double dist this.distance(ORIGIN)
  • if (dist gt pDist)

11
  • public interface Attributed
  • void add(Attr newAttr)
  • Attr find(String attrName)
  • Attr remove(String attrName)
  • java.util.IteratorltAttrgt attrs()

12
  • import java.util.
  • class AttributedImpl implements Attributed,
    IterableltAttrgt
  • protected MapltString, Attrgt attrTable
  • new HashMapltString, Attrgt()
  • public void add(Attr newAttr)
  • attrTable.put(newAttr.getName(),
    newAttr)
  • public Attr find(String name)
  • return attrTable.get(name)
  • public Attr remove(String name)
  • return attrTable.remove(name)
  • public IteratorltAttrgt attrs()

13
  • import java.util.Iterator
  • class AttributedBody extends Body
  • implements Attributed
  • private AttributedImpl attrImpl new
    AttributedImpl()
  • public AttributedBody()
  • super()
  • public AttributedBody(String name, Body
    orbits)
  • super(name, orbits)
  • // Forward all Attributed methods to the
    attrImpl object
  • public void add(Attr newAttr)
  • attrImpl.add(newAttr)

14
References
  • Bloch, Joshua. Effective Java Programming
    Language Guide. Prentice Hall, June 2001.
  • Arnold, Ken, Gosling James, Holmes David. THE
    Java Programming Language. Prentice Hall Fourth
    Edition, August 2005.
Write a Comment
User Comments (0)
About PowerShow.com