OOP1 - Unit 3 (Amiraj) VisionPapers - in
OOP1 - Unit 3 (Amiraj) VisionPapers - in
In mathematics, we might have studied about functions. For example, f(x) = x2 is a function that returns a squared
value of x.
If x = 2, then f(2) = 4
If x = 3, f(3) = 9
and so on.
Similarly, in computer programming, a function is a block of code that performs a specific task.
In object-oriented programming, the method is a jargon used for function. Methods are bound to a class and they
➔ Depending on whether a method is defined by the user, or available in the standard library, there are two types of
methods in Java:
◆ User-defined Methods
STANDARD LIBRARY METHOD
➔ The standard library methods are built-in methods in Java that are readily available
for use. These standard libraries come along with the Java Class Library (JCL) in a
➔ For example,
We can also create methods of our own choice to perform some task. Such methods are called
user-defined methods.
}
➔ Here, we have created a method named myMethod(). We can see that we have used
the public, static and void before the method name.
◆ public - access modifier. It means the method can be accessed from anywhere.
in Java is:
// method body
Here,
➔ modifier - It defines access types whether the method is public, private and so on.
➔ static - If we use the static keyword, it can be accessed without creating objects.
For example, the sqrt() method of standard Math class is static. Hence, we can directly call Math.sqrt()
➔ returnType - It specifies what type of value a method returns For example if a method has int return type
If the method does not return a value, its return type is void.
method.
Passing Parameters by Value means calling a method with a parameter. Through this, the argument value is passed to the
parameter.
OVERLOADING METHODS
➔ In Java, two or more methods can have same name if they differ in parameters (different number of
parameters, different types of parameters, or both). These methods are called overloaded methods and this
➔ Here, the func() method is overloaded. These methods have the same name but accept different arguments.
➔ Notice that, the return type of these methods is not the same. Overloaded methods may or may not have
different return types, but they must differ in parameters they accept.
➔ Two or more methods can have
achieved by either:
◆ changing the number of
arguments.
of arguments.
type of methods.
AMBIGUITY IN METHOD OVERLOADING
You will get compile time error as
The method foo(Object) is
ambiguous for the type Test because
both String and Integer class have
Object as parent class and there is no
inheritance. So java compiler doesn't
consider any of them to be more
specific, hence the method
ambiguous call error.
SCOPE OF VARIABLE ➔ Scope of a variable is the part of
the program where the variable is
accessible. Like C/C++, in Java,
all identifiers are lexically (or
statically) scoped, i.e.scope of a
variable can determined at
compile time and independent of
function call stack.
➔ Java programs are organized in
the form of classes. Every class is
part of some package. Java scope
rules can be covered under
following categories.
ABSTRACTIO
Abstract method in Java with
N AND examples. A method without body
(no implementation) is known as
STEPWISE abstract method. A method must
always be declared in an abstract
REFINEMEN class, or in other words you can say
that if a class has an abstract
T method, it should be declared
abstract as well.
TOP DOWN DESIGN
TOP DOWN AND BOTTOM UP
IMPLEMENTATION
➔ An array is a collection of similar types of data. It is a container that holds data (values)
of one single type. For example, you can create an array that can hold 100 values of int
type.
➔ In Java, arrays are a fundamental construct that allows you to store and access a large
dataType[] arrayName;
● dataType - it can be primitive data types like int, char, double, byte, etc. or Java objects
● arrayName - it is an identifier
double[] data;
Good question! We have to allocate memory for the array. The memory will define the
➔ Just as you can pass primitive type values to methods, you can also pass
arrays to a method. To pass an array to a method, specify the name of the
array without any square brackets within the method call. Unlike C/C++, in
Java every array object knows its own length using its length field, therefore
while passing array's object reference into a method, we do not need to pass
the array length as an additional argument
RETURNING ARRAY FROM METHOD
TWO DIMENSIONAL ARRAY AND ITS
PROCESSING
array in Java.
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
SEARCHING AND SORTING ARRAYS
AND ARRAY CLASS
SEARCHING
How to sort an array and search
ARRAY an element inside it?
LINEAR SEARCH
Linear search is used to search a key element from multiple elements. Linear search is less used
today because it is slower than binary search and hashing.
Algorithm: