0% found this document useful (0 votes)
2 views

Java Second Internal Study Material March 2025

The document provides study material for a Java second internal exam, covering key concepts such as classes, method overloading, method overriding, the 'this' keyword, thread priority, Java packages, thread lifecycle, final variables/methods/classes, interfaces, and multiple inheritance in Java. Each topic is explained with definitions, examples, and code snippets to illustrate the concepts. The material is structured to help students understand and prepare for their exam effectively.

Uploaded by

john8012karthik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Second Internal Study Material March 2025

The document provides study material for a Java second internal exam, covering key concepts such as classes, method overloading, method overriding, the 'this' keyword, thread priority, Java packages, thread lifecycle, final variables/methods/classes, interfaces, and multiple inheritance in Java. Each topic is explained with definitions, examples, and code snippets to illustrate the concepts. The material is structured to help students understand and prepare for their exam effectively.

Uploaded by

john8012karthik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

JAVA SECOND INTERNAL EXAM STUDY MATERIAL

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.

A class in Java can contain:

o Fields
o Methods
o Constructors
o Blocks
o Nested class and interface

Class Declaration in Java


access_modifier class <class_name>
{
data member;
method;
constructor;
nested class;
interface;
}

Example:

class Student {
// data member (also instance variable)
int id;
// data member (also instance variable)
String name;

public static void main(String args[])


{
// creating an object of
// Student
Student s1 = new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
2. Method Overloading in java

• class has multiple methods having same name but different in parameters, it is known
as Method Overloading

There are two ways to overload the method in java

• By changing number of arguments


• By changing the data type

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;}
}

class ICICI extends Bank{


int getRateOfInterest(){return
getRateOfInterest(){ 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return
getRateOfInterest(){ 9;}
}
//Test class to create objects and call the methods
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI
"SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI
"ICICI Rate of Interest: "+i.getRateOfInterest());
+i.getRateOfInterest());
System.out.println("AXIS
"AXIS Rate of Interest: "+a.getRateOfInterest());
+a.getRateOfInterest());
}
}
4. Short note on this keyword in java
There can be a lot of usage of Java this keyword.

In Java, this is a reference variable that refers to the current object.

Usage of Java this keyword

Here is given the 6 usage of java this keyword.

1. this can be used to refer current class instance variable.


2. this can be used to invoke current class method (implicitly)
3. this() can be used to invoke current class constructor.
4. this can be passed as an argument in the method call.
5. this can be passed as argument in the constructor call.
6. this can be used to return the current class instance from the method.

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:

111 Velu 5000.0


112 Mithran 6000.0
5. Discuss about Thread Priority

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:

public class A implements Runnable


{
public void run()
{
System.out.println(Thread.currentThread()); // This method is static.
}
public static void main(String[] args)
{
A a = new A();
Thread t = new Thread(a, "NewThread");

System.out.println("Priority of Thread: " +t.getPriority());


System.out.println("Name of Thread: " +t.getName());
t.start();
}
}
Output:
Priority of Thread: 5
Name of Thread: NewThread
Thread[NewThread,5,main]
10- Marks
1. Explain Java Package
A java package is a group of similar types of classes, interfaces and sub-packages.

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.

Advantage of Java Package

1) Java package is used to categorize the classes and interfaces so that they can be easily
maintained.

2) Java package provides access protection.

3) Java package removes naming collision.

How to access package from another package?

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.

Example of package by import package.classname

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

Explanation of Different Thread States

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.

Implementation of Thread States


In Java, one can get the current state of a thread using the Thread.getState() method.
The java.lang.Thread.State class of Java provides the constants ENUM to represent the state of a
thread. These constants are:

public static final Thread.State NEW

It represents the first state of a thread that is the NEW state.

public static final Thread.State RUNNABLE

It represents the runnable state.It means a thread is waiting in the queue to run.

public static final Thread.State BLOCKED

It represents the blocked state. In this state, the


the thread is waiting to acquire a lock.

public static final Thread.State WAITING

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.

public static final Thread.State TIMED_WAITING

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.

1) Java final variable

If you make any variable as final, you cannot change the value of final variable(It will be
constant).

Example of final variable

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

Output:Compile Time Error

2) Java final method


If you make any method as final, you cannot override it.

Example of final method


1. class Bike{
2. final void run(){System.out.println("running");}
3. }
4.
5. class Honda extends Bike{
6. void run(){System.out.println("running safely with 100kmph");}
7.
8. public static void main(String args[]){
9. Honda honda= new Honda();
10. honda.run();
11. }
12. }

Output:Compile Time Error

3) Java final class


If you make any class as final, you cannot extend it.

Example of final class


1. final class Bike{}
2.
3. class Honda1 extends Bike{
4. void run(){System.out.println("running safely with 100kmph");}
5.
6. public static void main(String args[]){
7. Honda1 honda= new Honda1();
8. honda.run();
9. }
10. }
11.
Output:Compile Time Error
4.. Explain an Interface in java

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.

Java Interface also represents the IS-A relationship.

There are mainly three reasons to use interface. They are given below.

o It is used to achieve abstraction.


o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling.

How to declare an interface?

o An interface is declared by using the interface keyword. It provides total abstraction;


means all the methods in an interface are declared with the empty body, and all the fields
are public, static and final by default. A class that implements an interface must implement
all the methods declared in the interface.

Syntax:
interface <interface_name>
{
// declare constant fields
// declare methods that abstract
// by default.
}

The relationship between classes and interfaces

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");}

public static void main(String args[]){


A7 obj = new A7();
obj.print();
obj.show();
}
}
Output:Hello
Welcome

You might also like