Professional Documents
Culture Documents
OOP (O - O P S) : S Bject Riented Rogramming Ystem
OOP (O - O P S) : S Bject Riented Rogramming Ystem
OOP (O - O P S) : S Bject Riented Rogramming Ystem
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)
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
7
OBJECT
11
OO PROGRAMMING CONCEPTS
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
14
OBJECTS
Class Name: Circle A class template
Data Fields:
radius is _______
Methods:
getArea
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
17
CLASSES
class Circle {
/** The radius of this circle */
double radius = 1.0; Data field
19
UNIFIED MODELING LANGUAGE (UML) CLASS DIAGRAM
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.
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
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
User-defined Method
42
PREDEFINED METHOD
{
}
44
USER-DEFINED METHOD
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
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