0% found this document useful (0 votes)
40 views60 pages

Vips Oops Unit 1

Uploaded by

Pranay Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views60 pages

Vips Oops Unit 1

Uploaded by

Pranay Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Course :

Paper Code:
Faculty : Dr. Shivanka
Assistant Professor
VIPS
Ø Introduction to Object Oriented Programming
Ø Benefits of Object Oriented Programming
Ø Java Features
Ø How Java Differs from other OO languages
Ø Java Environment
Ø Execution Process organization of the Java virtual Machine Model of Java
Ø Build your first Java Program
Ø General Structure of Java program
Ø Classes and objects
Ø Inheritance
Ø Data Encapsulation and Abstraction:
Object-oriented programming (OOP) is a programming
paradigm using "objects" – data structures consisting of
data fields and methods together with their interactions
– to design applications and computer programs.
Programming techniques may include features such as
data abstraction, encapsulation, messaging, modularity,
pol ym orphi sm , a nd i nhe ri t a nc e . M a n y m o d e rn
programming languages now support OOP.
Benefits of Object Oriented Programming

•Ability to reflect the real world


•Easy to Maintain the code
•Information Hiding
•Reusability of Code
•Flexibility of Code
•Security
•Troubleshooting is easy
Ability to reflect the real world

❖ Every Entity in the real world can be expressed as an object in OOP.


❖ An Object is an entity with has state and behavior.
❖ Eg. Dog is a real world entity
• It has state: breed, color etc.,
• behavior: barks, eats etc.,
• It can be expressed as Object, by defining state and behavior in the
class.
Easy to Maintain the code
• Object Oriented Programming relies on classes, creation and
communication among objects.
• It is easy to maintain the code.
• Part of the code can be modified without affect the entire program.
• Eg. Overriding, inheritance etc.,
Information Hiding

• Variables,Methods are bundled in


class and accessed byobjects.
• Variables of a class are used by
methods of that class only.
• Data Abstraction provide the concept
of hiding implementation details and
workingonly with the
requiredmethods.Eg. Though users
do not know the internal mechanism
ofTV-Remote,they operate them by
pressing buttons of the remote.
Reusability of Code

•The existing classes properties can be acquired by subclasses through


the concept of inheritance.
•Reusability of Code adds essence to the Object Oriented Paradigm.
•Several types of Inheritances like single level, multilevel , hierarchical
etc., help in reusability of code
Flexibility of the code

It is easy to modify the a part of the code without affecting the entire
program. It is easy to reuse the existing code. It is easy to extend the
code for performing new operations.
Security

• Object Oriented Programming provides security.


• The concept of Encapsulation bundles variables and methods into a
component.
• The code can be protected from further modification, enhancement if
required.
For example: final keyword is used for not letting the class to be used
for inheritance.
Troubleshooting is easy

• Object Oriented Programming follows modular division and bottom


up programming approach.
• Hence, makes troubleshooting easy.
Characteristics of Java

vJava Is Simple
vJava Is Object-Oriented
vJava Is Distributed
vJava Is Interpreted
vJava Is Robust
vJava Is Secure
vJava Is Architecture-Neutral
vJava Is Portable
vJava's Performance
vJava Is Multithreaded
vJava Is Dynamic
Characteristics of Java
vJava Is Simple
vJava Is Object-Oriented
vJava Is Distributed
vJava Is Interpreted
vJava Is Robust
vJava Is Secure
vJava Is Architecture-Neutral
vJava Is Portable
vJava's Performance
vJava Is Multithreaded
vJava Is Dynamic

14
Characteristics of Java
vJava Is Simple Java is inherently object-oriented. Object-oriented
vJava Is Object-Oriented programming (OOP) is a popular programming
approach that is replacing traditional procedural
vJava Is Distributed programming techniques.
vJava Is Interpreted
One of the central issues in software development
vJava Is Robust is how to reuse code. Object-oriented
vJava Is Secure p r o g r a m m i n g p r o v i d e s g r e a t f l e x i b i l i t y,
modularity, clarity, and reusability through
vJava Is Architecture-Neutral encapsulation, inheritance, and polymorphism.
vJava Is Portable • focus on the data (objects) and methods
manipulating the data
vJava's Performance • all functions are associated with objects
vJava Is Multithreaded • almost all datatypes are objects (files, strings,
vJava Is Dynamic etc.)
• potentially better code organization and reuse

15
Characteristics of Java
vJava Is Simple Distributed computing involves several
computers working together on a network.
vJava Is Object-Oriented Java is designed to make distributed
vJava Is Distributed c o m p u t i n g e a s y. S i n c e n e t w o r k i n g
capability is inherently integrated into
vJava Is Interpreted Java, writing network programs is like
vJava Is Robust sending and receiving data to and from a
vJava Is Secure file.

vJava Is Architecture-Neutral
vJava Is Portable
vJava's Performance
vJava Is Multithreaded
vJava Is Dynamic

16
Characteristics of Java
vJava Is Simple
vJava Is Object-Oriented You need an interpreter to run Java
vJava Is Distributed programs. The programs are compiled into
vJava Is Interpreted the Java Virtual Machine code called
bytecode. The bytecode is machine-
vJava Is Robust independent and can run on any machine
vJava Is Secure that has a Java interpreter, which is part of
vJava Is Architecture-Neutral the Java Virtual Machine (JVM).
vJava Is Portable
vJava's Performance
vJava Is Multithreaded
vJava Is Dynamic

17
Characteristics of Java
vJava Is Simple Java compilers can detect many problems
vJava Is Object-Oriented that would first show up at execution time
in other languages. It has extensive
vJava Is Distributed compile-time and runtime error checking
vJava Is Interpreted • It has eliminated certain types of error-
vJava Is Robust prone programming constructs found
in other languages. There are no
vJava Is Secure pointers. Memory corruptions or
vJava Is Architecture-Neutral unauthorized memory accesses are
impossible
vJava Is Portable • Java has a runtime exception-handling
vJava's Performance feature to provide programming
vJava Is Multithreaded support for robustness.
• It has automatic garbage collection
vJava Is Dynamic tracks objects usage over time

18
Characteristics of Java
vJava Is Simple
vJava Is Object-Oriented
vJava Is Distributed
Java implements several security mechanisms
vJava Is Interpreted to protect your system against harm caused by
vJava Is Robust stray programs.
vJava Is Secure • usage in networked environments requires
more security
vJava Is Architecture-Neutral • memory allocation model is a major
vJava Is Portable defense
vJava's Performance • access restrictions are forced (private,
public)
vJava Is Multithreaded
vJava Is Dynamic

19
Characteristics of Java
vJava Is Simple
vJava Is Object-Oriented
vJava Is Distributed
vJava Is Interpreted
vJava Is Robust
vJava Is Secure
Write once, run anywhere
vJava Is Architecture-Neutral
vJava Is Portable With a Java Virtual Machine (JVM),
vJava's Performance you can write one program that will
run on any platform.
vJava Is Multithreaded
vJava Is Dynamic

20
Characteristics of Java
vJava Is Simple
vJava Is Object-Oriented
vJava Is Distributed
vJava Is Interpreted
vJava Is Robust
vJava Is Secure
vJava Is Architecture-Neutral
vJava Is Portable Because Java is architecture neutral,
vJava's Performance Java programs are portable. They can
be run on any platform without being
vJava Is Multithreaded recompiled.
vJava Is Dynamic

21
Characteristics of Java
vJava Is Simple
vJava Is Object-Oriented
vJava Is Distributed
vJava Is Interpreted
vJava Is Robust
vJava Is Secure
vJava Is Architecture-Neutral
vJava Is Portable Java’s performance is very high
vJava's Performance because of its architecture neutral and
vJava Is Multithreaded portable features.
vJava Is Dynamic

22
Characteristics of Java
vJava Is Simple
vJava Is Object-Oriented
vJava Is Distributed
vJava Is Interpreted
vJava Is Robust
vJava Is Secure
vJava Is Architecture-Neutral
vJava Is Portable
Multithread programming is smoothly
vJava's Performance integrated in Java, whereas in other
vJava Is Multithreaded languages you have to call procedures
specific to the operating system to enable
vJava Is Dynamic multithreading.

23
Characteristics of Java
vJava Is Simple
vJava Is Object-Oriented
vJava Is Distributed
vJava Is Interpreted
vJava Is Robust
vJava Is Secure
vJava Is Architecture-Neutral
vJava Is Portable
Java was designed to adapt to an evolving
vJava's Performance environment. New code can be loaded on the
vJava Is Multithreaded fly without recompilation. There is no need for
developers to create, and for users to install,
vJava Is Dynamic major new software versions. New features can
be incorporated transparently as needed.

24
ØSlower than compiled language such as C
ü an experiment in 1999 showed that Java was 3 or 4 times slower than C or C++
ü title of the article: “Comparing Java vs. C/C++ Efficiency Issues to Interpersonal Issues” (Lutz
Prechelt)
ü Running bytecode through the interpreter is not as fast as running machine code, which is
specific to that platform.

ØBecause it is platform independent, it is difficult to use platform specific


features (e.g., Windows taskbar, quick launch) in Java.

ØJava interpreter must be installed on the computer in order to run Java


programs.
Garbage Collection in Java
• Since objects are dynamically allocated by using the new operator, you might
be wondering how such objects are destroyed and their memory released for
later reallocation. In some languages, such as C++, dynamically allocated
objects must be manually released by use of a delete operator.
• Java takes a different approach; it handles deallocation for you automatically.
The technique that accomplishes this is called garbage collection. It works like
this: when no references to an object exist, that object is assumed to be no
longer needed, and the memory occupied by the object can be reclaimed.
• There is no explicit need to destroy objects as in C++. Garbage collection only
occurs sporadically (if at all) during the execution of your program. It will not
occur simply because one or more objects exist that are no longer used.
Furthermore, different Java run-time implementations will take varying
approaches to garbage collection, but for the most part, you should not have to
think about it while writing your programs.
this Keyword
Sometimes a method will need to refer to the object that invoked it.
To allow this, Java defines the this keyword. this can be used inside any method to refer to the current object. That is,
this is always a reference to the object on which the method was invoked. You can use this anywhere a reference to an
object of the current class’ type is permitted.
To better understand what this refers to, consider the following version of Box( ):
// A redundant use of this.
Box(double w, double h, double d) {
this.width = w;
this.height = h;
this.depth = d;}
This version of Box( ) operates exactly like the earlier version. The use of this is redundant, but perfectly correct.
Inside Box( ), this will always refer to the invoking object. While it is redundant in this case, this is useful in other
contexts.
Programmer

Source Code Object Executable


Code Code
Text Editor Compiler linker
.c file .o/.obj a.out file
file
Notepad, gcc
emacs,vi
Java is Compiled and Interpreted
Hardware and
Programmer
Operating System

Source Code Byte Code


Text Editor Compiler Interpreter
.java file .class file
Notepad, javac java
emacs,vi, appletviewer
textpad, netscape
Java Interpreter
vJava is a little different.
vJava compiler produces bytecode not machine code.
vBytecode can be run on any computer with the Java interpreter
installed.
Win

eter
er pr
Java Program Java Bytecode Int
MAC
compiler Interpreter
Inte
r pret
er Unix
ØFor most languages, compilation produces machine code
ØJava compilation produces “bytecode”
üIntermediate code readable by the VM
üTransferable across the Internet as applets
ØVM interprets BC into instructions
üPartly responsible for performance lag
ØByteCode produced on any platform may be executed on
any other platform which supports a VM
(translator)

(same for all platforms)

(one for each different system)

Windows 95 Macintosh Solaris Windows NT


Write Once, Run Anywhere
source bytecode
(text) compiler (aka. class file)
JVML
dynamic
loading

verifier
virtual machine

JIT compiled
bytecode code
compiler
interpreter

CPU
ØJust-In-Time compiler
ØTranslates bytecode into machine code at runtime
ü1-time overhead when run initiated
üPerformance increase 10-30 times
ØNow the default for most JVM’s
üCan be turned off if desired
üJIT can apply statistical optimizations based on runtime usage
profile
ØJVM (J2EE & J2SE)
üWell-known Java Virtual Machine.
ØCVM , KVM (J2ME)
üSmall devices.
üReduces some VM features to fit resource-
constrained devices.
ØJCVM (Java Card)
üSmart cards.
üIt has least VM features.
and there are also lots of other JVMs
C++

C Java
In a first contact, Java seems like C++, and it's logical because Java takes the C and C++ syntax.
But Java has some important differences with C++. For instance, you can't use pointers in Java,
neither operators overload, neither multiple inheritance, neither predefined types. These features
of C++ that Java doesn't has, make it a simplest and more robust language. Interpreted Java is
furthermore slower than C++ (even 20 to 50 times slower than C in the original Java
interpreters).

Java memory administration is automatic; memory is assigned


automatically when you create an object, and also a garbage collector frees the memory when
that object is not used.
Functions malloc() and free() don't exist in Java.

: In Java, Primitive data types (like char, int, long...) have sizes and behaviors which
may be different in some platforms and operative systems. In Java language, unsigned data don't
exist. The boolean data has two values in Java : true and false. So it isn't an integer type, but you
can force "0" and "1" (which are integers) to be booleans.
The execution order of the operators in Java is same as in C.

The syntax of the following statements if, while, for and do is the
same as in C and C++. But there is an important difference : the proof expression
for each flux construction should return a boolean value (true or false). In C and
C++, the expression can return an integer.

In Java all method definitions have to have an specific number of


arguments. The arguments in the command line have a different behavior than in C
and C++. So, in these languages argv[0] is the name of the program but in Java this
is the first of additional arguments.
Ø No Typedefs, Defines, or Preprocessor
Ø No Global Variables
Ø No Goto statements
Ø No Pointers
Ø No Unsafe Structures
Ø No Multiple Inheritance
Ø No Operator Overloading
Ø No Automatic Coercions
Ø No Fragile Data Types
Feature C++ Objective C Ada Java
Encapsulation Yes Yes Yes Yes

Inheritance Yes Yes No Yes

Multiple Inherit. Yes Yes No No

Polymorphism Yes Yes Yes Yes

Binding (Early or Both Both Early Late


Late)
Concurrency Poor Poor Difficult Yes

Garbage Collection No Yes No Yes

Genericity Yes No Yes Limited

Class Libraries Yes Yes Limited Yes


üjavac - The Java Compiler
üjava - The Java Interpreter
üjdb- The Java Debugger
üappletviewer -Tool to run the applets
üjavap - to print the Java bytecodes
üjavaprof - Java profiler
üjavadoc - documentation generator
üjavah - creates C header files
Text Editor

Java Source
javadoc HTML Files
Code

javac

Java Class File javah Header Files

java jdb

Output
ØJava API and Virtual Machine insulate the Java program from hardware
dependencies.
ØJava API
• Collection of ready- • Core API
made software • Essentials: Object, String,
components that Input and Output...
provide many useful • Applets
capabilities. • Networking
• Internationalization
• Grouped into libraries • Security
(packages) of related • Software Components
components. • Object Serialization
• Java Database
Connectivity (JDBC)
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
ØCompile
üjavac HelloWorld.java
ØOne file named HelloWorld.class is created if the
compilation succeeds.
ØRun
üjava HelloWorld
• Since Java is object-oriented, programs are organized into modules called classes,
which may have data in variables and subroutines called methods.

class HelloWorld
{ public static void main (String[] args)
{ System.out.println(“Hello World!”);
}
}
•class keyword is used to declare a class in java.

•public keyword is an access modifier which represents visibility, it means it is


visible to all.

•static is a keyword, if we declare any method as static, it is known as static


method. The core advantage of static method is that there is no need to create object
to invoke the static method. The main method is executed by the JVM, so it doesn't
require to create object to invoke the main method. So it saves memory.

•void is the return type of the method, it means it doesn't return any value.

•main represents startup of the program.

•String[] args is used for command line argument.

•System.out.println() is used print statement.


In object-oriented programming, a class is a construct that is used as a blueprint to create instances
of the class (class instances, class objects, instance objects or just objects). A class defines
constituent members which enable class instances to have state and behavior. Data field members
(member variables or instance variables) enable a class object to maintain state. Other kinds of
members, especially methods, enable a class object's behavior. Class instances are of the type of the
associated class.

For example, an instance of the class "Fruit" (a "Fruit" object) would be of the type "Fruit". A class
usually represents a noun, such as a person, place or (possibly quite abstract) thing. Programming
languages that include classes as a programming construct subtly differ in their support for various
class-related features. Most support various forms of class inheritance. Many languages also support
advanced encapsulation control features, such as access specifiers.
Object is an run time entity.

Is an Instance of class

Represents a Place ,Person ,anything that have some


attributes.
There is a very important distinction between an object
and an instance of an object. An object is actually a
definition, or a template for instances of that object. An
instance of an object is an actual thing that can be
manipulated.
For instance, we could define a Person object, which may
include such member data as hair color, eye color, height,
weight, etc. An instance of this object could be "Dave"
and Dave has values for hair color, eye color, etc. This
allows for multiple instances of an object to be created.
Data encapsulation, sometimes referred to as data hiding.
Data Encapsulation and Data Abstraction is one of the most striking feature of object
oriented programming.

The wrapping up of data and code into a single unit is called data encapsulation. The
data is not accessible to the outside world only those functions which are wrapped
into a class can only access the private data of the class.

Contd…
Data Encapsulation and Abstraction:
• The concept of data encapsulation is supported in C++ through the use of the public,
protected and private keywords which are placed in the declaration of the class.

• Note :
v Anything in the class placed after the public keyword is accessible to all the users of
the class

v Elements placed after the protected keyword are accessible only to the methods of the
class or classes derived from that class

v Elements placed after the private keyword are accessible only to the methods of the
class.
Inheritance is one of the most striking feature of object
oriented programming.

Inheritance is the process by which one class can acquire the


properties of another class.

The new classes, known as subclasses (or derived classes),


inherit attributes and behavior of the pre-existing classes,
which are referred to as superclasses (or ancestor classes).
The inheritance relationships of classes gives rise to a
hierarchy
Contd…
A superclass, base class, or parent class is a class from
which other classes are derived. The classes that are derived
from a superclass are known as child classes, derived
classes, or subclasses.

In object-oriented programming (OOP), inheritance is a


way to compartmentalize and reuse code by creating
collections of attributes and behaviors called objects
which can be based on previously created objects.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy