OOPJ [Link]
com/c/EDULINEFORCSE
STUDENTS
CHAPTER 2
OBJECT ORIENTED PROGRAMMING IN
JAVA
Prepared By Mr. EBIN PM, AP, IESCE 1
Class Fundamentals
Classes and Objects
• Classes and objects are the two main aspects of object-oriented
programming.
• So, a class is a template for objects, and an object is an instance
of a class.
• A Class is a "blueprint" for creating objects.
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 1
OOPJ [Link]
STUDENTS
Create a Class
• To create a class, use the keyword class
Create an Object
• To create an object of MyClass, specify the class name, followed by
the object name, and use the keyword new
Prepared By Mr. EBIN PM, AP, IESCE
Multiple Objects
• You can create multiple objects of one class
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 2
OOPJ [Link]
STUDENTS
Initialize the object through a reference variable
Output
101 sonoo
Prepared By Mr. EBIN PM, AP, IESCE
Using Multiple Classes
• We can also create an object of a class and access it in another class.
• This is often used for better organization of classes
• One class has all the attributes and methods, while the other class
holds the main() method (code to be executed).
• Remember that the name of the java file should match the class
name.
• In the following example, we have created two files in the same
directory/folder:
[Link]
[Link]
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 3
OOPJ [Link]
STUDENTS
Prepared By Mr. EBIN PM, AP, IESCE
JAVA CLASS ATTRIBUTES
• Class attributes are variables within a class
Example
Create a class called "MyClass" with two attributes x and y
• Another term for class attributes is fields / Data members
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 4
OOPJ [Link]
STUDENTS
Accessing Attributes
• We can access attributes by creating an object of the class, and by
using the dot syntax (.)
Example
Create an object called "myObj" and print the value of x
Output
5
Prepared By Mr. EBIN PM, AP, IESCE
Modify Attributes
• We can also modify attribute values
Example
Set the value of x to 40
Output
40
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 5
OOPJ [Link]
STUDENTS
Override existing values
Example
Change the value of x to 25
Output
25
Prepared By Mr. EBIN PM, AP, IESCE
If you don't want the ability to override existing values, declare
the attribute as final
The final keyword is useful when we want a variable to always
store the same value, like PI (3.14159...)
The final keyword is called a "modifier".
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 6
OOPJ [Link]
STUDENTS
Multiple Objects
If we create multiple objects of one class, you can change the
attribute values in one object, without affecting the attribute
values in the other
Example - Change the value of x to 25 in myObj2, and leave x in
myObj1 unchanged
Prepared By Mr. EBIN PM, AP, IESCE
Multiple Attributes
We can specify as many attributes as you want
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 7
OOPJ [Link]
STUDENTS
JAVA CLASS METHODS
Methods are declared within a class, and that they are used to
perform certain actions
Example - Create a method named myMethod() in MyClass
myMethod() prints a text (the action), when it is called.
To call a method, write the method's name followed by two
parentheses () and a semicolon;
Prepared By Mr. EBIN PM, AP, IESCE
Example
Inside main, call myMethod()
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 8
OOPJ [Link]
STUDENTS
Static vs. Non-Static Methods
Java programs have either static or public attributes and methods.
Static method can be accessed without creating an object of the
class
Public methods can only be accessed by objects
Example
The differences between static and public methods
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 9
OOPJ [Link]
STUDENTS
Access Methods With
an Object
Prepared By Mr. EBIN PM, AP, IESCE
Remember that..
• The dot (.) is used to access the object's attributes and methods.
• To call a method in Java, write the method name followed by a set
of parentheses (), followed by a semicolon (;)
Using Multiple Classes
• It is a good practice to create an object of a class and access it in
another class.
• Remember that the name of the java file should match the class
name. In this example, we have created two files in the same
directory:
[Link]
[Link]
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 10
OOPJ [Link]
STUDENTS
Prepared By Mr. EBIN PM, AP, IESCE
CONSTRUCTORS
A constructor in Java is a special method that is used to initialize
objects.
The constructor is called when an object of a class is created.
It can be used to set initial values for object attributes
Types of Java constructors
Default constructor (no-argument constructor)
Parameterized constructor
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 11
OOPJ [Link]
STUDENTS
Syntax of default constructor:
<class_name>(){}
In the following example, we are creating the no-argument
constructor in the Bike class. It will be invoked at the time of object
creation.
Output
Bike is created
Prepared By Mr. EBIN PM, AP, IESCE
If there is no constructor in a class, compiler automatically creates
a default constructor.
The purpose of a default constructor
• The default constructor is used to provide the default values to the
object like 0, null, etc., depending on the type.
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 12
OOPJ [Link]
STUDENTS
Prepared By Mr. EBIN PM, AP, IESCE
The constructor name must match the class name, and it cannot
have a return type (like void).
The constructor is called when the object is created.
All classes have constructors by default
If you do not create a class constructor yourself, Java creates one
for you. However, then you are not able to set initial values for
object attributes.
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 13
OOPJ [Link]
STUDENTS
Constructor Parameters ( Parameterized constructor )
Constructors can also take parameters, which is used to initialize
attributes
Prepared By Mr. EBIN PM, AP, IESCE
Constructor Parameters - We can have as many parameters as
you want
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 14
OOPJ [Link]
STUDENTS
Prepared By Mr. EBIN PM, AP, IESCE
Method Overloading
• With method overloading, multiple methods can have the same
name with different parameters
• Method overloading is one of the ways that Java supports
polymorphism.
There are two ways to overload the method in java
By changing number of arguments
By changing the data type
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 15
OOPJ [Link]
STUDENTS
Example
Advantage of method overloading
• The main advantage of this is cleanliness of code.
• Method overloading increases the readability of the program.
• Flexibility
Prepared By Mr. EBIN PM, AP, IESCE
Example - Consider the following example, which have two methods
that add numbers of different type
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 16
OOPJ [Link]
STUDENTS
Instead of defining two methods that should do the same thing, it is
better to overload one.
Prepared By Mr. EBIN PM, AP, IESCE
RECURSION
• Recursion is the technique of making a method call itself.
• This technique provides a way to break complicated problems
down into simple problems which are easier to solve.
Syntax
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 17
OOPJ [Link]
STUDENTS
Working
Example
Use recursion to add all of the
numbers up to 10.
Prepared By Mr. EBIN PM, AP, IESCE
Working
Example
Factorial of a number
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 18
OOPJ [Link]
STUDENTS
USING OBJECT AS A PARAMETER / ARGUMENT
Prepared By Mr. EBIN PM, AP, IESCE
THIS KEYWORD
There can be a lot of usage of java this keyword. In java, this is a
reference variable that refers to the current object.
Usage of java this keyword
• this can be used to refer current class instance variable.
• this can be used to invoke current class method (implicitly)
• this() can be used to invoke current class constructor.
• this can be passed as an argument in the method call.
• this can be passed as argument in the constructor call.
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 19
OOPJ [Link]
STUDENTS
Output
Understanding the problem
without this keyword
Prepared By Mr. EBIN PM, AP, IESCE
Output
Solution of the problem
with this keyword
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 20
OOPJ [Link]
STUDENTS
• It is better approach to use meaningful names for variables. So we
use same name for instance variables and parameters in real time,
and always use this keyword.
this: to invoke current class method
• You may invoke the method of the current class by using the this
keyword.
• If you don't use the this keyword, compiler automatically adds this
keyword while invoking the method
Prepared By Mr. EBIN PM, AP, IESCE
Output
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 21
OOPJ [Link]
STUDENTS
JAVA INNER CLASS
In Java, it is also possible to nest classes (a class within a class).
The purpose of nested classes is to group classes that belong
together, which makes your code more readable and maintainable.
To access the inner class, create an object of the outer class, and
then create an object of the inner class
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 22
OOPJ [Link]
STUDENTS
Access Outer Class From Inner Class
One advantage of inner classes,
is that they can access attributes
and methods of the outer class
Prepared By Mr. EBIN PM, AP, IESCE
Command-Line Arguments
• Sometimes we want to pass information into a program when we
run it. This is accomplished by passing command-line arguments
to main( ).
• The main method can receive string arguments from the
command line
• To access the command-line arguments inside a Java program is
quite easy— they are stored as strings in a String array passed to
the args parameter of main( ).
• The first command-line argument is stored at args[0], the second
at args[1], and so on.
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 23
OOPJ [Link]
STUDENTS
Prepared By Mr. EBIN PM, AP, IESCE
Prepared By [Link] PM, AP, IESCE 24