Lecture 1 Intro To Java

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Object Oriented

Programming
Lecture 1

BSCS 2(Afternoon)
Spring 2024
sqayuum@numl.edu.pk
Topics to be covered according to
outline
Recommended Books

• Herbert Schildt: Java-The Complete Reference (latest editions)


• Deitel, H.M and Deitel, P.J.: Java: How to Program (latest editions)
Evaluation Criteria

Theory Marks
Midterm Examination 25
Final Examination 50
IE (Assignments / Quizzes) 25
total = 100
Revision

• Decision Making Statements.


• If- else
• Switch Case
• Loops
• For
• While
• Do-while
• Nested {{}}
Revision

• Variables
• String in C/C++ ?
• Arrays
• 1D
• 2D
• ND
Revision

• Functions / Methods
• Declaration
• Definition
• Calling
• Overloading
• Passing Arguments
• By Value
• By reference in java ?
Programming Paradigms
Programming Paradigms

• A programming paradigm is a fundamental approach or style of


programming that provides
• a set of principles,
• concepts, and
• techniques
for designing and implementing computer programs.
• It defines the structure, organization, and flow of the code, as well
as the methodologies for problem-solving and expressing
computations.
• Programming paradigms dictate how programmers should think
about and structure their code
Procedural Programming:

Procedural programming is a paradigm where the program is


structured around procedures or functions that manipulate data. It
focuses on step-by-step instructions and emphasizes code
reusability through the use of functions. C and Pascal are examples
of languages that follow this paradigm.
• Programming model which is derived from structured programming
• based upon the concept of calling procedure.
• Procedures, also known as routines, subroutines or functions,
• simply consist of a series of computational steps to be carried out.
• During a program’s execution, any given procedure might be
called at any point, including by other procedures or itself.
Languages used in Procedural
Programming
• BASIC
• PASCAL
•C
Object-Oriented Programming (OOP):

Object-Oriented Programming revolves around the concept of


objects, which are instances of classes. It organizes code into
objects that encapsulate data and behavior. OOP promotes
modularity, reusability, and allows for concepts such as inheritance,
polymorphism, and encapsulation. Java, C++, and Python are
popular languages that support OOP.
• Programming model which is based upon the concept of objects.
• Objects contain data in the form of attributes and code in the form of
methods.
• In object-oriented programming, computer programs are designed
using the concept of objects that interact with the real world
• Object-oriented programming languages are various but the most
popular ones are class-based.
• meaning that objects are instances of classes, which also determine their
types
Languages used in Object-Oriented
Programming
• Java, C++, C#, Python,
• PHP, JavaScript, Ruby, Perl,
• Objective-C, Dart, Swift, Scala
Procedural Programming vs Object-
Oriented Programming
Procedural Oriented Programming Object-Oriented Programming

In procedural programming, the program is divided into small In object-oriented programming, the program is divided into
parts called functions. small parts called objects.

Procedural programming follows a top-down approach. Object-oriented programming follows a bottom-up approach.

There is no access specifier in procedural programming. Object-oriented programming has access specifiers like
private, public, protected, etc.

Adding new data and functions is not easy. Adding new data and function is easy.

Procedural programming does not have any proper way of Object-oriented programming provides data hiding so it
hiding data so it is less secure. is more secure.

In procedural programming, overloading is not possible. Overloading is possible in object-oriented programming.


Procedural Programming vs Object-
Oriented Programming
In procedural programming, there is no concept of data In object-oriented programming, the concept of data
hiding and inheritance. hiding and inheritance is used.

In procedural programming, the function is more important In object-oriented programming, data is more important than
than the data. function.

Procedural programming is based on the unreal world. Object-oriented programming is based on the real world.

Procedural programming is used for designing medium-sized Object-oriented programming is used for designing large and
programs. complex programs.

Procedural programming uses the concept of procedure Object-oriented programming uses the concept of data
abstraction. abstraction.

Code reusability absent in procedural programming, Code reusability present in object-oriented programming.

Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc.
Advantages of OOP
Encapsulati It allows us to hide implementation details from clients. This makes it easy to
on change internal implementations without affecting the client’s behavior.

Inheritance Allows reuse of existing functionality through inheritance.

Polymorphi Provides flexibility at run time.


sm

Abstraction Makes complex problems look simple.

Modularity Separates concerns such as input/output, presentation, etc.

Code We can use the same set of codes across multiple projects or even different
reusability programming paradigms like procedural, functional, imperative, etc.

Testable Unit testing becomes much simpler because we don't have to test individual
components but rather the entire system.

Maintainabl Classes provide encapsulated states so they're more maintainable than traditional
e modules.

Scalable Objects allow you to add new features easily.


Advantages of OOP
Extendibility Extending builtins gives us power over how things work.

Open-source With open-source software, anyone can contribute changes back to


the community.

Cross-platform Because classes are independent units that contain data and
functions, they can be used anywhere.
Easy Debugging an application written in an object-oriented language is
debugging easier than one written in a nonobject-oriented language.
Easier learning Learning object-oriented concepts is less difficult compared to
curve procedural programming.

More readable Class definitions make programs easier to read.

Fewer bugs Bugs tend to occur when developers try to write too many lines of
code instead of writing them correctly.
Better When using objects, there is no need to allocate memory for each
performance variable separately. Instead, all variables share the same space.
Faster Writing code in an object-oriented style takes less effort than writing
development code in a procedural style.
cycle
Ease of The structure of object-oriented applications tends to remain stable
maintenance throughout.
Disadvantages of OOP
As mentioned earlier, creating an object requires defining its state and
behaviors. However, not every business process has these
Complexity characteristics. Some processes require only a few steps while others
involve dozens of steps. Creating an abstract class with hundreds of
methods would result in bloated code.

Lack of A single method call could potentially access several pieces of


modularization information. Therefore, it's important to separate logic into smaller
chunks.

Code bloat Since everything is contained within a single file, large files become
very hard to manage. This makes refactoring harder as well.

Inheriting behavior from another class means inheriting both the


Inheritance parent's properties and methods. It might cause unexpected results
problems since the child class doesn’t necessarily inherit all the attributes and
methods of the parent class.

Testing an object-oriented program involves verifying whether the


Difficulties in unit correct output was produced by calling the right number of methods.
testing Unit tests usually test individual components rather than entire
systems.

Difficulty in Object-oriented languages does not support multiple threads or tasks


parallelizing easily. They're designed around sequential execution.
JAVA
• Object oriented
• automatic garbage collector
• originally developed in 1991 by James Gosling at Sun Microsystems
• released in 1995 as a core component of Sun Microsystems' Java
platform
• Now merged into Oracle Corporation
• much of its syntax from C and C++, but it has fewer low-level
facilities than either of them.
Lexical issues in Java

• Lexemes or lexical issues are atomic elements of Java. Java


programs are a collection of

• whitespace
• identifiers
• comments
• literals
• operators
• separators
• keywords
Whitespaces
• Java is a free-form language. This means that you do not need to
follow any special indentation rules.
• For example
• the Example program could have been written all on oneline or in any
other strange way you felt like typing it, as long as there was at least
onewhitespace character between each token that was not already
delineated by an operatoror separator.
• In Java, whitespace is a space, tab, or newline.
Identifiers
• Identifiers are used for class names, method names, and variable
names.
• An identifier may be any descriptive sequence of uppercase and
lowercase letters, numbers, or the underscore and dollar-sign
characters.
• They must not begin with a number, lest they be confused with a
numeric literal
• Java is case-sensitive, so VALUE is a different identifier
than Value.
Identifiers
• Some examples of valid identifiers are:
• HighTemp
• temp
• h8
• $low
• this_is_ok
• Invalid variable names include:
• 2temp
• low-temp
• Yes/ok
Comments

• there are three types of comments defined by Java. You have


already
• seen two:
• 1) single-line( // )
• 2) multiline. //
//

OR
/* and ends with a */
Literals
• A constant value in Java is created by using a literal representation
of it. For example,
• here are some literals:

• 100 literal specifies an integer


• 98.6 literal specifies a floating-point value
• 'X' literal specifies is a character constant
• "This is a test" literal specifies is a string

• A literal can be used anywhere a value ofits type is allowed.


Literals
Separators
• There are a few characters that are used as separators.
• The most commonly used separator in Java is the semicolon
Escape Sequence

• used in Strings to give a particular character a escape


Keywords
abstract const Finally int Public this boolean continue
Float interface Return throw
break default For long Short throws byte do
Goto native Static transient
case double If new Strictfp try catch else Im
plements package Super
void char extends Import private Switch volatile cl
ass final Instanceof protected
Synchronized while
principles of Object Oriented Programming
are

• Data hiding
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism
Object Oriented Programming

Data hiding
Information Hiding is concealing some information
For example use of access modifiers in C++ (public,
private, protected etc.) hides some data.
Encapsulation
Encapsulation is encapsulating or grouping 1 or more
things (data and operations) together into a single
entity.
Don’t mistake this with information hiding, all the data
encapsulated need not be hidden.
Object Oriented Programming

Abstraction
Abstraction is a way of dealing with the information you
want and ignoring the rest (as good as non-existent for
you) from 1 or more items.
Inheritance
Inheritance is the process of creating new classes,
called derived classes, from existing or base classes.
Polymorphism
Different forms
OOP Concepts

• Classes
• Objects
• Instance
• Method
• Message Parsing
Questions are Welcome

You might also like