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

2 Using Objects

The document provides an overview of object-oriented programming concepts in Java, focusing on class and object declarations, constructors (default and parameterized), and methods (predefined and user-defined). It explains constructor overloading, method overloading, variable types (local, instance, static), and the differences between constructors and methods. Additionally, it covers call by value, call by reference, variable length arguments, and the for-each loop for traversing arrays or collections.

Uploaded by

usf0992
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)
3 views26 pages

2 Using Objects

The document provides an overview of object-oriented programming concepts in Java, focusing on class and object declarations, constructors (default and parameterized), and methods (predefined and user-defined). It explains constructor overloading, method overloading, variable types (local, instance, static), and the differences between constructors and methods. Additionally, it covers call by value, call by reference, variable length arguments, and the for-each loop for traversing arrays or collections.

Uploaded by

usf0992
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/ 26

CSE 1201

Object Oriented Programming


slide-2
Class and Object Declaration in java:
Constructor in java (Default & Parameterized):
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.
At the time of calling the constructor, memory for the object
is allocated in the memory.

Rules for creating Java constructor-----


▪ Constructor name must be the same as its class name
▪ A Constructor must have no explicit return type
▪ A Java constructor cannot be abstract, static, final, and
synchronized
Constructor in java (Default & Parameterized):
There are two types of Constructor in Java. 1. Default 2. Parameterized

A constructor is called "Default Constructor" when it doesn't have any


parameter.
It calls a default constructor if there is no constructor available in the
class. In such case, Java compiler provides a default constructor by
default.
The default constructor is used to provide the default values to the object
like 0, null, etc., depending on the type.

A constructor which has a specific number of parameters is called a


parameterized constructor.

The parameterized constructor is used to provide different values to


distinct objects. However, you can provide the same values also.
Parameterized Constructor
Default Constructor
Constructor Overloading:
In Java, a constructor is just like a method but without return type. It
can also be overloaded like Java methods.

Constructor overloading in Java is a technique of having more than


one constructor with different parameter lists.

They are arranged in a way that each constructor performs a different


task.

They are differentiated by the compiler by the number of parameters


in the list and their types.
Constructor overloading
Constructor vs Method
Constructor Method

1. Used to initialize the state of an object 1. Used to expose behaviour of an object

2. Must have not return type 2. Must have return type

3. Constructor is invoked implicitly 3. Method is invoked explicitly

4. Constructor name must be same as the 4.Method name should not be the same
class name. as the class name.
5. Constructor will get executed only 5. A method can be executed n number of
once per object. times on a object.
6. The Java compiler provides a default 6. The method is not provided by the
constructor if you don't have any compiler in any case.
constructor in a class.
Methods
● The method in Java is a collection of statements that perform
some specific task and return the result to the caller.
● A Java method can perform some specific task without
returning anything.
● Java Methods allow us to reuse the code without retyping the
code.
● In Java, every method must be part of some class that is
different from languages like C, C++, and Python.

● 1. A method is like a function i.e. used to expose the behavior of


an object.
● 2. It is a set of codes that perform a particular task.
Methods
● There are two types of methods in Java:
● 1. Predefined Method
In Java, predefined methods are the method that is already defined in
the Java class libraries is known as predefined methods. 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.

● 2. 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.
Methods
Methods Implementation in Java
Method overloading
❑ If a class has multiple methods having same name but
different in parameters, it is known as Method Overloading.
❑ If we have to perform only one operation, having same name
of the methods increases the readability of the program.
❑ Suppose you have to perform addition of the given numbers
but there can be any number of arguments, if you write the
method such as a(int,int) for two parameters, and
b(int,int,int) for three parameters
❑ then it may be difficult for you as well as other programmers
to understand the behavior of the method because its name
differs.
Example of Method Overloading
Method overloading possible in three ways.
1. Changing the number of parameters.
2. Changing the datatype of parameters.
3. Changing the order of parameters.

Examples:
void add();
void add(int a, int b);
Void add(int a, int b, int c);
Void add(double a. double b);

***In java, method overloading is not possible by changing the


return type of the method only because of ambiguity.
● Can we overload java main() method?
● Yes, by method overloading. You can have any number of main
methods in a class by method overloading. But JVM calls
main() method which receives string array as arguments only.
Let's see the simple example:

Output: main with String[]


Variables
● There are three types of variables in Java:
1. local variable: A variable declared inside the body of the method
is called local variable. A local variable cannot be defined with
"static" keyword.
2. instance variable: A variable declared inside the class but
outside the body of the method, is called an instance
variable. It is not declared as static.
3. Static/class variable: A variable that is declared as static is
called a static variable. It cannot be local. You can
create a single copy of the static variable and share it
among all the instances of the class. Memory allocation
for static variables happens only once when the class is
loaded in the memory.
Variables
Static variables
***Static variables can be
accessed by calling the class
name of the class.
There is no need to create an
instance of the class for accessing
the static variables. because static
variables are the class variables
and are shared among all the
class instances.
Static Method
● Static Methods in Java
1. The static methods of a particular class can only access
the static variables and can change them.
2. A static method can only call other static methods.
3. Static methods can't refer to non-static variables or
methods.
4. Static methods can't refer to “super” or “this” members.
5. static methods in Java are resolved at compile time.
Since method overriding is part of Runtime
Polymorphism, static methods can't be overridden.
Call by value:
Primitive data types - includes byte , short , int , long , float , double ,
boolean and char.
Non-primitive data types - such as String , Arrays and Classes
1. Call a method by passing a value(primitive data)
2. The value is copied to a method parameter
3. Changes to that formal parameter doesn’t affect the actual
parameter.
4. Original value doesn’t change

Output:10
10
Call by Reference
● If we call a method by passing a reference type data then it
is known as call by reference.
● Change to the formal parameter does affect the actual
parameter
● Original value gets change

Output:
Zim
Mim
Variable length arguments A feature that allows a
function to receive any
number of arguments.
For each loop
● The for-each loop is used to traverse array or
collection in Java. It is easier to use than simple for
loop because we don't need to increment value and
use subscript notation.
● It works on the basis of elements and not the index. It
returns element one by one in the defined variable.
Output:
12
23
44
56
78

You might also like