Chapter One Introduction To Object Oriented Programming OOP
Chapter One Introduction To Object Oriented Programming OOP
programming
Chapter 1
Introduction to Object-Oriented
Programmi
Contents ng
1. Overview of OOP?
2. Why Java?
3. The JVM and Byte Code
4. Basic concepts of OOP
1. Classes
2. Objects
3. Members
4. Class member visibility
5. Methods
6. Encapsulation, inheritance & polymorphism
2
1.1 Overview of OOP
• JAVA was developed by Sun Microsystems Inc
in 1991, later acquired by Oracle
Corporation. It was conceived by James
Gosling and Patrick Naughton.
• It is a simple programming language. Writing,
compiling and debugging a program is easy
in java.
• It helps to create modular programs and
reusable code.
3
Cont’d …
New features added in Java:
Multithreading, that allows two or more pieces of
the same program to execute concurrently.
C++ has a set of library functions that use a
common header file. But java replaces it with
its own set of API classes.
It adds packages and interfaces.
Java supports automatic garbage collection.
break and continue statements have been enhanced
in java to accept labels as targets.
The use of unicode characters ensures portability.
4
Cont’d …
Features that differ:
7
WORA
(Write Once Run Anywhere)
8
Tools you will need:
13
Cont’d…
4. Robust Language
• Two main problems that cause program failures
are memory management mistakes and
mishandled runtime errors. Java handles both of
them efficiently.
21
1.3.4 Java Source code
1) It essentially consists of a main() method
2) This method is public and thus can be called by
any object
3) This method is also static and so can be called
without instantiating the object of the
class . It does not return any value.
4) The controlling class of every Java application
usually contain a main method
5) This can be avoided to allow the class to be
tested in a stand-alone mode.
6) Other methods can subsequently be called in
main(). 22
1.4 Basic Concept of OOP
1. Classes in Java
A class is a blue print from which
individual objects are created.
A class can have any
number of
methods to access
the value of
various kinds of
methods.
In the given example,
barking(),
hungry() and
sleeping() are A sample of a 23
Cont’d…
A class can contain any of the following variable types.
Local variables:
Variables defined inside methods, constructors or blocks are
called local variables.
The variable will be declared and initialized within the
method and the variable will be destroyed when the
method has completed.
Instance variables:
Instance variables are variables within a class but outside
any method.
These variables are initialized when the class is instantiated.
Instance variables can be accessed from inside any method,
constructor or blocks of that particular class.
Class variables:
Class variables are variables declared with in a class,
outside any method, with the static keyword.
24
Cont’d…
• When a number of objects are created from the same class
blueprint, they each have their own distinct copies of
instance variables.
• In the case of a Bicycle class, the instance variables are
cadence, gear, and speed.
• Each Bicycle object has its own values for these variables,
stored in different memory locations.
• Sometimes, you want to have variables that are common to all
objects. This is accomplished with the static modifier.
25
Cont’d…
• Fields that have the static modifier in their declaration are
called static fields or class variables.
• They are associated with the class, rather than with any
object.
• Every instance of the class shares a class variable, which is
in one fixed location in memory.
• Any object can change the value of a class variable, but
class variables can also be manipulated without creating an
instance of the class.
26
Cont’d…
• For example, suppose you want to create a number
of Bicycle objects and assign each a serial number,
beginning with 1 for the first object.
• This ID number is unique to each object and is
therefore an instance variable.
• At the same time, you need a field to keep track of
how many Bicycle objects have been created so
that you know what ID to assign to the next one.
• Such a field is not related to any individual object,
but to the class as a whole.
• For this you need a class variable,
numberOfBicycles, as follows: 27
Cont’d…
33
Cont’d…
• The first data column indicates whether the class
itself has access to the member defined by the
access level.
• As you can see, a class always has access to its
own members.
• The second column indicates whether classes in
the same package as the class (regardless of their
parentage) have access to the member.
• The third column indicates whether subclasses of
the class declared outside this package have
access to the member.
• The fourth column indicates whether all classes
have access to the member.
34
1.4.5 Method in Java
A method is a set of code which is referred to by name and can be called
(invoked) at any point in a program simply by utilizing the method’s
name.
1) Method Overloading
2) Method Overriding
Child class has the same method as of base class. In such cases child
class overrides the parent class method without even touching the
source code of the base class.
35
1.4.6 Encapsulation, Inheritance and
Polymorphism in Java
Encapsulation
• Encapsulation is a process of wrapping code and
data together into a single unit, for example
capsule i.e. mixed of several medicines.
• We can create a fully encapsulated class in java
by making all the data members of the class
private.
• Now we can use setter and getter methods to set
and get the data in it.
• The Java Bean class is the example of fully
encapsulated class.
36
Advantage of Encapsulation
• By providing only setter or getter method,
you can make the class read-only or
write- only.
• It provides you the control over the data.
Suppose you want to set the value of id
i.e. greater than 100 only, you can write the
logic inside the setter method.
37
Inheritance
• Inheritance can be defined as the process where one
Single
40
Cont’d…
Multilevel Inheritance
• Multilevel inheritance refers
to a
mechanism in OO
where one technology can
inherit
derived class, thereby fromthis
making a
derived class the base class for the
new class.
• In such kind of inheritance one
class is inherited by many sub
classes. Multilevel
41
Multiple Inheritance
• It refers to the concept of one
class extending (Or inherits)
more than one base class.
• The inheritance we learnt earlier
had the concept of one base class
or parent.
• The problem with “multiple
inheritance” is that the derived
class will have to manage the
dependency on two base classes.
• When a class extends
multiple classes i.e. known as Multiple
multiple inheritance.
42
Hybrid Inheritance
• In simple terms you can say that
Hybrid inheritance is a combination
of Single and Multiple inheritance.
• A typical flow diagram would
look like below.
• A hybrid inheritance can be achieved
in the java in a same way as
multiple inheritance can be using
interfaces.
• By using interfaces you can have
multiple as well as hybrid
inheritance in Java. Hybrid
43
Hierarchical Inheritance
• In such kind of A
inheritance one class is
inherited by many sub
classes.
• A is parent class D
B C
(or base class) of B,C
Hierarchical Inheritance
&D
44
Polymorphism
• Polymorphism is the ability of an object to take on many
forms. The most common use of polymorphism in OOP occurs
when a parent class reference is used to refer to a child
class object.
• It is important to know that the only possible way to access an
object is through a reference variable. A reference variable
can be of only one type. Once declared, the type of a
reference variable cannot be changed.
• The reference variable can be reassigned to other objects
provided that it is not declared final. The type of the
reference variable would determine the methods that it
can invoke on the object.
• A reference variable can refer to any object of its declared type
or any subtype of its declared type. A reference variable can
be declared as a class or interface type. 45
Cont’d…
• Polymorphism is the capability of a method to do
different things based on the object that it is acting upon.
• In other words, polymorphism allows you define
one
interface and have multiple implementations.
It is a feature that allows one interface to be used for
a general class of actions.
An operation may exhibit different behavior in different
instances.
The behavior depends on the types of data used
in the
operation.
It plays an important role in allowing objects having
different internal structures to share the same
external interface.