OOP (O - O P S) : S Bject Riented Rogramming Ystem

You might also like

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

OOPS (OBJECT-ORIENTED PROGRAMMING

SYSTEM)

1
• Simula is considered the first object-oriented
programming language. The programming paradigm
where everything is represented as an object is known as
a truly object-oriented programming language.
• Smalltalk is considered the first truly object-oriented
programming language.
• The popular object-oriented languages are Java,
PHP,C#,Python etc.
• The main aim of object-oriented programming is to
implement real-world entities, for example, object,
classes, abstraction, inheritance, polymorphism, etc.
OOPS (OBJECT-ORIENTED PROGRAMMING
SYSTEM)

• Object means a real-world entity such as a pen, chair,


table, computer, watch, etc.
• Object-Oriented Programming is a methodology to
design a program using classes and objects.
• It simplifies software development and maintenance by
providing some
concepts:Object,Class,Inheritance,Polymorphism,Abstra
ction ,Encapsulation etc.
Object:Any entity that has state and behavior is known as
an object. For example, a chair, pen, table, keyboard, bike,
etc. It can be physical or logical.
Class:Collection of objects is called class. It is a logical
entity.
A class can also be defined as a blueprint from which you
can create an individual object. Class doesn't consume any
space.
Inheritance: When one object acquires the properties and
behaviors of a parent object, it is known as inheritance. It
provides code reusability.
4
POLYMORPHISM
• If one task is performed in different ways, it is known as
polymorphism.
• For example: to convince the customer differently, to
draw something, for example, shape, triangle, rectangle,
etc.
• In Java, we use method overloading and method
overriding to achieve polymorphism.
• Another example can be to speak something; for
example, a cat speaks meow, dog barks woof, etc.

5
ABSTRACTION
• Hiding internal details and showing functionality is
known as abstraction.
• For example phone call, we don't know the internal
processing.
• In Java, we use abstract class and interface to achieve
abstraction.

6
ENCAPSULATION

• Binding (or wrapping) code and data together into a


single unit are known as encapsulation.
• For example, a capsule, it is wrapped with different
medicines.
• A java class is the example of encapsulation. Java bean is
the fully encapsulated class because all the data members
are private here.

7
OBJECT

• Any entity that has state and behavior is known as an


object. For example, a chair, pen, table, keyboard, bike,
etc. It can be physical or logical.
• An Object can be defined as an instance of a class. An
object contains an address and takes up some space in
memory.
• Objects can communicate without knowing the details of
each other's data or code.
• Example: A dog is an object because it has states like
color, name, breed, etc. as well as behaviors like
wagging the tail, barking, eating, etc.
An object has three characteristics:
State: represents the data (value) of an object.
Behavior: represents the behavior (functionality) of an
object such as deposit, withdraw, etc.
Identity: An object identity is typically implemented via a
unique ID. The value of the ID is not visible to the external
user. However, it is used internally by the JVM to identify
each object uniquely.
• For Example, Pen is an object. Its name is Reynolds;
color is white, known as its state. It is used to write, so
writing is its behavior.
• An object is an instance of a class. A class is a template
or blueprint from which objects are created. So, an object
is the instance(result) of a class.
• Object Definitions:
• An object is a real-world entity.
• An object is a runtime entity.
• The object is an entity which has state and behavior.
• The object is an instance of a class.
OO PROGRAMMING CONCEPTS

• Object-oriented programming (OOP) involves


programming using objects.
• An object represents an entity in the real world that can
be distinctly identified.
• For example, a student, a desk, a circle, a button, and
even a loan can all be viewed as objects.

11
OO PROGRAMMING CONCEPTS

• The state of an object (also known as its properties or attributes)


is represented by data fields with their current values.
• A circle object, for example, has a data field radius, which is the
property that characterizes a circle.
• A rectangle object has the data fields width and height, which
are the properties that characterize a rectangle.

12
• The behavior of an object (also known as its actions) is
defined by methods. To invoke a method on an object is
to ask the object to perform an action.
• For example, you may define methods named getArea()
and getPerimeter() for circle objects.
• A circle object may invoke getArea() to return its area
and getPerimeter() to return its perimeter.

13
OO PROGRAMMING CONCEPTS

• Objects of the same type are defined using a common class. A


class is a template, blueprint, or contract that defines what an
object’s data fields and methods will be.
• An object is an instance of a class. You can create many instances
of a class. Creating an instance is referred to as instantiation.

• The terms object and instance are often interchangeable. The


relationship between classes and objects is analogous to that
between an apple-pie recipe and apple pies: You can make as
many apple pies as you want from a single recipe.

14
OBJECTS
Class Name: Circle A class template

Data Fields:
radius is _______

Methods:
getArea

Circle Object 1 Circle Object 2 Circle Object 3 Three objects of


the Circle class
Data Fields: Data Fields: Data Fields:
radius is 10 radius is 25 radius is 125

An object has both a state and behavior. The state defines the object,
and the behavior defines what the object does.

15
CLASS
16

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All
rights reserved.
CLASSES

• Classes are constructs that define objects of the same type. A


Java class uses variables to define data fields and methods to
define behaviors.
• Additionally, a class provides a special type of methods, known
as constructors, which are invoked to construct objects from the
class.

17
CLASSES
class Circle {
/** The radius of this circle */
double radius = 1.0; Data field

/** Construct a circle object */


Circle() {
}
Constructors
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}

/** Return the area of this circle */


double getArea() { Method
return radius * radius * 3.14159;
}
} 18
CLASSES
• The Circle class is different from all of the other classes you
have seen thus far. It does not have a main method and therefore
cannot be run; it is merely a definition for circle objects. The
class that contains the main method will be referred to in this
book, for convenience, as the main class.
• The illustration of class templates and objects in can be
standardized using Unified Modeling Language (UML) notation.
This notation is called a UML class diagram, or simply a class
diagram.

19
UNIFIED MODELING LANGUAGE (UML) CLASS DIAGRAM

In the class diagram, the data field is denoted as


dataFieldName: dataFieldType

The constructor is denoted as:


ClassName(parameterName: parameterType)

The method is denoted as


methodName(parameterName: parameterType): returnType 20
EXAMPLE: SIMPLECIRCLE CLASS

21
EXAMPLE: SIMPLE CIRCLE CLASS

22
EXAMPLE: SIMPLECIRCLE CLASS
• The program contains two classes. The first of these,
TestSimpleCircle, is the main class. Its sole purpose is to test
the second class, SimpleCircle.
• Such a program that uses the class is often referred to as a
client of the class.
• When you run the program, the Java runtime system invokes
the main method in the main class.

23
• You can put the two classes into one file, but only one
class in the file can be a public class.
• Furthermore, the public class must have the same name
as the file name. Therefore, the file name is
TestSimpleCircle.java, since TestSimpleCircle is public.
• Each class in the source code is compiled into a .class
file.
• When you compile TestSimpleCircle.java, two class
files TestSimpleCircle.class and SimpleCircle.class are
generated,
24
COMBINE TWO CLASSES INTO ONE

25
COMBINE TWO CLASSES INTO ONE

26
EXAMPLE: DEFINING CLASSES AND CREATING OBJECTS
TV
channel: int The current channel (1 to 120) of this TV.
volumeLevel: int The current volume level (1 to 7) of this TV.
on: boolean Indicates whether this TV is on/off.

The + sign indicates +TV() Constructs a default TV object.


a public modifier.
+turnOn(): void Turns on this TV.
+turnOff(): void Turns off this TV.
+setChannel(newChannel: int): void Sets a new channel for this TV.
+setVolume(newVolumeLevel: int): void Sets a new volume level for this TV.
+channelUp(): void Increases the channel number by 1.
+channelDown(): void Decreases the channel number by 1.
+volumeUp(): void Increases the volume level by 1.
+volumeDown(): void Decreases the volume level by 1.

The constructor and methods in the TV class are


defined public so they can be accessed from
other classes. 27
EXAMPLE: DEFINING CLASSES AND CREATING OBJECTS

28
EXAMPLE: DEFINING CLASSES AND CREATING OBJECTS

29
METHOD
30

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All
rights reserved.
METHOD IN JAVA

 A method is a block of code or collection of statements


or a set of code grouped together to perform a certain
task or operation. It is used to achieve the reusability of
code.
 We write a method once and use it many times. We do
not require to write code again and again.
 It also provides the easy
modification and readability of code, just by adding or
removing a chunk of code.
 The most important method in Java is
the main() method.
31
JAVA MAIN() METHOD

 The main() is the starting point for JVM to start


execution of a Java program. Without the main() method,
JVM will not execute the program. The syntax of the
main() method is:

32
 public: It is an access specifier. We should use a public
keyword before the main() method so that JVM can
identify the execution point of the program.
 static: You can make a method static by using the
keyword static. We should call the main() method
without creating an object.
 Static methods are the method which invokes without
creating the objects, so we do not need any object to call
the main() method.
 void: In Java, every method has the return type. Void
keyword acknowledges the compiler that main() method
33
does not return any value.
 main(): It is a default signature which is predefined in
the JVM. It is called by JVM to execute a program line
by line and end the execution after completion of this
method. We can also overload the main() method.
 String args[]: The main() method also accepts some
data from the user. It accepts a group of strings, which is
called a string array. It is used to hold the command line
arguments in the form of string values.
 main(String args[])
 Here, agrs[] is the array name, and it is of String type. It
means that it can store a group of string. Remember, this
array can also store a group of numbers but in the form
of string only. Values passed to the main() method is
called arguments. These arguments are stored into args[]
34
array, so the name args[] is generally used for it.
 What happens if the main() method is written
without String args[]?

35
 The program will compile, but not run, because JVM
will not recognize the main() method. Remember JVM
always looks for the main() method with a string type
array as a parameter.

36
METHOD DECLARATION

37
 Method Signature: Every method has a method signature. It is a
part of the method declaration. It includes the method
name and parameter list.
 Access Specifier: Access specifier or modifier is the access type
of the method. It specifies the visibility of the method. Java
provides four types of access specifier:
 Public: The method is accessible by all classes when we use
public specifier in our application.
 Private: When we use a private access specifier, the method is
accessible only in the classes in which it is defined.
 Protected: When we use protected access specifier, the method
is accessible within the same package or subclasses in a different
package.
 Default: When we do not use any access specifier in the method
declaration, Java uses default access specifier by default.
38
 Return Type: Return type is a data type that the method
returns. It may have a primitive data type, object,
collection, void, etc. If the method does not return
anything, we use void keyword.
 Method Name: It is a unique name that is used to define
the name of a method. It must be corresponding to the
functionality of the method.
 Suppose, if we are creating a method for subtraction of
two numbers, the method name must be subtraction(). A
method is invoked by its name.

39
 Parameter List: It is the list of parameters separated by
a comma and enclosed in the pair of parentheses. It
contains the data type and variable name. If the method
has no parameter, left the parentheses blank.
 Method Body: It is a part of the method declaration. It
contains all the actions to be performed. It is enclosed
within the pair of curly braces.

40
NAMING A METHOD

 While defining a method, remember that the method


name must be a verb and start with a lowercase letter. If
the method name has more than two words, the first
name must be a verb followed by adjective or noun. In
the multi-word method name, the first letter of each word
must be in uppercase except the first word. For
example:
 Single-word method name: sum(), area()
 Multi-word method name: areaOfCircle(),
stringComparision()
 It is also possible that a method has the same name as
another method name in the same class, it is known
as method overloading. 41
TYPES OF METHOD

 There are two types of methods in Java:


 Predefined Method

 User-defined Method

42
PREDEFINED METHOD

 In Java, predefined methods are the method that is already


defined in the Java class libraries is known as predefined
methods. It is also known as the standard library
method or built-in method.
 We can directly use these methods just by calling them in the
program at any point. Some pre-defined methods are length(),
equals(), compareTo(), sqrt(), etc.
 When we call any of the predefined methods in our program,
a series of codes related to the corresponding method runs in
the background that is already stored in the library.
 Each and every predefined method is defined inside a class.
Such as print() method is defined in
the java.io.PrintStream class. It prints the statement that we
write inside the method. For example, print("Java"), it prints 43
Java on the console.
 public class Demo
{

 public static void main(String[] args)

{

 // using the max() method of Math class

 System.out.print("The maximum number is: " +


Math.max(9,7));
}

}

44
USER-DEFINED METHOD

 The method written by the user or programmer is


known as a user-defined method.
 These methods are modified according to the
requirement.

45
 //user defined method
 public static void findEvenOdd(int num)

{

 //method body

 if(num%2==0)

 System.out.println(num+" is even");

 else

 System.out.println(num+" is odd");

}

46
STATIC METHOD

 A method that has static keyword is known as static


method. In other words, a method that belongs to a class
rather than an instance of a class is known as a static
method. We can also create a static method by using the
keyword static before the method name.
 The main advantage of a static method is that we can call
it without creating an object. It can access static data
members and also change the value of it. It is used to
create an instance method. It is invoked by using the
class name. The best example of a static method is
the main() method.
47
ASSINGMENT
48

Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All
rights reserved.
Create a class called Time, which has three private
instance variables – hour, min and sec.
It contains a method called add( ) which takes one
Time object as parameter and print the added
value of the calling Time object and passes Time
object.
In the main method, declare two Time objects and
assign values using constructor and call the add()
method.

49
50

You might also like