Chapter 1
Chapter 1
programming
Chapter 1
Introduction to Object-Oriented
Programming
Contents
1.1. Overview of OOP?
1.2. Why Java?
1.3. The JVM and Byte Code
1.4. Basic concepts of OOP
1.4.1. Classes
1.4.2. Objects
1.4.3. Members
1.4.4. Class member visibility
1.4.5. Methods
1.4.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:
– Java JDK 8
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.
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.
14
Cont’d…
5. Secure
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
execute procedures on a remote JVM.
16
Cont’d…
7. Multithreading
• Java supports multithreading. It enables a
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)
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.4.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
methods.
A sample of a class 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.
• Each Bicycle object has its own values for these variables,
stored in different memory locations.
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.
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
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.
Single
40
Cont’d…
Multilevel Inheritance
• Multilevel inheritance refers to a
mechanism in OO technology
where one can inherit from a
derived class, thereby making this
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.
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.
Polymorphism is extensively used in implementing
inheritance. 46