Java Second Internal Study Material March 2025
Java Second Internal Study Material March 2025
5 Marks
1. What is a class in Java and objects in java and how to create a class and its elements.
A class is a group of objects which have common properties. It is a template or blueprint from
which objects are created. It is a logical entity. It can't be physical.
o Fields
o Methods
o Constructors
o Blocks
o Nested class and interface
Example:
class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;
• class has multiple methods having same name but different in parameters, it is known
as Method Overloading
Example:
class Adder
{
static int add(int a,int b)
{
return a+b;
}
static int add(int a,int b,int c)
{
return a+b+c;}
}
class TestOverloading1
{
public static void main(String[] args)
{
System.out.println(Adder.add(10,12));
System.out.println(Adder.add(10,20,30));
}
}
3.. Method Overriding in Java
• If subclass (child class) has the same method as declared in the parent class, it is known
as method overriding in Java.Java
• If a subclass provides the specific implementation of the method that has been declared by
one of its parent class, it is known as method overriding.
• Method
od overriding is used to provide the specific implementation of a method which is
already provided by its super class.
• Method overriding is used for runtime polymorphism
class Bank{
int getRateOfInterest(){return
getRateOfInterest(){ 0;}
}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest(){return
getRateOfInterest(){ 8;}
}
Example :
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"Velu",5000f);
Student s2=new Student(112,"Mithran",6000f);
s1.display();
s2.display();
}}
Output:
Thread priority in Java is a number assigned to a thread that is used by Thread scheduler to
decide which thread should be allowed to execute.
In Java, each thread is assigned a different priority that will decide the order (preference) in which
it is scheduled for running.
Thread priorities are represented by a number from 1 to 10 that specifies the relative priority of
one thread to another. The thread with the highest priority is selected by the scheduler to be
executed first.
The default priority of a thread is 5. Thread class in Java also provides several priority constants
to define the priority of a thread. These are:
1. MIN_PRIORITY = 1
2. NORM_PRIORITY = 5
3. MAX_PRIORTY = 10
Let’s create a Java program in which we will determine the priority and name of the current
thread.
Program code:
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.
There are two ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will be accessible but not
subpackages.
The import keyword is used to make the classes and interface of another package accessible to the
current package.
Example of package that import the packagename.*
/save by A.java
package pack;
public class A
{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B
{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
2) Using packagename.classname
If you import package.classname then only declared class of this package will be accessible.
//save by A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.A;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Output:Hello
2. Life cycle of a Thread (Thread States)
Thread always exists in any one of the following states. These states are:
1. New
2. Active
3. Blocked / Waiting
4. Timed Waiting
5. Terminated
New: Whenever a new thread is created, it is always in the new state. For a thread in the new
state, the code has not been run yet and thus has not begun its execution.
Active: When a thread invokes the start() method, it moves from the new state to the active state.
The active state contains two states within it: one is runnable, and the other is running.
o Runnable: A thread, that is ready to run is then moved to the runnable state.
o In the runnable state, the thread may be running or may be ready to run at any
given instant of time.
o It is the duty of the thread scheduler to provide the thread time to run, i.e., moving
the thread the running state.
o A program implementing multithreading acquires a fixed slice of time to each
individual thread.
o Each and every thread runs for a short span of time and when that allocated time
slice is over, the thread voluntarily gives up the CPU to the other thread, so that the
other threads can also run for their slice of time
o Running: When the thread gets the CPU, it moves from the runnable to the running state.
Generally, the most common change in the state of a thread is from runnable to running
and again back to runnable.
Blocked or Waiting: Whenever a thread is inactive for a span of time (not permanently) then,
either the thread is in the blocked state or is in the waiting state.
Timed Waiting: Sometimes, waiting for leads to starvation. For example, a thread (its
name is A) has entered the critical section of a code and is not willing to leave that critical
section. In such a scenario, another thread (its name is B) has to wait forever, which leads
to starvation. To avoid such scenario, a timed waiting state is given to thread B. Thus,
thread lies in the waiting state for a specific span of time, and not forever.
Terminated: A thread reaches the termination state because of the following reasons:
o When a thread has finished its job, then it exists or terminates normally.
o Abnormal termination: It occurs when some unusual events such as an unhandled
exception or segmentation fault.
A terminated thread means the thread is no more in the system. In other
other words, the thread is dead,
and there is no way one can respawn (active after kill) the dead thread.
The following diagram shows the different states involved in the life cycle of a thread.
It represents the runnable state.It means a thread is waiting in the queue to run.
It represents the waiting state. A thread will go to this state when it invokes the Object.wait()
method, or Thread.join() method with no timeout. A thread in the waiting statesta is waiting for
another thread to complete its task.
It represents the timed waiting state. The main difference between waiting and timed waiting is
the time constraint. Waiting has no time constraint, whereas
whereas timed waiting has the time constraint.
3. Discuss final variables ,final methods and final class.
The final keyword in java is used to restrict the user. The java final keyword can be used in many
context. Final can be:
1. variable
2. method
3. class
The final keyword can be applied with the variables, a final variable that have no value it is called
blank final variable or uninitialized final variable. It can be initialized in the constructor only. The
blank final variable can be static also which will be initialized in the static block only. We will
have detailed learning of these. Let's first learn the basics of final keyword.
If you make any variable as final, you cannot change the value of final variable(It will be
constant).
There is a final variable speedlimit, we are going to change the value of this variable, but It can't
be changed because final variable once assigned a value can never be changed.
1. class Bike9{
2. final int speedlimit=90;//final variable
3. void run(){
4. speedlimit=400;
5. }
6. public static void main(String args[]){
7. Bike9 obj=new Bike9();
8. obj.run();
9. }
10. }//end of class
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods
in the Java interface, not method body. It is used to achieve abstraction and multiple inheritance in
Java.
In other words, you can say that interfaces can have abstract methods and variables. It cannot have
a method body.
There are mainly three reasons to use interface. They are given below.
Syntax:
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}
As shown in the figure given below, a class extends another class, an interface extends another
interface, but a class implements an interface.
Example:
interface Drawable{
void draw();
}
//Implementation: by second user
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class Circle implements Drawable{
public void draw(){System.out.println("drawing circle");}
}
//Using interface: by third user
class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()
d.draw();
}}
Output:
drawing circle
5. Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known
as multiple inheritance.
Example:
interface Printable{
void print();
}
interface Showable{
void show();
}
class A7 implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}