Unit II
Unit II
class class_name
// fields
// method
// body of class
Example :
Object :
An entity that has state and behavior is known as an object e.g., chair, bike,
marker, pen, table, car, etc.
Object Definitions:
Syntax of object:
Example:
Method:
In general, a method is a way to perform some task. Similarly,
the method in Java is a collection of instructions that performs
a specific task. It provides the reusability of code. We can also
easily modify code using methods.
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.
Naming a Method
Types of Method
Predefined Method
User-defined Method
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.
Advantage of Method
o Code Reusability
o Code Optimization
Unit-II Core Java
Demo.java
}
}
Output:
The maximum number is: 9
User-defined Method
The method written by the user or programmer is known as a
user-defined method. These methods are modified according
Unit-II Core Java
to the requirement.
import java.util.Scanner;
Unit-II Core Java
Example :
Every time an object is created using the new() keyword, at least one
constructor is called.
<class_name>(){}
Example of default constructor :
Example:
A constructor must not have a return type. A method must have a return type.
The Java compiler provides a default constructor if The method is not provided by the
you don't have any constructor in a class. compiler in any case.
Unit-II Core Java
The constructor name must be same as the class The method name may or may not
name. be same as the class name.
o switch statement
2. Loop statements
o do while loop
o while loop
o for loop
3. Jump statements
o break statement
o continue statement
Decision making:
1) If Statement:
OR
The "if statement" will ensure the given condition, and if the
condition becomes true, then the block of statements enclosed
within curly braces will get executed; otherwise, not.
OR
if statement to specify a block of java code to be executed if a
condition is True.
Syntax
if (condition)
{
// block of code to be executed if the condition is True
}
Example :
Unit-II Core Java
int x = 20;
int y = 18;
if (x > y)
{
System.out.println ("x is greater than y");
}
Output: x is greater than y
Example explained
if….else statement
The “if statement” will ensure the given condition and if the
condition becomes true, then the block of statement enclosed
within curly braces will get executed; otherwise , the block of code
associated with the else will get executed .
Syntax :
if (condition)
{
True condition ;
}
Else
{
False condition;
}
Example:
Int x=20;
Int y=15;
if(x<y)
{
System.out.println (“x is greater than y”);
Unit-II Core Java
}
Else
{
System.out.println(“condition is false ”);
}
Output : condition is false
Switch Statement:
In Java, Switch statements are similar to if-else-if statements.
The switch statement contains multiple blocks of code called
cases and a single case is executed based on the variable which
is being switched. The switch statement is easier to use instead
of if-else-if statements. It also enhances the readability of the
program.
switch (expression)
{
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}
class switchstat
{
Public static void main (String args[])
Unit-II Core Java
{
int day=2;
Switch(day)
{
Case 1:
System.out.println (“monday”);
Break;
Case 2:
System.out.println (“Tuesday”);
Break;
Case 3:
System.out.println (“Wednesday”);
Break;
Case 4:
System.out.println (“Thursday”);
Break;
Case 5:
System.out.println(“monday”);
Break;
}
Unit-II Core Java
}
}
Output : Tuesday
Loop Statements
In programming, sometimes we need to execute the block of code
repeatedly while some condition evaluates to true. However, loop
statements are used to execute the set of instructions in a repeated
order. The execution of the set of instructions depends upon a
particular condition.
1. for loop
2. while loop
3. do-while loop
In for loop, if the given condition is true body of the program will
executed until the condition becomes false and terminate the
Unit-II Core Java
Syntax:
}
}
Output:
While loop:
Unit-II Core Java
The code in the loop will run, over & over again as long as a
variable (i) is less than 5.
Note: Do not forget to increase the variable used in the condition,
otherwise the loop will never end.
Do while loop:
The do while loop is a variant of the while loop. The loop will
execute the code block once. Before checking if the condition is
true, then, it will repeat the loop as long as the condition is true.
Do while loop is executed at least once because condition is
checked after loop body.
Do while loop is used to iterate a part of the program several
times. If the number of iteration is not fixed and you must have to
execute the loop at least once, it is recommended to use do-while
loop.
Syntax:
do
{
// block of code to be executed at least once.
// Increment of variable;
Unit-II Core Java
} while(condition);
Array:
Normally, an array is a collection of similar type of elements
which has contiguous memory location.
on.
Advantages
Disadvantages
dataType[] arr;
OR
dataType []arr;
Unit-II Core Java
OR
dataType arr[];
arrayRefVar=new datatype[size];
class Testarray
{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
OURPUT :
Unit-II Core Java
10
20
30
40
50
dataType[][] arrayRefVar;
OR
dataType [][]arrayRefVar;
OR
dataType arrayRefVar[][];
dataType []arrayRefVar[];
1. arr[0][0]=1;
2. arr[0][1]=2;
3. arr[0][2]=3;
4. arr[1][0]=4;
5. arr[1][1]=5;
6. arr[1][2]=6;
7. arr[2][0]=7;
8. arr[2][1]=8;
9. arr[2][2]=9;
System.out.println();
}
}
}
Test it Now
Output:
123
245
445
Inheritance:
Inheritance in Java is a mechanism in which one object acquires all
the properties and behaviors of a parent object. It is an important part
of OOPs (Object Oriented programming system).
The extends keyword indicates that you are making a new class that
derives from an existing class. The meaning of "extends" is to
increase the functionality.
Types of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance ()
4. Hierarchical inheritance
5. Hybrid inheritance
Single Inheritance :
When a class inherits another class, it is known as a single inheritance.
In the example given below, Dog class inherits the Animal class, so
there is the single inheritance.
Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel
inheritance. As you can see in the example given below, BabyDog
class inherits the Dog class which again inherits the Animal class, so
Unit-II Core Java
Since compile-time errors are better than runtime errors, Java renders
compile-time error if you inherit 2 classes. So whether you have same
method or different, there will be compile time error.
class A
{
void msg()
{
System.out.println("Hello");
}
}
class B
{
void msg()
{
System.out.println("Welcome");
}
}
class C extends A,B
{ //suppose if it were
}
}
Output:
Interface :
In other words, you can say that interfaces can have abstract
methods and variables. It cannot have a method body.
Syntax:
interface <interface_name>
Example of Interface :
interface Drawable{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable
{
public void draw()
{
System.out.println("drawing rectangle");
}
}
class Circle implements Drawable
{
Unit-II Core Java