Sie sind auf Seite 1von 42

Object Oriented Programming in Java

Swakkhar Satabda and Sanjay Saha

CSI 211: Object Oriented Programming, Spring 2017


Department of Computer Science and Engineering
United International University

October, 2016

United International University CSI 211: Object Oriented Programming 1


Two Paradigm

Procedural Programming
Process oriented, code that acts on data.
What is happening?
A program is a series of linear steps - C!
As program grows larger - hard to manage!

Object Oriented Programming


data controls access to codes
Who is being affected?
Program is organized arround its data - easy to manage / Java

United International University CSI 211: Object Oriented Programming 2


Abstraction

Abstraction facilitates the easy conceptualization of real world


objects into the software program.
Humans manage complexity through abstraction.
people do not think of a car as a set of tens of thousands of
individual parts. They think of it as a well-defined object with
its own unique behavior.
A powerful way to manage abstraction is through the use of
hierarchical classifications.

United International University CSI 211: Object Oriented Programming 3


OOP Principles

United International University CSI 211: Object Oriented Programming 4


Encapsulation

Binds together code and the data it manipulates


Keeps code and data safe from outside interference and
misuse.
Real Life Example: Car
In Java: A class contains member variables and methods.
Encapsulation guarantees the integrity of the data contained
in the object.
United International University CSI 211: Object Oriented Programming 5
Inheritence
Motor Car
-speedLimit
-startEngine()

-infotainmentController -xDriveDivider -extraSafetyBag


-hFunction() -bmwOperation() -nFunction()

Inheritance is the process by which one object acquires the


properties of another object.
An object needs to define those qualities that make it unique
within its class and inherits general attributes from its parent.
In Java - super class, sub CSI
United International University
class
211: Object Oriented Programming 6
Polymorphism

Motor Car
-accelerate()

-accelerate() -accelerate() -accelerate()

Driver
-drive(BMW)
-drive(Nissan)
-drive(Honda)

Polymorphism (from Greek, meaning many forms)


One interface, multiple methods
Think about a method that finds the length of strings and
integers.
United International University CSI 211: Object Oriented Programming 7
OOP Terminology

Class: A class is a blueprint or prototype from which objects


are created.
Object: An object is a software bundle of related state and
behavior.
Interface: An interface is a contract between a class and the
outside world. When a class implements an interface, it
promises to provide the behavior published by that in interface
Package: A package is a namespace for organizing classes
and interfaces in a logical manner.
Hierarchy: The mapped relationships of sub- and super
classes is known as a hierarchy.
Information Hiding: Purpose of hiding is to make
inaccessible certain details that should not affect other parts
of a system.
United International University CSI 211: Object Oriented Programming 8
An Example Class

United International University CSI 211: Object Oriented Programming 9


How to use this?

United International University CSI 211: Object Oriented Programming 10


How to use this?

From command line


$ javac Rectangle.java RectangleTest.java
$ java RectangleTest
United International University CSI 211: Object Oriented Programming 11
Fields

United International University CSI 211: Object Oriented Programming 12


Fields

Each Object has its own copy of the fields defined in the class
rOne and rTwo has got different values for their height and
width
Similar to struct in C

United International University CSI 211: Object Oriented Programming 13


Methods

In addition there are methods


getArea() calculates and returns the area of the rectangle
Methods access the fields and perform operations on them
When a method change the value of a field, the change
remains in effect after the method ends
Lets add another method to the class Rectangle to see this
effect!
Lets call this new method increaseWidth(int w)
This method will increase the area of the rectangle by an
specified amount w

United International University CSI 211: Object Oriented Programming 14


A Modified Rectangle

United International University CSI 211: Object Oriented Programming 15


Another Test Example

United International University CSI 211: Object Oriented Programming 16


Fields: A Good Java Practice

Fields should be private! (Information Hiding)


Doing that, these are not accessible outside the class
We need public methods that will have access to them
(setters and getters)
We need to change our Rectangle class again
add setWidth(), getWidth()
add setHeight(),getHeight()

United International University CSI 211: Object Oriented Programming 17


Set and Get Methods

United International University CSI 211: Object Oriented Programming 18


Set and Get Methods

United International University CSI 211: Object Oriented Programming 19


Method Overloading

United International University CSI 211: Object Oriented Programming 20


Method Overloading

United International University CSI 211: Object Oriented Programming 21


Initialization of Fields

There are three ways to initialize the fields


Direct Assignment
Initialization Block
Constructors
If you do not initialize your fields
Direct Assignment
Objects are referenced as null

United International University CSI 211: Object Oriented Programming 22


Direct Assignment

United International University CSI 211: Object Oriented Programming 23


Direct Assignment

United International University CSI 211: Object Oriented Programming 24


Initialization Block

United International University CSI 211: Object Oriented Programming 25


Constructors

Constructors have the same name as their class


Constructors have no return type
Constructors are not methods! You can only call a constructor
using new ClassName()
Constructors are called when objects are created (using
new)

When you do not explicitly define a constructor for a class, then


Java creates a default constructor for the class. The default
constructor automatically initializes all instance variables to their
default values, which are zero, null, and false, for numeric types,
reference types, and boolean, respectively.

United International University CSI 211: Object Oriented Programming 26


Default Constructor

United International University CSI 211: Object Oriented Programming 27


Default Constructor

If a class has no constructor the compiler generates an default


constructor with no arguments for the class.
If a class has any constructor, the compiler will not generate
the default constructor.
If you add a constructor to an existing class, you may need to
also add a constructor with no arguments. (good practice)

United International University CSI 211: Object Oriented Programming 28


Parameterised Constructor

United International University CSI 211: Object Oriented Programming 29


Parameterised Constructor

If a class have a constructor defined and do not have a default


constructor you can not just call the following
BankAccount acc= new BankAccount(); // error

United International University CSI 211: Object Oriented Programming 30


Multiple Constructors
Multiple constructors must have different signatures.

United International University CSI 211: Object Oriented Programming 31


Constructors Overloaded

United International University CSI 211: Object Oriented Programming 32


Copy Constructor

United International University CSI 211: Object Oriented Programming 33


Copy Constructor

United International University CSI 211: Object Oriented Programming 34


Order Test

When you create an object the direct assignment of fields and


instance initialization blocks are done in order from top to
bottom of class, then the constructor is executed

United International University CSI 211: Object Oriented Programming 35


Order Test (Output)

United International University CSI 211: Object Oriented Programming 36


this - a reference to self

United International University CSI 211: Object Oriented Programming 37


toString()

United International University CSI 211: Object Oriented Programming 38


toString()

United International University CSI 211: Object Oriented Programming 39


toString()

United International University CSI 211: Object Oriented Programming 40


Reading

Java:Complete Reference Chapter 6


Java: How to Program Chapter 3 (Early Objects)

United International University CSI 211: Object Oriented Programming 41


Thats it!

Thank you

United International University CSI 211: Object Oriented Programming 42

Das könnte Ihnen auch gefallen