SlideShare a Scribd company logo
Introduction to Object oriented
programming
2
Chapter 1
Introduction to Object-Oriented
Programming
Contents
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
3
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.
4
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.
5
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.
6
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.
Java is architecture-neutral
JAVAProgram Execution
7
WORA
(Write Once Run Anywhere)
8
9
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
10
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
11
Main Features of JAVA
1. Java is a 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.
12
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 concepts
programming are:
– Abstraction
– Encapsulation
– Inheritance
– Polymorphism
of Object Oriented
13
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.
Cont’d…
14
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.
Cont’d…
15
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.
Cont’d…
16
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.
Cont’d…
17
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.
Cont’d…
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
1.3 JVM (Java virtual Machine)
18
19
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.
20
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.
1.3.3 Compilation
1) The compiler 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) The Javac (Java Compiler) reads the program and
converts it into byte code files called as class files
21
22
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().
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
1.4 Basic Concept of OOP
24
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.
25
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.
26
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.
27
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:
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
29
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.
30
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.
31
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
(packages are named groups of related classes
32
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.
Access Levels
33
34
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.
35
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.
1.4.5 Method in Java
36
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.
37
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.
38
• 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).
Inheritance
39
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.
Single inheritance
– It is damn easy to understand.
– When a class extends another one class only then
we call it a single inheritance.
Single
40
Cont’d…
Multilevel Inheritance
• Multilevel inheritance refers to a
mechanism
where one
in OO technology
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 inheritance.
Multiple
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
inheritance one class is
inherited by many sub
classes.
• A is parent class (or
base class) of B,C & D
A
B C D
Hierarchical Inheritance
44
45
• 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.
Polymorphism
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

More Related Content

Similar to chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx

PPS Java Overview Unit I.ppt
PPS Java Overview Unit I.pptPPS Java Overview Unit I.ppt
PPS Java Overview Unit I.ppt
CDSukte
 

Similar to chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx (20)

Elements of Java Language
Elements of Java Language Elements of Java Language
Elements of Java Language
 
Java
JavaJava
Java
 
PPS Java Overview Unit I.ppt
PPS Java Overview Unit I.pptPPS Java Overview Unit I.ppt
PPS Java Overview Unit I.ppt
 
PPS Java Overview Unit I.ppt
PPS Java Overview Unit I.pptPPS Java Overview Unit I.ppt
PPS Java Overview Unit I.ppt
 
1 Module 1 Introduction.pptx
1 Module 1 Introduction.pptx1 Module 1 Introduction.pptx
1 Module 1 Introduction.pptx
 
Java Introduction
Java IntroductionJava Introduction
Java Introduction
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java Language
 
Java questions and answers jan bask.net
Java questions and answers jan bask.netJava questions and answers jan bask.net
Java questions and answers jan bask.net
 
Comp102 lec 3
Comp102   lec 3Comp102   lec 3
Comp102 lec 3
 
CS8392 OOP
CS8392 OOPCS8392 OOP
CS8392 OOP
 
oop unit1.pptx
oop unit1.pptxoop unit1.pptx
oop unit1.pptx
 
Java introduction
Java introductionJava introduction
Java introduction
 
Top 10 Important Core Java Interview questions and answers.pdf
Top 10 Important Core Java Interview questions and answers.pdfTop 10 Important Core Java Interview questions and answers.pdf
Top 10 Important Core Java Interview questions and answers.pdf
 
JAVA PROGRAMMING-Unit I - Final PPT.pptx
JAVA PROGRAMMING-Unit I - Final PPT.pptxJAVA PROGRAMMING-Unit I - Final PPT.pptx
JAVA PROGRAMMING-Unit I - Final PPT.pptx
 
unit1.pptx
unit1.pptxunit1.pptx
unit1.pptx
 
1 java introduction
1 java introduction1 java introduction
1 java introduction
 
1 java intro
1 java intro1 java intro
1 java intro
 
Skillwise Elementary Java Programming
Skillwise Elementary Java ProgrammingSkillwise Elementary Java Programming
Skillwise Elementary Java Programming
 
basic core java up to operator
basic core java up to operatorbasic core java up to operator
basic core java up to operator
 
java slides
java slidesjava slides
java slides
 

More from FiromsaDine

Chapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptxChapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptx
FiromsaDine
 
Chapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptxChapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptx
FiromsaDine
 
Chapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptxChapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptx
FiromsaDine
 
Chapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptxChapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptx
FiromsaDine
 
chapter_2_Source_code_generation_from_UML_model_edited_version.pptx
chapter_2_Source_code_generation_from_UML_model_edited_version.pptxchapter_2_Source_code_generation_from_UML_model_edited_version.pptx
chapter_2_Source_code_generation_from_UML_model_edited_version.pptx
FiromsaDine
 

More from FiromsaDine (6)

Files in PHP.pptx g
Files in PHP.pptx                      gFiles in PHP.pptx                      g
Files in PHP.pptx g
 
Chapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptxChapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptx
 
Chapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptxChapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptx
 
Chapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptxChapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptx
 
Chapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptxChapter 1-software-engineering-tools-and-practices.pptx
Chapter 1-software-engineering-tools-and-practices.pptx
 
chapter_2_Source_code_generation_from_UML_model_edited_version.pptx
chapter_2_Source_code_generation_from_UML_model_edited_version.pptxchapter_2_Source_code_generation_from_UML_model_edited_version.pptx
chapter_2_Source_code_generation_from_UML_model_edited_version.pptx
 

Recently uploaded

The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
heathfieldcps1
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
ashishpaul799
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
17thcssbs2
 

Recently uploaded (20)

size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation[GDSC YCCE] Build with AI Online Presentation
[GDSC YCCE] Build with AI Online Presentation
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
 
IATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdffIATP How-to Foreign Travel May 2024.pdff
IATP How-to Foreign Travel May 2024.pdff
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 

chapter_one_Introduction_to_Object_Oriented_Programming_OOP.pptx

  • 1. Introduction to Object oriented programming
  • 2. 2 Chapter 1 Introduction to Object-Oriented Programming Contents 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
  • 3. 3 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.
  • 4. 4 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.
  • 5. 5 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.
  • 6. 6 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.
  • 8. WORA (Write Once Run Anywhere) 8
  • 9. 9 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
  • 10. 10 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
  • 11. 11 Main Features of JAVA 1. Java is a 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.
  • 12. 12 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 concepts programming are: – Abstraction – Encapsulation – Inheritance – Polymorphism of Object Oriented
  • 13. 13 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. Cont’d…
  • 14. 14 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. Cont’d…
  • 15. 15 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. Cont’d…
  • 16. 16 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. Cont’d…
  • 17. 17 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. Cont’d…
  • 18. 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 1.3 JVM (Java virtual Machine) 18
  • 19. 19 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.
  • 20. 20 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.
  • 21. 1.3.3 Compilation 1) The compiler 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) The Javac (Java Compiler) reads the program and converts it into byte code files called as class files 21
  • 22. 22 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().
  • 23. 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 1.4 Basic Concept of OOP
  • 24. 24 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.
  • 25. 25 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.
  • 26. 26 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.
  • 27. 27 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:
  • 28. 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
  • 29. 29 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.
  • 30. 30 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.
  • 31. 31 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 (packages are named groups of related classes
  • 32. 32 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.
  • 34. 34 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.
  • 35. 35 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. 1.4.5 Method in Java
  • 36. 36 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.
  • 37. 37 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.
  • 38. 38 • 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). Inheritance
  • 39. 39 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.
  • 40. Single inheritance – It is damn easy to understand. – When a class extends another one class only then we call it a single inheritance. Single 40
  • 41. Cont’d… Multilevel Inheritance • Multilevel inheritance refers to a mechanism where one in OO technology 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
  • 42. 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 inheritance. Multiple 42
  • 43. 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
  • 44. Hierarchical Inheritance • In such kind of inheritance one class is inherited by many sub classes. • A is parent class (or base class) of B,C & D A B C D Hierarchical Inheritance 44
  • 45. 45 • 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. Polymorphism
  • 46. 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