0% found this document useful (0 votes)
6 views

Basic concepts of OOP (1)

The document outlines the principles and characteristics of Object-Oriented Programming (OOP) including concepts such as classes, objects, encapsulation, abstraction, polymorphism, and inheritance. It also discusses the Java programming language, its features, and the Java Development Kit (JDK), Java Runtime Environment (JRE), and Java Virtual Machine (JVM). Additionally, it highlights the benefits and applications of OOP in various fields.

Uploaded by

hitfitai
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)
6 views

Basic concepts of OOP (1)

The document outlines the principles and characteristics of Object-Oriented Programming (OOP) including concepts such as classes, objects, encapsulation, abstraction, polymorphism, and inheritance. It also discusses the Java programming language, its features, and the Java Development Kit (JDK), Java Runtime Environment (JRE), and Java Virtual Machine (JVM). Additionally, it highlights the benefits and applications of OOP in various fields.

Uploaded by

hitfitai
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/ 93

Object Oriented

Programming
Course Objectives
● Understand object-oriented Principles.

● Write, Compile and Execute Java Programs.

● Familiarize GUI programming using Swing.

● Introduce database connectivity for


developing applications.
Module 1-CO1
• Apply object oriented programming
paradigms to develop simple JAVA applications
Object Oriented design
➢ OOD is based on Objects and interaction
between the objects
➢ Interaction between objects is called message
communication.
➢ It involves the designing of Objects,Classes
and the relationship between the classes
Object Oriented design
➢ Consider the example of Banking process.
➢ Here, customer, money and account are
objects.
Object Oriented design
• In OOD, implementation of a software based
on the concepts of objects.
• This approach is very close to the real-world
applications
Characteristics of OOP
Characteristics of OOP
• OBJECT
• Objects are real-world entities that has their
own properties and behavior.It has physical
existence
• Eg: person, banks, company, customers etc
OBJECTS

• An object is an instance of a class. Object may


represent a person, a place, a bank account, a
table of data or any item that the program has
to handle. In object oriented programming,
problem is analyzed in terms of objects and the
nature of communication between them.
Program objects should be chosen such that
they match closely with the real-world objects.
Objects takes space in system memory and
have an address.
OBJECTS

• When program is executed, the objects


interact by sending messages to one another.
Each object contains data, and code to
manipulate the data. Objects can interact
without having to know details of each
other’s data or code. It is sufficient to know
the type of the message accepted, and the
type of response returned by the objects.
Two ways of representing an object is shown
below:
OBJECTS
Characteristics of OOP
• CLASS
• A class is a blueprint or prototype from which
objects are created
• A class is a generalized description of an
object.
CLASSES

• Classes are created using the keyword class. A


class declaration defines a new data type that
links data and code. The new type is then used
to declare objects of that class. A class serves as
a plan or template. It specifies what data and
what functions will be included in objects of
that class. Thus a class is a logical abstraction,
but an object has physical existence.
OBJECTS

• A class declaration is similar syntactically to a


structure. Defining the class doesn’t create
any objects. Once a class has been defined,
we can create any number of objects
belonging to that class.
Characteristics of OOP
• Relationship between Object & Class
• Let’s take Human Being as a class. My name is
John, and I am an instance/object of the class
Human Being
• Object has a physical existence while a class is
just a logical definition.
Encapsulation
• The wrapping up of data(variables) and
function (methods) into a single unit (called
class) is known as encapsulation.
• It is also called "information hiding“
Encapsulation
• Encapsulation is the mechanism that binds
together code and the data it manipulates,
and keeps both safe from outside
interference and misuse. When code and data
are linked together, an object is created.
Encapsulation
• Key Points of Encapsulation
• Protection of data from accidental corruption
• Flexibility and extensibility of the code and
reduction in complexity
• Encapsulation of a class can hide the internal
details of how an object does something
• Encapsulation protects abstraction
ABSTRACTION
• ABSTRACTION
• Abstraction means displaying only essential
information and hiding the details.
• Data abstraction refers to providing only
essential information about the data to the
outside world, hiding the background details
or implementation.
ABSTRACTION

• The functions that operate on these data are


sometimes called methods or member
functions. Since the classes use the concept
of data abstraction, they are known as
abstract data types (ADT).
ABSTRACTION
• Consider a real-life example of a man driving
a car. The man only knows that pressing the
accelerators will increase the speed of the car
or applying brakes will stop the car but he
does not know about how on pressing
accelerator the speed is actually increasing,
he does not know about the inner mechanism
of the car or the implementation of
accelerator, brakes etc in the car. This is what
abstraction is.
POLYMORPHISM
• The word polymorphism means having many
forms
• In simple words, we can define polymorphism
as the ability of a message to be displayed in
more than one form.
POLYMORPHISM
• Eg: A person at the same time can have
different characteristic. Like a man at the
same time is a father, a husband, an
employee. So the same person posses
different behavior in different situations. This
is called polymorphism.
• An operation may exhibit different behaviors
in different instances. The behavior depends
upon the types of data used in the operation.
POLYMORPHISM
Inheritance
• The capability of a class to derive properties
and characteristics from another class is
called Inheritance.
• OR
Inheritance

• Inheritance is the process by which objects of


one class acquired the properties of objects
of another classes
• Sub Class : The class that inherits properties
from another class is called Sub class or
Derived Class.
• Super Class : The class whose properties are
inherited by sub class is called Base Class or
Super class.
Inheritance
• Reusability: Inheritance supports the concept
of “reusability”,
• i.e. when we want to create a new class and
there is already a class that includes some of
the code that we want, we can derive our
new class from the existing class. By doing
this, we are reusing the fields and methods of
the existing class.
Inheritance
• Eg: Dog, Cat, Cow can be Derived Class of
Animal Base Class.
Dynamic Binding
★ Binding refers to the linking of a procedure call to the code to
be executed in response to the call
★ Dynamic binding means that the code associated with a given
procedure call is not known until the time of the call at
runtime
★ It is associated with polymorphism and inheritance
. Message Passing
★ Objects communicate with one another by sending and receiving
information
★ A message for an object is a request for execution of a procedure
★ Message passing involves specifying the name of the object, the
name of the method(message), and the information to be sent
Benefits of OOP
★ Through inheritance, we can eliminate redundant code and extend
the use of existing classes.
★ We can build programs from the standard working modules that
communicate with one another, rather than having to start writing
the code from scratch. This leads to saving of development time
and higher productivity.
★ The principle of data hiding helps the programmer to build secure
programs that cannot be invaded by code in other parts of the
program.
Benefits of OOP
★ It is possible to have multiple instances of an object to co-exist
without interference.
★ It is possible to map objects in the problem domain to those in the
program.
★ It is easy to partition the work in a project based on objects.
★ The data-centered design approach enables us to capture more
details of a model in implementable form.
Benefits of OOP
★ Object oriented systems can be easily upgraded from small to
large systems.
★ Message passing techniques for communication between
objects makes the interface descriptions with external
systems much easier.
★ Software complexity can be easily managed.

* Unit I Classes and


Objects
Applications Of OOP
★ Real time systems.

★ Simulation and modeling

★ Object oriented data bases

★ Hyper text, hypermedia.


Applications Of OOP
★ AI and expert systems

★ Neural networks and parallel programming

★ Decision support and office automation systems.

★ CIM/CAM/CAD systems.
JAVA
• Java is a powerful general-purpose , Object
Oriented programming language developed
by Sun Micro System of USA in 1991.
• Development team members are James
Gosling, Patrick Naughton, Chris Warth, Ed
Frank, and Mike Sheridan
JAVA
• First name of Java is “Oak,” but was renamed
“Java” in 1995.
• Java derives much of its character from C and
C++.
• Java Changed the Internet by simplifying web
programming
• Java innovated a new type of networked
program called the applet.
FEATURES OF JAVA
Simple

• It’s simple and easy to learn if you already


know the basic concepts of Object Oriented
Programming.
• C++ programmer can move to JAVA with very
little effort to learn.
• Java syntax is based on C++
• Java has removed many complicated and
rarely-used features, for example, explicit
pointers, operator overloading, etc.
Object oriented

• Java is true object oriented language.


Everything in Java is an object.
• All program code and data reside within
objects and classes.
• Java comes with an extensive set of classes,
arranged in packages that can be used in our
programs through inheritance.
Distributed

• Java is designed for distributed environment


of the Internet. Its used for creating
applications on networks
• Java enables multiple programmers at
multiple remote locations to collaborate and
work together on a single project.
Compiled and Interpreted

• Usually a computer language is either


compiled or Interpreted. Java combines both
this approach and makes it a two-stage
system.
• Compiled : Java enables creation of a cross
platform programs by compiling into an
intermediate representation called Java Byte
code.
Compiled and Interpreted
• Interpreted : Byte code is then interpreted,
which generates machine code that can be
directly executed by the machine that
provides a Java Virtual machine.
Robust
• It provides many features that make the
program execute reliably in variety of
environments.
• Java is a strictly typed language. It checks
code both at compile time and runtime.
• Java takes care of all memory
management problems with garbage-
collection.
Robust

• Java, with the help of exception handling


captures all types of serious errors and
eliminates any risk of crashing the system.
Secure

• Java provides a “firewall” between a


networked application and your computer.
• When a Java Compatible Web browser is used,
downloading can be done safely without fear
of viral infection or malicious intent.
• Java achieves this protection by confining a
Java program to the java execution
environment and not allowing it to access
other parts of the computer.
Architecture Neutral

• Java language and Java Virtual Machine


helped in achieving the goal of “write once;
run anywhere, any time, forever.”
• Changes and upgrades in operating systems,
processors and system resources will not
force any changes in Java Programs
Portable

• Java is portable because it facilitates you to


carry the Java byte code to any platform. It
doesn't require any implementation.
• Java provides a way to download programs
dynamically to all the various typesof
platforms connected to the Internet.
High Performance
• Java performance is high because of the use
of byte code.
• The byte code can be easily translated into
native machine code.
Multithreaded

• Multithreaded Programs handled multiple


tasks simultaneously, which was helpful in
creating interactive, networked programs.
• Java run-time system comes with tools that
support multiprocess synchronization used to
construct smoothly interactive systems
Dynamic

• Java is capable of linking in new class libraries,


methods, and objects.
• It supports functions from native languages
(the functions written in other languages such
as C and C++).
• It supports dynamic loading of classes. It
means classes are loaded on demand.
Extensible
• Java programs support functions written in
other languages such as C and C++.Theses
functions are known as native methods. This
facility enables the programmers to use the
efficient functions available in these
languages .Native methods are linked
dynamically at runtime
JAVA DEVELOPMENT KIT (JDK)

• The Java Development Kit (JDK) is a software


development environment used for developing
and executing Java applications and applets
• It includes the Java Runtime Environment (JRE),
an interpreter/loader (Java), a compiler (javac),
an archiver (jar), a documentation generator
(Javadoc) and other tools needed in Java
development.
• JDK is only used by Java Developers.
JAVA DEVELOPMENT KIT (JDK)
JAVA RUNTIME ENVIRONMENT
(JRE)
• A software program needs an environment to
run .
• The runtime environment loads class files and
ensures there is access to memory and other
system resources to run them.
• Java Runtime Environment provides the
minimum requirements for executing a Java
application programs.
JAVA RUNTIME ENVIRONMENT
(JRE)
• JRE is an installation package which provides
environment to only run(not develop) the
java program(or application)onto your
machine.
• JRE is only used by them who only wants to
run the Java Programs
• i.e. end users of your system. JRE can be view
as a subset of JDK.
JAVA VIRTUAL MACHINE (JVM)

• JVM is a program which provides the runtime


environment to execute Java programs. Java
programs cannot run if a supporting JVM is
not available.
• JVM is a virtual machine that resides in the
real machine (your computer) and the
machine language for JVM is byte code.
JAVA VIRTUAL MACHINE (JVM)

• The Java compiler generate byte code for JVM


rather than different machine code for each
type of machine.
• JVM executes the byte code generated by
compiler and produce output.
• JVM is the one that makes java platform
independent.
JAVA VIRTUAL MACHINE (JVM)

• The primary function of JVM is to execute the


byte code produced by compiler
• The JVM doesn’t understand Java source code,
that’s why we need to have javac compiler
JAVA VIRTUAL MACHINE (JVM)

• Java compiler (javac) compiles *.java files to


obtain *.class files that contain the byte codes
understood by the JVM.
• JVM makes java portable (write once, run
anywhere).
• Each operating system has different JVM,
however the output they produce after
execution of byte code is same across all
operating systems.
JAVA VIRTUAL MACHINE (JVM)
BYTE CODE

• Java byte code is the instruction set for the


Java Virtual Machine
• It is the machine code in the form of a .class
file.
• Byte code is a machine independent code
BYTE CODE

• It is not completely a compiled code but it is


an intermediate code somewhere in the
middle which is later interpreted and
executed by JVM.
• Byte code is a machine code for JVM.
• Byte code implementation makes Java a
platform- Independent language.
BYTE CODE
JAVA COMPILER

• Java is compiled language. But it is very


different from traditional compiling in the
way that after compilation source code is
converted to byte code.
• Javac is the most popular Java compiler
JAVA COMPILER

• Java has a virtual machine called JVM which


then converts byte code to target code of
machine on which it is run.
• JVM performs like an interpreter. It doesn’t do
it alone, though. It has its own compiler to
convert the byte code to machine code.
• This compiler is called Just In Time or JIT
compiler.
JAVA APPLET
• An applet is a special kind of Java program
that is designed to be transmitted over the
Internet and automatically executed by a Java-
compatible web browser
• It runs inside the web browser and works at
client side
JAVA APPLET

• Applets are used to make the web site more


dynamic and entertaining
• Applets are not stand-alone programs.
Instead, they run within either a web browser
or an applet viewer. JDK provides a standard
applet viewer tool called applet viewer.
• In general, execution of an applet does not
begin at main() method.
Java Applet vs Java Application
JAVA PROGRAM STRUCTURE
Documentation Section

• You can write a comment in this section. It


helps to understand the code. These are
optional
• It is used to improve the readability of the
program.
• The compiler ignores these comments during
the time of execution
• There are three types of comments that Java
supports
Documentation Section

• Single line Comment //This is single line


comment
• Multi-line Comment /* this is multiline
comment.
• and support multiple lines*/
• Documentation Comment /** this is
documentation cmnt*/
Package Statement

• We can create a package with any name. A


package is a group of classes that are defined
by a name.
• That is, if you want to declare many classes
within one element, then you can declare it
within a package
Package Statement
• It is an optional part of the program, i.e., if
you do not want to declare any package, then
there will be no problem with it, and you will
not get any errors.
• Package is declared as: package
package_name;
• Eg: package mypackage;
Import Statement

• If you want to use a class of another


package, then you can do this by importing it
directly into your program.
• Many predefined classes are stored in
packages in Java
• We can import a specific class or classes in an
import statement. Examples:
Import Statement

• import java.util.Date; //imports the date class



• import java.applet.*; /*imports all the
classes from the java applet package*/
Interface Statement

• This section is used to specify an interface in


Java
• Interfaces are like a class that includesa
group of method declarations
• It's an optional section and can be used when
programmers want to implement multiple
inheritances within a program.
Class Definition

• A Java program may contain several class


definitions.
• Classes are the main and essential elements
of any Java program.
• A class is a collection of variables and methods
Main Method Class

• The main method is from where the


execution actually starts and follows the
order specified for the following statements
• Every Java stand-alone program requires the
main method as the starting point of the
program.
Main Method Class
• This is an essential part of a Java program.
• There may be many classes in a Java program,
and only one class defines the main method
• Methods contain data type declaration and
executable statements.
A simple java program to print
hello world
public class Hello
{
//main method declaration
public static void main(String args[])
{
System.out.println("hello world");
}
}
• public class Hello - This creates a class called
Hello. We should make sure that the class
name starts with a capital letter, and the
public word means it is accessible from any
other classes.
• Braces - The curly brackets are used to group
all the commands together
A simple java program to print
hello world
• public class Hello - This creates a class called
Hello. We should
• make sure that the class name starts with a
capital letter, and the public word means it is
accessible from any other classes.
• Braces - The curly brackets are used to group
all the commands together
A simple java program to print
hello world
• public static void main
• When the main method is declared public, it
means that it can be used outside of this class
as well.
• The word static means that we want to access
a method without making its objects
A simple java program to print
hello world
• The word void indicates that it does not
return any value. The main is declared as void
because it does not return any value.
• main is a method; this is a starting point of a
Java program.
A simple java program to print
hello world
• String[] args
• It is an array where each element is a string,
which is named as args. If you run the Java
code through a console, you can pass the
input parameter. The main() takes it as an
input.
A simple java program to print
hello world
• System.out.println();
• This statement is used to print text on the
screen as output
• System is a predefined class, and out is an
object of the PrintWriter class defined in the
System
• The method println prints the text on the
screen with a new line.
A simple java program to print
hello world
• We can also use print() method instead of
println() method. All Java statement ends
with a semicolon.
Java Tokens
• TOKENS
• Java Tokens are the smallest individual
building block or smallest unit of a Java
program
• Java program is a collection of
different types of tokens, comments, and
white spaces.
Java Tokens
Keywords

• A keyword is a reserved word. You cannot use


it as a variable name, constant name etc.
• The meaning of the keywords has already
been described to the java compiler. These
meaning cannot be changed.
Keywords

• Thus, the keywords cannot be used as


variable names because that would try to
change the existing meaning of the keyword,
which is not allowed.
• Java language has reserved 50 words as
keywords
Keywords

You might also like