Java Unit Test-1
Java Unit Test-1
Object
Class
Inheritance
Polymorphism
Abstraction
Encapsulation
CLASS
Introduction to Classes
A class is a user-defined data type. It consists of data members
and member functions, which can be accessed and used by
creating an instance of that class.
A class is like a Blueprint (or) logical entity for an object.
Before we create an object, we first need to define the
class.
Class does not occupy memory.
Type of Classes
In Java classes are mainly divided into two types.
Those are:
1. Built-in Classes
2. User-Defined Classes
1. Built-in Classes
Built-in classes are just the predefined classes that
come along with the Java Development Kit (JDK). Some
examples of built-in classes include:
java.lang.System
java.util.Date
java.util.ArrayList
java.lang.Thread
User-Defined Classes
User-Defined Classes are defined by the user
according to their requirement.
4
A class in Java consists of the following components
1. Access Modifier
2. Class Name
3. Body of the Class
1. Access Modifier:
access modifiers specify the accessibility or scope of the class.
Java provides four types of access modifiers. Those are
a) Public : Access from any where
b) Private : Access with in the class where it is defined
c) Protected: Access with in same package and sub classes of
another package
d) Default: Access with in the same package only
2. Class Name
This describes the name given to the class.
3. Body of the Class
The body of the class mainly includes executable statements.
Note:
Apart from these, a class may include keywords like
"super" 5,”extends”, ”implements”.
User-defined classes are broken down into three types:
1. Concrete Class
2. Abstract Class
3. Interfaces
1.Concrete Class
A Concrete class is a standard class that the user
defines and stores the methods and data members in.
Rules for Creating Classes:
1. The keyword "class" must be used to declare a class.
2. Every class name should start with an upper case
character. If it include multiple words first letter of each
word must be in capital letter(camel case).
3. A Java project can contain any number of default classes
but should not hold more than one public class
General form of a class is as follows:
6
Syntax:
class Classname
{
datatype variable1;
datatype variable2;
………..
datatype variablen;
type method1(parameterlist)
{
statements;
}
type method2(parameterlist)
{
statements;
}
……..
} 7
Example 1:
class Example
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Save: Example.java
Compilation: javac Example.java
Run: java Example
Output:
Hello World!
8
Object
An object is called an instance of a
class. Object means a real-world entity such as a pen, chair,
table, computer, watch, etc.
State:
represents the data (value) of an object.
Behavior:
represents the behavior (functionality) of an object such
as deposit, withdraw, etc.
9
There are 3 ways to initialize object in Java.
1. By reference variable
2. By method
3. By constructor
1. By reference variable
class Student{
int id=200;
String name=“VITS”;
public static void main(String args[]){
Student s1=new Student();
System.out.println(s1.id+" "+s1.name);
}
}
10
Inheritance
Inheritance is a process where child class acquired all the properties
and behaviors of the parent class.
It helps to reuse the code and establish a relationship between different
classes.
Here parent class also called a super class(Base class) and child class
called as a subclass(Derived class).
Example:
In phone call we don’t know how internally work
In ATM we don’t know how internally work.
30
The solution is the “this” keyword Append both ‘a’ and ‘b’
with the Java this keyword followed by a dot (.) operator.
32
Example:
class ThisExample {
int x, y; //instance variables
void display(int x, int y) { // x,y are arguments
this.x = x+100;
this.y = y+100;
System.out.println(“the instance variable x value is”+this.x);
System.out.println(“the instance variable y value is”+this.y);
System.out.println(“the display() local variable x value is”+x);
System.out.println(“the display() local variable y value is”+y);
}
public static void main(String args[]) {
ThisExample te=new ThisExample();
te.dispay(10,20);
}
}
33
4) a) Describe the following statements with example
i) While ii) for
b) Write a Java program factorial of a given number using
recursion.
while statement:
While statement is used for repetitive execution of statements
more than once.
Syntax: Flowchart
while (condition)
{
------
Block of the statements
------
}
First the condition is evaluated. If the condition is TRUE, then Block
Statements will be executed by the compiler. After executing the Block
Statements, again control reaches to condition section and is evaluated.
The process is repeated until the condition becomes FALSE.
When the condition reaches to FALSE, then the control is transferred out of
the loop.
Example:
class Demo{
public static void main(String args[]) {
int i = 1;
while( i <=10 ) {
System.out.print(i + ” “);
i++;
}
}
}
Output: 1 2 3 4 5 6 7 8 9 10
36
iii) For statement:
For statement is also used for repetitive execution of
same statements more than once.
Syntax:
for(Initialization ; Condition ; Increment/Decrement){
/* Block Statements */
}
Here,
The Initialization initializes and/or declares variables and executes only
once.
The condition is evaluated. If the condition is true, the body of
the for loop is executed.
The Increment/Decrement updates the value of variable.
The condition is evaluated again. The process continues until
the condition
37
is false
Example:
}
Output: 1 2 3 4 5 6 7 8 9 10
38
// Factorial of a given number using recursion
class Factorial {
static long factorial(long n) {
if (n == 1)
return 1;
else
return n * factorial(n - 1);
}
public static void main(String[] args) {
System.out.println("Factorial of the number 6 is: " + factorial(6));
}
}
Output:
Factorial of the number 6 is: 720