lecture 4
lecture 4
lecture 4
Programming (CS2012)
Lecture 4
Fundamental Principles of OOP
Fundamental Principles of OOP
• Inheritance
– Inherit members from parent class
• Abstraction
– Define and execute abstract actions
• Encapsulation
– Hide the internals of a class
• Polymorphism
– Access a class through its parent interface
Abstraction
• Abstraction – we know what an object does and
how to use it, but not how it does what it does
– Ignoring irrelevant features, properties, or functions
and emphasizing the relevant ones to a given project
– E.g. a TV
• Know what vs know how
• Abstraction helps
– managing complexity
– Managing change
– Code reuse
Abstraction
• Abstraction is something we do every day
– Looking at an object, we see those things about it
that have meaning to us
– We abstract the properties of the object, and keep
only what we need
– E.g. students get "name" but not "color of eyes"
• Allows us to represent a complex reality in terms
of a simplified model
• Abstraction highlights the properties of an entity
that we need and hides the others
Abstraction in Java
• Interfaces
• Abstract classes
Abstract Classes (in Java)
• Just like an ordinary class
• But cannot be instantiated
• Then how can it be used???
Abstract Classes
• Parent class contains the common
functionality of a collection of child classes
• But the parent class itself is too abstract to be
used on its own
• Impose what should be there but not how it
should be done
Abstract Class - Analogy
• Vehicles
• Birds
Abstract Methods
• If you want a class to contain a particular
method but you want the actual
implementation of that method to be
determined by child classes
• Can be included ONLY in an abstract class
• Any child class must either override the
abstract method or declare itself abstract
Level of abstraction
• Interfaces???
• Abstract Classes???
Interface or Abstract Class???
• Use abstract class if
– You think you will plan on using inheritance since
it provides a common base class implementation
to derived classes
– You want to be able to declare non-public
members.
– If you think you will need to add methods in the
future
– If some methods have a common implementation
across sub-classes
Interface or Abstract Class???
• Use interface if
– You think that the API will not change for a while
– When you want to have something similar to
multiple inheritance, since you can implement
multiple interfaces.
Abstract Data Types
• An abstract data type (ADT) is a model of a
data structure that specifies:
– The characteristics of the collection of data
– The operations that can be performed on the
collection
• It is abstract because it doesn’t specify how
the ADT will be implemented
– A given ADT can have multiple implementations
ADTs
Abstract Data Types - Queue
• A container for a group of data items
– Queue<E>
• Has specific characteristics
– FIFO
• Supports a set of operations
– Insert
– Remove
– Examine
Abstraction Example in Java Class
Hierarchy
• https://docs.oracle.com/javase/tutorial/collec
tions/interfaces/queue.html