You are on page 1of 46

Introduction to Object oriented

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:

 Though C++ and java supports Boolean data type,


C++ takes any nonzero value as true and zero
as false.
 True and false in java are predefined literals that are
values for a boolean expression.
 Java has replaced the destructor function with a
finalize() function.
 C++ supports exception handling that is similar to
java's. However, in C++ there is no requirement that a
thrown exception be caught.
5
Java Environment
• Java includes many development tools, classes and
methods
– Development tools are part of Java Development Kit (JDK)
and
– The classes and methods are part of Java Standard
Library (JSL), also known as Application
Programming Interface (API).
• JDK constitutes of tools like java compiler, java
interpreter and many.
• API includes hundreds of classes and methods
grouped into several packages according to
their functionality.
6
Java is architecture-neutral

JAVA Program Execution

7
WORA
(Write Once Run Anywhere)

8
Tools you will need:

• You need the following software:


– Linux 7.1 or Windows xp/7/8
operating system.
– Java JDK 8

– Microsoft Notepad or any other text


editor
9
1.2 Why Java is Important
• Two reasons :
– Trouble with C/C++ language is that they
are not portable and are not platform independent
languages.
– Emergence of World Wide Web, which
demanded portable programs

• Portability and security necessitated the invention


of Java 10
Main Features of
1. Java is aJAVA
platform independent language
• To understand the meaning of platform independent, we
must need to understand the meaning of platform first.
• A platform is a pre-existing environment in which a
program runs, obeying its constraints, and making use of
its facilities.
• During compilation, the compiler converts java program
to its byte code. This byte code can run on any platform
such as Windows, Linux, Mac/OS etc. Which means a
program that is compiled on windows can run on Linux
and vice-versa.
• This is why java is known as platform independent
language.
11
Cont’d…
2. Java is an Object Oriented language
• Object oriented programming is a way of
organizing programs as collection of
objects, each of which represents an instance
• of a class.
4 main of Object Oriented
concepts
programming are:
– Abstraction
– Encapsulation
– Inheritance
– Polymorphism
12
Cont’d…
3. Simple
• Java is considered as one of simple language
because it does not have complex features
like Operator overloading, Multiple
inheritance, pointers and Explicit memory
allocation.

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.

1)Memory management mistakes can


be overcome by garbage collection.
Garbage collection is automatic de-allocation
of objects which are no longer needed.

2)Mishandled runtime errors are resolved


by Exception Handling procedures.
14
Cont’d…
5. Secure
• It provides a virtual firewall between the
application and the computer.
• Java codes are confined within Java Runtime
Environment (JRE) thus it does not
grant unauthorized access on the system
resources.
15
Cont’d…
6. Java is distributed
• Using java programming language we can create
distributed applications.
• RMI(Remote Method Invocation) and
EJB(Enterprise Java Beans) are used for creating
distributed applications in java.
• In simple words: The java programs can
be distributed on more than one systems that
are connected to each other using internet
connection.
• Objects on one JVM (java virtual machine) can
16
execute procedures on a remote JVM.
Cont’d…
7. Multithreading
• Java supports It enables a
multithreading.
program to perform several tasks
simultaneously.
8. Portable
• As discussed above, java code that is written on
one machine can run on another machine.
• The platform independent byte code can be
carried to any platform for execution that
makes java code portable.
17
1.3 JVM (Java virtual Machine)

1) Class loader accepts class files


2) Compilation creates class files
3) The interim memory is required
during execution
4) It consists of heaps, stacks and
registers to store data
5) JRE has native methods and
libraries
6) JVM runs two main
threads:
a) demon
b) Non-demon threads 18
1.3.1 JVM Threads
Demon Threads
• It has been Run by JVM for itself. Used for
garbage collection. JVM decides on a
thread for being a demon thread
Non-demon threads
• main() is the initial and non-demon thread.
Other implemented threads are also
non- demon threads.
• The JVM is active till any non-demon thread is
active.
19
1.3.2 Execution on JVM
1) JVM executes Java byte codes.
2) Other programming language codes if converted
to adequate Java byte code can be executed on JVM
3) JVM is different for different platforms and can also act
as a platform itself.
4) JVM supports automatic error handling by
intercepting the errors which can be controlled
5) This feature is useful in platform independency and
multi user ability of Java.
20
1.3.3 Compilation
1) Thecompiler requires to know the TYPE of
every CLASS used in the program source code
2) This is done by setting a default user
environment variable CLASSPATH
3) TheJavac (Java Compiler) reads the program
and converts it into byte code files called as class
files

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…

Class variables are referenced by the class name itself, as in


Bicycle.numberOfBicycles This makes it clear that they are class variables.
28
1.4.2 Objects in Java
what are objects?
• If we consider the real-world we can find many objects
around us, Cars, Dogs, Humans, etc. All these objects
have a state and behavior.
• If we consider a dog, then its state is - name, breed,
color, and the behavior is - barking, wagging, running
• If you compare the software object with a real world
object, they have very similar characteristics.
• Software objects also have a state and behavior. A
software object's state is stored in fields and behavior is
shown via methods.
• In software development, methods operate on the
internal state of an object and the object-to-object
communication is done via methods.
29
1.4.3 Members
Member Variable
• Member is an interface that reflects identifying
information about a single member (a field or a
method) or a constructor.
• In object-oriented programming, a member variable
(sometimes called a member field) is a variable that is
associated with a specific object, and accessible for all
its methods (member functions).
• In class-based languages, these are distinguished into
two types: if there is only one copy of the variable
shared with all instances of the class, it is called a class
variable or static member variable; while if each
instance of the class has its own copy of the variable,
the variable is called an instance variable.
30
1.4.4 Classes
Controlling Access to Members of a Class
• Access level modifiers determine whether other classes can
use a particular field or invoke a particular method.
There are two levels of access control:
– At the top level—public, or package-private (no explicit
modifier).
– At the member level—public, private,
protected, or
package-private (no explicit modifier).
• A class may be declared with the modifier public, in which
case that class is visible to all classes everywhere.
• If a class has no modifier (the default, also known as
package-private), it is visible only within its own package
31
(packages are named groups of related classes
Cont’d…
• At the member level, you can also use the public
modifier or no modifier (package-private) just
as with top-level classes, and with the same
meaning.
• For members, there are two additional access
modifiers: private and protected.
• The private modifier specifies that the member can
only be accessed in its own class.
• The protected modifier specifies that the member can
only be accessed within its own package (as
with package-private) and, in addition, by a
subclass of its class in another package.
32
Access Levels

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

In Java, it is possible to define two or more methods of same name in a


class, provided that there argument list or parameters are different. This
concept is known as 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

class acquires the properties (methods and fields) of


another.
• With the use of inheritance the information is made
manageable in a hierarchical order.

• The class which inherits the properties of other is


known as subclass (derived class, child class) and
the class whose properties are inherited is known as
superclass (base class, parent class).
38
Types of inheritance in java
• On the basis of class, there can be three types
of inheritance in java: single, multilevel and
hierarchical.
• In java programming, multiple and hybrid
inheritance is supported through interface
only.
Note: Multiple inheritance is not supported in java
through class.
39
Single inheritance
– It is damn easy to understand.

– When a class extends another one class only


then wecall it a single inheritance.

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.

You might also like