You are on page 1of 27

COMP 223/INTE 222/COSF 311

OOP WITH JAVA

LECTURE ONE*
INTRODUCTION

BY: JOYCE*
Devotional Meditation

Romans 15:4
For whatever was written in earlier times was written
for our instruction, so that through perseverance and
the encouragement of the Scriptures we might have
hope.
Lecture Outline

1. Introduction to OOP.
2. Overview of OOP concepts
3. Introduction to Java
4. History of Java
5. Features of Java
Object-oriented programming (OOP) is a way to
organize and conceptualize a program as a set of
interacting objects.
Key Object-Oriented Systems Concepts

Objects and Classes


Encapsulation
Methods and Variables
Inheritance
Message Passing and Polymorphism
Basic Software Lifecycle Concepts
Introduction to OOA/OOD
What is an Object?
Real-world objects have attributes and
behaviors.

Example:
Dog
Attributes: breed, color, hungry, tired, etc.
Behaviors: eating, sleeping, etc.
Software Objects - Cont’d

In traditional programming languages (Fortran, Cobol, C, etc)


data structures and procedures are defined separately.
In object-oriented languages, they Bank
are defined together. Account
Account
An object is a collection of attributes Account
and the behaviors that operate on
number:
them.
balance:
Procedures associated with an
deposit()
object are called methods.
withdra()
Classes

The definitions of the attributes and methods of an object are


organized into a class. Thus, a class is the generic definition
for a set of similar objects

A class can be thought of as a template used to create a set of


objects.
A class is a static definition; a piece of code written in a
programming
Classes - Cont’d

Each instance will have its own distinct set of


attributes.
Every instance of the same class will have the
same
set of attributes;
Bank Example

The "account" class describes the class: Account


attributes and behaviors of bank number:
accounts.
balance:

deposit()
The “account” class defines two
state variables (account number withdraw()

and balance) and two methods


(deposit and withdraw).
Bank Example - Cont’d
Instance #1
When the program runs there will be number: 054
many instances of the account class.
balance: $19

Each instance will have its own Instance #2

account number and balance (object number: 712


state)
balance: $240

Methods can only be invoked. Instance #3

number: 036

balance: $941
Encapsulation
When classes are defined, programmers can specify that
certain methods or state variables remain hidden inside the
class.
 The combination of collecting all
Visible
the attributes of an object into a Methods
Hidden
single class definition, combined State
with the ability to hide some Variable
s and
definitions and type information Method
s
within the class, is known as Visible
encapsulation. Variables

Class
Definition
Graphical Model of an Object

balance()
Instance
variables

withdraw() theBalance deposit()


acctNumber

Methods
accountNumber()

State variables make up the nucleus of the object. Methods surround


and hide (encapsulate) the state variables from the rest of the
program.
Inheritance

The advantage of making a new class a subclass is that it will


inherit
attributes and methods of its parent class (also called the
superclass).

Subclasses extend existing classes in three ways:


By defining new attributes and methods.
Subclasses
When a new class is developed a programmer can define it to
be a subclass of an existing class.
Subclasses are used to define special cases, extensions, or other
variations from the originally defined class.
Examples:
Generic Class Terrier is
 Terrier can be defined as a derived
for Dog
from Dog
subclass of Dog. With general
 SavingsAccount and attributes and Specific Class
behaviors for all for Terrier
CheckingAccount can be dogs. With new
derived from the Account class attributes and
behaviors
specific to the
Terrier breed.
Polymorphism

Polymorphism is one of the essential features of an object-oriented


language.
This is the mechanism of decoupling the behavior from the message.

The same message sent to different types of objects results in: execution
of
behavior that is specific to the object and, possibly different behavior than
that of other objects receiving the same message.
Polymorphism – Cont’d

There are many forms of Polymorphism in object-oriented


languages, such as:

True Polymorphism: Same method signature defined for different classes


with different behaviors (i.e. draw() for the Classes Circle and Square)
Parametric Polymorphism: This is the use of the same method name
within a class, but with a different signature
Overloading: This usually refers to operators (such as +,-,/,*, etc) when
they can be applied to several types such as int, floats, strings, etc.
Overriding: This refers to the feature of subclasses that replace the
behavior of a parent class with new or modified behavior.
OO Concepts Summary

Object-oriented programming is a way of conceptualizing a program as


groups of objects that interact with one another.
A class is a general template used to create objects.
The combination of collecting all the attributes of an object into a single
class
definition, combined with the ability to hide some definitions within the
class,
is known as encapsulation.
Introduction
Introduction
Java programming language was originally developed by Sun
Microsystems which was initiated by James Gosling and released
in 1995 as core component of Sun Microsystems' Java platform.

The latest release of the Java Standard Edition is Java SE 8.

The new J2 versions were renamed as Java SE, Java EE and Java
ME respectively. Java is guaranteed to be Write Once, Run
Anywhere.
Java is:
Object Oriented: In Java, everything is an Object. Java can be easily extended
since it is based on the Object model.

Platform independent: Unlike many other programming languages including C


and C++, when Java is compiled, it is not compiled into platform specific machine,
rather into platform independent byte code.

This byte code is distributed over the web and interpreted by virtual Machine JVM
on whichever platform it is being run.

Simple: Java is designed to be easy to learn. If you understand the basic concept of
OOP Java would be easy to master.
Secure: With Java's secure feature it enables to develop virus-free, tamper-free systems.
Authentication techniques are based on public-key encryption.

Architectural-neutral: Java compiler generates an architecture-neutral object file format


which makes the compiled code to be executable on many processors, with the presence of
Java runtime system.

Portable: Being architectural-neutral and having no implementation dependent aspects of


the specification makes Java portable. Compiler in Java is written in ANSI C with a clean
portability boundary which is a POSIX subset.

Robust: Java makes an effort to eliminate error prone situations by emphasizing mainly on
compile time error checking and runtime checking.
Multithreaded: With Java's multithreaded feature it is possible to write programs
that can do many tasks simultaneously. This design feature allows developers to
construct smoothly running interactive applications.
Interpreted: Java byte code is translated on the fly to native machine instructions
and is not stored anywhere. The development process is more rapid and analytical
since the linking is an incremental and light weight process.
High Performance: With the use of Just-In-Time compilers, Java enables high
performance.
Distributed: Java is designed for the distributed environment of the internet.
Dynamic: Java is considered to be more dynamic than C or C++ since it is designed
to adapt to an evolving environment. Java programs can carry extensive amount of
run-time information that can be used to verify and resolve accesses to objects on
run-time.
History of Java:
James Gosling initiated the Java language project in June 1991 for use in one of his many set-
top box projects. The language, initially called Oak after an oak tree that stood outside
Gosling's office, also went by the name Green and ended up later being renamed as Java, from
a list of random words.

Sun released the first public implementation as Java 1.0 in 1995. It promised Write Once,
Run AnywhereWORA, providing no-cost run-times on popular platforms.

On 13 November 2006, Sun released much of Java as free and open source software under the
terms of the GNU General Public License GPL.

On 8 May 2007, Sun finished the process, making all of Java's core code free and open-
source, aside from a small portion of code to which Sun did not hold the copyright.
Tools you will need:
For performing the examples discussed in this tutorial, you will need a
Pentium 200-MHz computer
with a minimum of 64 MB of RAM 128MBofRAMrecommended.
You also will need the following softwares:
Java JDK 8
Microsoft Notepad or any other text editor especially netbeans.
END

You might also like