0% found this document useful (0 votes)
30 views26 pages

1-Lecture-one new

Uploaded by

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

1-Lecture-one new

Uploaded by

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

CHAPTER 1

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

 Procedure oriented programming(POP):  Object-oriented programming


 Emphasis is on doing things (algorithms)  Emphasis is on data rather than procedure
 Large programs are divided into smaller programs  Programs are divided into what are known as
known as functions objects
 Most functions share global data  Data structures are designed such that they
 characterize the object
Data move openly around the system from
function to function  Functions that operate on the data of an object
 Functions transform data from one form to are tied together in the data structure
another  Data is hidden and can’t be accessed by external
 Employs a top-down approach in program design functions.
 Objects can communicate with each other
through functions
 New data and functions can be easily added
whenever necessary
4
 Follows bottom-up approach in program design
EXAMPLES OF OOP AND POP: 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

passing. Objects perform operations in response to the message.


BENEFITS OF OOP
 The main advantages of oop are:

11
BENEFITS OF OOP…

12
WHAT IS JAVA

 Java is a general-purpose programming language


 Any program that can be written in any programming language could be written in
java
 Java is a simple language
 Java is an object-oriented programming language
 Java is not Javascript
 Java is not a tiny language; it comes with an enormous library of classes that you
can include as a part of your program.
 Reading Assignment
 History of Java 13
JAVA CLASS LIBRARIES

 Java programs consist of pieces called classes.


 Classes include pieces called methods that perform tasks and return information
when they complete them.
 Programmers can create each piece they need to form Java programs.
 However, most Java programmers take advantage of the rich collections of existing
classes in the Java class libraries, which are also known as the Java APIs (Application
Programming Interfaces).
 Thus, there are really two aspects to learning the Java “world.” The first is the Java
language itself, so that you can program your own classes, and the second is the
classes in the extensive Java class libraries.
 Therefore, java is both a language and a platform
14
WHY 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…

 public class Welcome


 Begins a class declaration for class Welcome.
 Every program in Java consists of at least one class declaration that is defined by the
programmer
 class keyword is used to declare a class in Java.
 public keyword is an access modifier that represents visibility. It means it is visible to
all.
 By convention, all class names in Java begin with a capital letter and capitalize the
first letter of each word they include (e.g., SampleClassName).
 public static void main( String args[] )
 Is the starting point of every Java application. The parentheses after the identifier
main indicate that it is a program building block called a method. 17

 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

 If the compilation was successful, a file called Hello.class will be


produced

20
INTERPRETING JAVA PROGRAMS

(A). Some other programming languages


(B). Java programming languages
21
WHAT IS JAVA BYTECODE
 A special machine language that can be understood by the Java
Virtual Machine (JVM)
 Independent of any particular computer hardware, so any computer
with a Java interpreter can execute the compiled Java program, no
matter what type of computer the program was compiled
on.
 Five steps to write java program:
a. Write it in Text Editor
b. Compiler creates byte codes
c. The “Class Loader” places the .Class file in memory
d. The “Bytecode verifier” makes sure the code adheres to java’s security22
rules.
CONT’D
 Advantages of platform independence :
 Only need to compile once and then it works on every platform
 Disadvantages:
 Software or drivers must be installed on the computers in order to run the programs.
 In general the java programming language follows the following Edit, Compile and Run
Cycle
1. Edit the program using Notepad.
2. Save the program to the disk
3. Compile the program with the javac command.
4. If there are syntax errors, go back to step 1.
5. Run the program with the java command.
23
6. If it does not run correctly, go back to step 1.
7. When it runs correctly, quit.
CONT’D

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)

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