1-Lecture-one new
1-Lecture-one new
Introduction to Object-
Oriented Programming
Paradigm
1
CONTENTS
Overview of OOP?
Why Java?
The JVM and Byte Code
Basic concepts of OOP
classes
objects
members
class member visibility
encapsulation, inheritance and polymorphism 2
INTRODUCTION
Procedural programming- is a programming paradigm that focuses
on procedures or functions. Ex. Fortran, COBOL, Pascal, C
In procedural programming, the program is composed of a series of
procedures or functions that are executed in a specific order to
accomplish a specific task
Object-oriented programming (OOP)- is a programming paradigm
that focuses on objects, which are instances of classes.
In OOP, the program is composed of objects that interact with each
other to accomplish a specific task
Object oriented programming languages: Java, C++, C-sharp (C#)
3
POP VS OOP: CONT’D
5
OBJECT ORIENTED PROGRAMMING CHARACTERISTICS
(FEATURES)
Object
Any entity that has state and behaviour is known as an object. For
example, a chair, pen, table, keyboard, bike, etc. It can be physical or
logical(conceptual).
These objects always correspond to things found in the real world, i.e., real
entities. So, they are also called run-time entities of the world. These are
self–contained which consists of methods and properties which make data
useful.
It contains addresses and takes up some space in memory.
They are called instances of a class which are created from a class
Example: The objects of a class called Animals, for example, will be a cat,6
dog, elephant, and so on. Each object has its own identity, attribute, and
behavior.
CONT’D
Class
The class is one of the Basic concepts of OOPs which is a group of similar
entities. It is only a logical component and not the physical entity.
Collection of objects of similar type is called class. It is a logical entity.
Is a blueprint from which you can create an individual object. Class
doesn't consume any space.
Classes have members which can be fields, methods and constructors.
Example, if you had a class called “Expensive Cars” it could have objects
like Mercedes, BMW, Toyota, etc. Its properties(data) can be price or
speed of these cars. While the methods that may be performed with
7
these cars are driving, reverrse braking etc.
CLASS VS OBJECTS: CONT’D
Class:
Object:
A collection of objects of similar types
basic units of object oriented
programming Base-structures/blue prints/templates
A collection of data and method from which objects are created
A runtime entity which has data and Set of objects that share common
methods structure and behavior
An instance of a class Examples
Attributes (properties): Describe the Object: mobile
state /data of an object at a certain time
attributes: Model type, Size, Screen
Method: Are used to implementing
color
behavior. Encapsulated the behavior of the
object and provide interface to the object, Method: make-call (), Receive -
8
and hide any of internal substructures and call ()
states maintained by the object
CONT’D
Data- abstraction: the act of representing essential features without including the background
details or explanations classes use the concept of abstraction and are defined as a list of
abstract attributes such as size, weight and functions to operate on these attributes. They
encapsulate all the essential properties of the objects that are to be created
Encapsulation (data hiding): The wrapping up of data and functions into a single unit(class).
The insulation of data from direct access is done using access modifiers like protected and
privates. Encapsulation means hiding the internal details or mechanics of how an object does
something for security reasons. The data is not accessible to the outside world, and only those
functions that are wrapped in the class can access it. These functions provide the interface
between the object’s data and the program.
Inheritance: The process by which objects of one class acquire the properties of objects of
another class. An object-oriented system organizes classes into a sub-class, super-class
hierarchy. Subclass inherits all of the properties (non-private attributes) and methods defined in
its superclass. Inheritance provides the idea of reusability (allowing objects to be built from other
objects). 9
Polymorphism: Using functions in different ways depending on what they are operating on
(function overloading) is polymorphism, i.e. one thing with several distinct forms. For example, a
CONT’S
Dynamic binding: the process of determining dynamically which
function is to be involved (i.e. at runtime). Making this determination
at compile time is called static binding. Overloaded methods are
resolved at compile time based on the arguments supplied.
Overridden methods are resolved at run time
Message passing: The process of developing an object-oriented
program consists of
1. Create a class (classes)
2. Create an object
3. Establish communication among objects
Hence communication among objects in held by the help of message
10
11
BENEFITS OF OOP…
12
WHAT IS JAVA
1. Java is object-oriented
2. Java works on most platforms
3. Java is network enabled
4. Java is multithreaded: You can write programs in which several
sections run simultaneously in different execution threads.
5. Java can be used for web applications development
15
A FIRST JAVA PROGRAM
// Welcome.java
public class Welcome
{
public static void main(String args[])
{
System.out.println(“welcome to java programming
language”);
}
} 16
A FIRST JAVA PROGRAM…
Java class declarations normally contain one or more methods. For a Java application,
A FIRST JAVA PROGRAM…
static is a keyword. If we declare any method as static, it is known as the static method. The
core advantage of the static method is that there is no need to create an object to invoke the
static method. The main() method is executed by the JVM, so it doesn't require creating an
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 the starting point of the program
String args[] stores Java command-line arguments and is an array of
type java.lang.String class. Here, the name of the String array is args but it is not fixed and
the user can use any name in place of it.
System.out.println( "Welcome to Java Programming!" );
Instructs the computer to perform an action to print the string of characters contained
between the double quotations.
System.out: is known as the standard output object that allows Java applications to display
18
sets of characters in the command window from which the Java application executes.
Print: is a function which prints the string inside parenthesis to the standard output. Println
EDITING, COMPILING AND INTERPRETING OF JAVA PROGRAM
Java Compilers
Java is a high level language like C, C++ and Visual Basic, but Java compilers
differ from most other compilers in that they produce compiled binary
instructions not for the target computer hardware but for an abstract
computer platform called the Java Virtual Machine.
This Java machine does not actually exist in hardware but it simulates a real
computer hardware.
The binary code a compiler generates for languages such as C, C++ will vary for
Intel computers, Macintoshes and Solaris-based computers, whereas the binary
codes produced by the Java compiler are the same on all 3 platforms
19
JAVA PROGRAM FILES
All Java source files have the .java extension. For example.
Program1.java
Compiling Java Programs:
You have created a java program called Hello.java
To compile it, you run the JDK supplied utility called Javac
C:\path to javac\javac Hello.java
20
INTERPRETING JAVA PROGRAMS
24
TYPES OF PROGRAMMING ERRORS IN JAVA PROGRAMMING
LANGUAGE
A. Syntax error
The Syntax of a programming language consists of the rules for the correct use of the
language. These involve the correct grammatical construction and arrangement of the
language, correct spelling, hyphenation, inflection and so on. The syntax of a programming
language has to be strictly adhered to.
Character set: The character set of a language is simply that set of symbols from which
programming languages are composed. Most current programming language designs conform in
their character sets to ASCII or EBCDIC.
Vocabulary: The vocabulary of a programming language is a set of characters and words from
which programs are constructed.
For example:
The required word "class" has been changed to "Class" with a capital "C". This is called a syntax
25
error.
Note: The compiler will not create a new bytecode file because it stops compiling when it gets to an
CONT’D
B. Bugs or semantic error
The semantics of a programming language deal with the meanings given to
syntactically correct constructs of the language. Usually, semantics is defined in
terms of the program’s run-time behavior:
What happens when the program is executed with a certain set of inputs, what
statements are executed, what values are assigned to the variables, and
what output is produced.
Thus syntax has nothing to do with “meaning” or run-time behavior of a program.
A program could be syntactically correct yet meaningless.
The program below (code fragment) is syntactically correct but does not have any
meaning at runtime (never terminates).
For example:
sum=0; 26
while (sum!=-1)