0% found this document useful (0 votes)
41 views23 pages

Chapter 2 Lecture 3

This document discusses key concepts in object oriented design including abstraction, encapsulation, modularity, inheritance, polymorphism, exceptions, interfaces, abstract classes, casting, and generics. Abstraction involves modeling data structures with precise language while hiding implementation details. Encapsulation keeps internal structures hidden. Modularity divides a system into functional units. Inheritance allows code reuse through hierarchical structures. Polymorphism allows objects to take different forms. Exceptions handle unexpected events. Interfaces define methods without implementation. Abstract classes combine abstract and concrete methods. Casting converts between types. Generics define types at runtime.

Uploaded by

Carlos Sam
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
41 views23 pages

Chapter 2 Lecture 3

This document discusses key concepts in object oriented design including abstraction, encapsulation, modularity, inheritance, polymorphism, exceptions, interfaces, abstract classes, casting, and generics. Abstraction involves modeling data structures with precise language while hiding implementation details. Encapsulation keeps internal structures hidden. Modularity divides a system into functional units. Inheritance allows code reuse through hierarchical structures. Polymorphism allows objects to take different forms. Exceptions handle unexpected events. Interfaces define methods without implementation. Abstract classes combine abstract and concrete methods. Casting converts between types. Generics define types at runtime.

Uploaded by

Carlos Sam
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 23

Object Oriented Design

Lecture 3
Abstraction
 Refinement of complicated system down to its most
fundamental parts and describes these parts in simple,
precise language.
 Applying the abstraction paradigm to the design of data
structures gives rise to abstract data types (ADTs).
 An ADT is a mathematical model of a data structure that
specifies the type of data stored, the operations supported
on them, and the types of parameters of the operations.
 An ADT specifies what each operation does, but not how it
does it. In Java, an ADT can be expressed by an interface,
which is simply a list of method declarations, where each
method has an empty body.
Encapsulation

 An objects ability of keep its internal structures


hidden.
 Encapsulation, states that different
components of a software system should not
reveal the internal details of their respective
implementations.
 One of the main advantages of encapsulation
is that it gives the programmer freedom in
implementing the details of a system
Modularity
 Modularity refers to an organizing principle for code in
which different components of a software system are
divided into separate functional units.
 Modern software systems typically consist of several
different components that must interact correctly in
order for the entire system to work properly.
 Keeping these interactions straight requires that these
different components be well organized.
Inheritance
 Inheritance is a technique that Object Oriented paradigm
provides through a modular hierarchical organizing
structures for code reuse.
 The general class, which is also known as a base class or
superclass, can define standard instance variables and
methods that apply in a multitude of situations.
 A class that specializes, or extends, or inherits from, a
superclass need not give new implementations for the
general methods, for it inherits them. It should only define
those methods that are specialized for this particular
subclass
 To make class a subclass of superclass, we use the
keyword extends.
Dynamic Dispatch
 Dynamic dispatch is the process of selecting which
implementation of a polymorphic operation (method or
function) to call at run time.
 Dynamic dispatch contrasts with static dispatch in which
the implementation of a polymorphic operation is selected
at compile-time..
 This algorithm provides an effective mechanism for
locating reused software.
 It also allows for another powerful technique of object-
oriented programming—polymorphism.
Polymorphism
 refers to the ability of an object variable to take different
forms.
 Two types exist

 override: A method of a superclass is re-defined in the


subclass.

 overloading: A method name is used by more than one


method in the same class.
The Keyword this

 Java provides a keyword called this to reference


the current instance of a class.
 The reference this is useful in cases where we
would like to pass the current object as a
parameter to some method.
 Also to reference a field inside current object that
has a similar name with a variable defined in the
current block of code.
Using Inheritance in Java
 There are two primary ways of using inheritance of classes in Java,
 specialization
 extension
Specialization
 Using specialization we are specializing a general class to
particular subclasses. Such subclasses typically possess
an "is a" relationship to their superclass.
 A subclass then inherits all the methods of the
superclass. For each inherited method, if that method
operates correctly independent of whether it is operating
for a specialization, no additional work is needed.
 If, on the other hand, a general method of the superclass
would not work correctly on the subclass, then we should
override the method to have the correct functionality for
the subclass
Extension
 Using extension, on the other hand, we utilize
inheritance to reuse the code written for methods
of the superclass.

 But we then add new methods that are not present


in the superclass, so as to extend its functionality
Exception

 Exceptions are unexpected events that occur during the


execution of a program.
 An exception can be the result of an error condition or
simply an unanticipated input.
 Exceptions can be thought of as being objects
themselves.
 The exceptions can be thrown and then caught
Throwing Exceptions
 Exceptions are objects that are thrown by code
that encounters some sort of unexpected
condition.
 They can also be thrown by the Java run-time
environment should it encounter an unexpected
condition, like running out of object memory.
 A thrown exception is caught by other code that
"handles" the exception somehow, or the program
is terminated unexpectedly.
Catching Exceptions
 When an exception is thrown, it must be caught or the
program will terminate
 When an exception is caught, it can be analyzed and
dealt with.
 The general methodology for dealing with exceptions is
to "try" to execute some fragment of code that might
throw an exception.
 If it does throw an exception, then that exception is
caught by having the flow of control jump to a
predefined catch bloc
Interface
 An interface is a collection of method declarations with no
data and no bodies.
 In order for two objects to interact, they must "know"
about the various messages that each will accept, that is,
the methods each object supports.
 To enforce this "knowledge," the object-oriented design
paradigm asks that classes specify the application
programming interface (API), or simply interface, that
their objects present to other objects.
Abstract Classes
 An abstract class is a class that contains empty method
declarations (that is, declarations of methods without
bodies) as well as concrete definitions of methods and/or
instance variables.
 Thus, an abstract class lies between an interface and a
complete concrete class.
 Like an interface, an abstract class may not be
instantiated, that is, no object can be created from an
abstract class.
 A subclass of an abstract class must provide an
implementation for the abstract methods of its superclass,
unless it is itself abstract
Abstract Methods

 An abstract class contains both abstract methods and


normal methods.
 The abstract methods must be overridden in subclasses.
Because of the abstract methods, an abstract class
cannot be instantiated
Casting
 We use casting to convert data types in java
 Two type of Casting
 Widening Conversions
 Narrowing Conversions
Widening Conversions
 A widening conversion occurs when a type T is converted into
a "wider" type U. The following are common cases of widening
conversions:

 T and U are class types and U is a superclass of T


 T and U are interface types and U is a super interface of T
 T is a class that implements interface U.
 Example.
 Integer i = new Integer (3);
 Number n = i; // widening conversion from Integer to Number
Narrowing Conversions

 A narrowing conversion occurs when a type T is converted


into a "narrower" type S. The following are common cases
of narrowing conversions.

 T and S are class types and S is a subclass of T


 T and S are interface types and S is a sub interface of T
 T is an interface implemented by class S.
 Example:
 Number n = new Integer (2); // widening conversion from Integer to Number
 Integer i = (Integer) n; // narrowing conversion from Number to Integer
Casting Exceptions
 We can cast an object reference O of type T into a type
S, provided, object o is referring to is actually of type S.
 If object O is not also of type S, then attempting to cast O
to type S will throw an exception called
ClassCastException.
 Example
 Number n;
 Integer i;
 n = new Integer(3);
 i = (Integer) n; // This is legal
 n = new Double(3.1415);
 i = (Integer) n; // This is illegal
Casting with Interfaces
 Interfaces allow us to enforce that objects implement
certain methods, but using interface variables with
concrete objects sometimes requires casting.
 Example:
Generics
 A generic type is a type that is not defined at compilation
time, but becomes fully specified at run time.
 The generics framework allows us to define a class in
terms of a set of formal type parameters, with could be
used to abstract the types of some internal variables of
the class.
 Angle brackets are used to enclose the list of formal type
parameters.

You might also like