0% found this document useful (0 votes)
3 views48 pages

Chapter 1 Introduction - To - Object - Oriented

The document outlines a course on Object-Oriented Programming (OOP) covering fundamental concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with practical Java programming techniques. It includes a detailed course structure with chapters on advanced OOP concepts, exception handling, GUI development, and file handling. The document also lists assessment criteria and references for further reading.

Uploaded by

ahazinsights
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)
3 views48 pages

Chapter 1 Introduction - To - Object - Oriented

The document outlines a course on Object-Oriented Programming (OOP) covering fundamental concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with practical Java programming techniques. It includes a detailed course structure with chapters on advanced OOP concepts, exception handling, GUI development, and file handling. The document also lists assessment criteria and references for further reading.

Uploaded by

ahazinsights
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/ 48

Object Oriented Programming 1

INTRODUCTION
TO
OBJECT-ORIENTED
PROGRAMMING (OOP)

06/27/2025 OOP- CH-1- Introduction to OOP


Contents(Outline of the
2
course)
• Chapter 1: Introduction to Object-Oriented
Programming (OOP)
• Overview of OOP
• Java , JVM and Byte Code
• Basic concepts of OOP
• Classes
• Objects
• Members
• Class member visibility
• Encapsulation, Inheritance and Polymorphism
• Editing, compiling and running a java code

Object Oriented Programming 06/27/2025


Contents(Outline of the
3
course)…
• Chapter 2: More on OOP concepts
• Class declaration and Objects instantiation
• Static and Instance members
• Methods and their components
• Constructors (Default, parameterized and Overloaded)
• Access modifiers
• Accessors and Mutators
• Calling and returning methods
• Java class libraries
• Arrays and Strings
• Exploring java.io

Object Oriented Programming 06/27/2025


Contents(Outline of the
4
course)…
• Chapter 3: Inheritance
• Concept of inheritance
• Super-classes and sub-classes
• Protected members
• Overriding methods
• Using this() and super()
• Use of final keyword with inheritance
• Constructors in subclasses

Object Oriented Programming 06/27/2025


Contents(Outline of the
5
course)…
• Chapter 4: Polymorphism and Interface
• Introduction
• Relationships among objects in an inheritance hierarchy
• Demonstrating polymorphism concepts with
superclasses and subclasses
• Abstract classes and methods
• Multiple inheritance and interfaces

Object Oriented Programming 06/27/2025


Contents(Outline of the
6
course)…
• Chapter 5: Exception Handling
• Exception handling overview
• The causes of exceptions and its types
• Java exception hierarchy
• Handling of an exception
• The throw statement
• The finally clause
• User defined exceptions

Object Oriented Programming 06/27/2025


Contents(Outline of the
7
course)…
 Chapter 6: Java GUI and Database
 Introduction
 Common GUI Components [1]
 Simple GUI-Based input/output with JOptionPane
 Swing GUI
 Introduction to Layout Managers
 Event type and listener interfaces
 Mouse and key event handling
 Common GUI Components [2]
• JavaFX GUI
 Database connection with Java
Object Oriented Programming 06/27/2025
Contents(Outline of the
8
course)…
• Chapter 7: Files and Streams
• Introduction
• Java I/O classes
• File and FileDialog objects
• Low-Level and High-Level File I/O
• Random Access files
• Object Serialization
• Stream API

Object Oriented Programming 06/27/2025


Contents(Outline of the
9
course)…

Assessment Forms: % of credit


Lecture and Practice (100%) allotted
Lab and Project 25
Test 25
Final examination 50
Total 100

Object Oriented Programming 06/27/2025


Textbook and References
10

1. Herbert Schildt (2022) Java Beginner’s guide (9th ed)


2. Herbert Schildt (2022) Java the Complete Reference (11th ed)
3. Walter Savitch (2019) Java an Int. to Problem Solving & Programming
(8th ed)
4. Paul Deitel and Harvey Deitel (2018). Java How to program Early
Objects (11th ed)
5. Daniel Liang (2015) Int. to Java Programming Compressive version
(10th ed)
6. Ralph Morelli and Ralph Walde (2017) Java, java, java Object Oriented
Problem Solving (3rd ed)
7. Java documentation available at:
https://docs.oracle.com/javase/tutorial/java/index.html

Object Oriented Programming 06/27/2025


Chapter 1: Introduction to Object-Oriented
Programming (OOP)
11

• Overview of OOP?
• Java , JVM and Byte Code
• Basic concepts of OOP
• Classes
• Objects
• Members
• Class member visibility
• Encapsulation, Inheritance and Polymorphism
• Editing, compiling and running a java code

Object Oriented Programming 06/27/2025


Overview of Object Oriented
Programming
12

 As the demand for more powerful software


increases, building software quickly and
economically still remains a challenge
 Programmers and most organizations are
therefore always eager to reuse “past work” to
reduce cost
 Using OOP, it is possible to create reusable
blocks of code called classes.
 A class is a set of plans that specify how to
build an object
 A class serves a blueprint for an object
Object Oriented Programming 06/27/2025
Basic Concepts of OOP
13

 What is an Object?
 It is a single software unit that
represents an entity in the real world.
E.g., Dog, Automobile, Student, Account, Book…
 Objects have attributes and methods.
 Attribute: the "characteristics" of an
object; such as name, color and size
 Method: A "behavior" of an object; like a
function associated with an object. E.g.,
move, calculate, and communicate
Object Oriented Programming 06/27/2025
Basic Concepts of OOP
14

 What is an Object? …
 It is a software bundle of states and behavior or
an encapsulation of its attributes and methods or
an integration of data and functionalities
 Software objects are used to model real world
objects.
 Every real world object shares two
characteristics: state and behavior. For example,
a dog as an object has state (name, color, breed,
hungry), and behavior (barking, wagging tail,
fetching)

Object Oriented Programming 06/27/2025


Basic Concepts of OOP
15

 Identifying an Object
 Take a minute right now and identify two
objects in your surrounding and characterize
them in terms of their state and behavior.
 In other words, pick two real world objects
and specify what their current states are and
what possible behavior they can perform (or
what they are capable of doing to change
their current state at any time).

Object Oriented Programming 06/27/2025


Basic Concepts of OOP
16
 Identifying an Object …
 In your observations, notice that real-world objects vary in
complexity;
 your ceiling lamp may have only two possible states (on and
off) and two possible behaviors (turn on, turn off), but
 your TV remote might have additional states (on, off, current
volume, current program) and behavior (turn on, turn off,
increase volume, decrease volume, select program).
 You may also notice that some objects, in turn, will also contain
other objects. Can you give an example? ->
 TV in room, engine in car, etc.
 These real-world observations all translate into the world of
object-oriented programming.

Object Oriented Programming 06/27/2025


Basic Concepts of OOP
17
 Identifying an Object …
 You can also think of other non physical
things as objects:- such as a bank account
 A bank account is not something that can be
physically touched but intellectually we can
consider a bank account to be an object.
Bank Account
Attributes: account number, owner, balance …
Behaviors: withdraw, deposit, checkbalance,
transfer, …

Object Oriented Programming 06/27/2025


Basic Concepts of OOP
18

 Identifying an Object …
 Software objects are conceptually similar to real world
objects. They too constitute states and related behavior.
 An object stores its states in fields (variables in some
programming languages) and reveals its behavior
through methods (functions in some programming
languages).
 Methods operate on an object's internal states and serve
as a primary means for object to object communication.
 Hiding internal states and requiring all interactions to be
performed through an object’s methods is called data
encapsulation - a fundamental principle of object
oriented programming.
Object Oriented Programming 06/27/2025
Basic Concepts of OOP
19
 Benefits of bundling code into individual software objects:
 Modularity: The source code for an object can be written and maintained
independently of the source code for other objects. Once created, an
object can be easily passed around inside the system.
 Information-hiding: By interacting only with an object's methods, the
details of its internal implementation remain hidden from the outside
world.
 Code re-use: If an object already exists (perhaps written by another
software developer), you can use that object in your program.
 Ease in code plug and debug: If a particular object turns out to be
problematic, you can simply remove it from your application and plug in a
different object as its replacement. This is analogous to fixing mechanical
problems in the real world. If a bolt breaks, you replace it, not the entire
machine.

Object Oriented Programming 06/27/2025


Basic Concepts of OOP
20

 When creating an Object Oriented program, we start by


writing the code for a class, not an object.
 A class is a blueprint that defines the nature of future
objects by grouping their attributes and methods
together.
 In real world, you'll often find many individual objects all of
the same kind, eg DOG

Object Oriented Programming 06/27/2025


Sample Class: Vehicle
21

❑The class Vehicle with attributes door, speed, color and method
run() can be defined as follows:

Object Oriented Programming 06/27/2025


Basic Concepts of OOP
24

 The objects created from the class are


called instances of the class.
 The process of instantiating a class occupies
the initial memory for the object and returns
a reference
 Every object will have the same
attributes but,
 Each instance will have its own distinct
values for those attributes.

Object Oriented Programming 06/27/2025


Basic Concepts of OOP-Bank Example
25

class: Account
 This "account" class
number:
describes the attributes and
behaviors of bank accounts. balance:

 The “account” class defines deposit()

two state variables (account withdraw()


number and balance) and two
methods (deposit and
withdraw).
Object Oriented Programming 06/27/2025
Basic Concepts of OOP-Bank Example
26 Instance #1

number: 054
 When the program runs
balance: $19
there will be many instances
Instance #2
of the account class.
number: 712
 Each instance will have its
balance: $240
own account number and
Instance #3
balance (object state)
number: 036
 Methods can only be
balance: $941
invoked .
Object Oriented Programming 06/27/2025
Pillars of OOP
27

 OOP contains the following fundamentals


principles
 Abstraction
 Allows us to consider complex ideas while ignoring
irrelevant detail that would confuse us.
 It is the process of selecting data from a larger pool to
show only the relevant details to the object.
 This concept helps developers more easily make
changes and additions over time.
 Example
 Suppose you want to create a Student information
management system/application and you are asked to
collect all the information about your users.

Object Oriented Programming 06/27/2025


Example: Abstraction
Student information management
system/application
28

Full name, phone number, ID, GPA,


batch, department, religion, credit
history, economic status, academic
status, hobbies, favorite movie

Full name, phone


number ID, GPA,
batch, department,
academic status
The process of fetching/removing/selecting the
user information from a larger pool is referred to
as Abstraction.
Object Oriented Programming 06/27/2025
Pillars of OOP-Encapsulation
29

 The implementation and state of each


object are privately held inside a defined
boundary, or class.
 Other objects do not have access to this
class or the authority to make changes but
are only able to call a list of public methods.
 This characteristic of data
hiding provides greater program security
and avoids unintended data corruption.

Object Oriented Programming 06/27/2025


Pillars of OOP-
30
Encapsulation
 Imagine you are designing a basic program to
simulate a car and its driver.
 Car has attributes: speed and fuel_level

 Driver has attributes: name and

driving_experience
 Without Encapsulation, all attributes of the Car
and Driver classes are public.
 I.e., The Driver class can modify the speed and

fuel_level attributes of the Car class.


 What might be the problem for the Driver
class to directly access and modify the
attributes of the Car class??
Object Oriented Programming 06/27/2025
Pillars of OOP-
31
Encapsulation
 Directly accessing and modifying attributes of
the Car class without encapsulation creates tight
coupling between the two classes,
 meaning any changes to the implementation of
the Car class might affect the Driver class as
well. For instance,
 Any changes to the variable declarations in the Car
class (such as renaming speed to velocity or changing its
data type) will impact the Driver class.
 Or if the Car class has public methods that the Driver
class relies on, changes to the method signatures
(parameter types, return types, or method names) can
break the Driver class.
Object Oriented Programming 06/27/2025
Pillars of OOP-Inheritance
32

 Allows us to define general characteristics and


operation of an object and allow us to create more
specialized versions of this object.
 Inheritance is the ability of one object to acquire
some/all properties of another object.
 For example, a child inherits the traits of his/her
parents.
 With inheritance, reusability is a major advantage.
You can reuse the fields and methods of the
existing class.
 For example, Apple is a fruit so assume that we
have a class Fruit and a subclass of it named Apple.
Our Apple acquires the properties of the Fruit class.
Object Oriented Programming 06/27/2025
Pillars of OOP-Polymorphism
33

 Allows us to interact with an object as its


generalized category regardless of its more
specialized category.
 Polymorphism gives us a way to use a class exactly
like its parent so there is no confusion with mixing
types.
 This being said, each child sub-class keeps its own
functions/methods as they are.
 Example: If we had a superclass called Mammal
that has a method called mammalSound().
 The sub-classes of Mammals could be Dogs,
whales, elephants, and horses.
 Each of these would have their own version
ofObject
a mammal sound (dog-barks,
Oriented Programming whale-
06/27/2025
Benefits of OOP Approach
34

 The OOP approach to programming is well-suited


for programs that are large, complex and
actively updated. OOP provides
 Better abstraction
 Modeling information and behavior together
 Better maintainability
 More comprehensible, less fragile software
 Better usability
 Classes
as encapsulated components that can
be used in other systems

Object Oriented Programming 06/27/2025


Java
35

 Java is a programming language and


a platform.
 Java is a high level, robust, object-oriented
and secure programming language.
 Java was developed by Sun
Microsystems (which is now the
subsidiary of Oracle) in the year 1995.
 James Gosling is known as the father of
Java.
 Java is owned and maintained by Oracle.

Object Oriented Programming 06/27/2025


Cont’d
36

 According to Sun, 3 billion devices run Java.


 There are many devices where Java is currently
used. Some of them are as follows:
 Desktop Applications such as acrobat reader, media
player, antivirus, etc.
 Web Applications such as irctc.co.in, javatpoint.com, etc.
 Enterprise Applications such as banking applications.
 Mobile
 Embedded System
 Smart Card
 Robotics
 Games, etc.

Object Oriented Programming 06/27/2025


Types of Java Applications
37

 There are mainly 4 types of applications


that can be created using Java
programming:
1. Standalone Application: Desktop
applications or window-based applications.
2. Web Application: An application that runs
on the server side and creates a dynamic
page.
3. Enterprise Application: An application that
is distributed in nature, such as banking
applications.
4. Mobile Application: An application which is
created forProgramming
Object Oriented mobile devices. 06/27/2025
38 Object Oriented Programming 06/27/2025
Features of Java
39

 Object Oriented
 In Java, everything is an Object. Java can be easily
extended since it is based on the Object model.
 Simple
 Java is designed to be easy to learn. If you
understand the basic concept of OOP Java, it
would be easy to master.
 Secure
 With Java's secure feature it enables to develop
virus-free, tamper-free systems. Authentication
techniques are based on public-key encryption.

Object Oriented Programming 06/27/2025


Features of Java
40

 Platform Independent
 Unlike many other programming languages
including C and C++, when Java is compiled, it is
not compiled into platform specific machine,
rather into platform-independent byte code.
 This byte code is distributed over the web and
interpreted by the Virtual Machine (JVM) on whichever
platform it is being run on.
 Robust
 Java makes an effort to eliminate error-prone
situations by emphasizing mainly on compile time
error checking and runtime checking.
Object Oriented Programming 06/27/2025
Features of Java
41

 Portable
 Being architecture-neutral and having no
implementation dependent aspects of the
specification makes Java portable.
 Architecture-neutral
 Java compiler generates an architecture-
neutral object file format, which makes the
compiled code executable on many
processors, with the presence of Java
runtime system.
Object Oriented Programming 06/27/2025
Features of Java
42

 High Performance
 With the use of Just-In-Time compilers, Java
enables high performance.
 Multithreaded
 With Java's multithreaded feature it is possible to
write programs that can perform many tasks
simultaneously.
 This design feature allows the developers to construct
interactive applications that can run smoothly.
 Distributed
 Java is designed for the distributed environment of
the internet.
Object Oriented Programming 06/27/2025
Editing a Java Program
43

 Any text editor can be used to write/edit a


Java program.
 Every class definition is written in a
separate file.
 The name of each file must have the same name
as the class, with .java added to the end.
 For example, the class BankAccount must be
defined in a file named BankAccount.java

Object Oriented Programming 06/27/2025


Compiling a Java Program
44

 Different computers have their own


machine language,
 For most high-level programming
languages, compilers translate the source
code directly into the machine language of
only a particular computer.
 Therefore, a different compiler for each
type of computer is needed.
 Java, however, uses a slightly different and
much more versatile approach to compiling.

Object Oriented Programming 06/27/2025


Compiling a Java Program-Java bytcode
45

 As soon as a java program is compiled, java


bytecode is generated.
 a machine code in the form of a .class file
 a file that has the same name as the source file but
with the extension .class
 Bytecode is not the machine language for any
particular computer.
 Bytecode is an intermediate machine language
understood only by a hypothetical computer
known as the java virtual machine (JVM)
 With the help of java bytecode we achieve
platform independence in java.
Object Oriented Programming 06/27/2025
Running a Java Program
46

 Once a java program successfully compiles and


generates the “.class” file, it can be executed
 The JVM loads, verifies and executes Java bytecode.
 It is known as the interpreter or the core of Java
programming language because it executes Java
programming.
 JVM (Java Virtual Machine) is an abstract
machine.
 Itis a specification that provides runtime
environment in which java byte-code can be
executed.
 JVMs are available for many hardware and software
platforms (i.e. JVM is platform dependent).
Object Oriented Programming 06/27/2025
Editing, compiling, and running
HelloWorld.java
47

Object Oriented Programming 06/27/2025


Steps of compiling and running java
program
48

 Write the source code


 Compile the code with java compiler or
javac
 The javac will create a .class file which is
the bytecode(not executable)
 Java Virtual Machine interprets the
bytecode and converts it to machine
code
 The JVM executes the bytecode
 OutputObject
is Oriented
displayed
Programming 06/27/2025
49 Object Oriented Programming 06/27/2025
50

End of chapter 1

Object Oriented Programming 06/27/2025

You might also like