java unit 3 tt2
java unit 3 tt2
4. **No Need for Global Error Checks**: You don't need to explicitly check
for errors after every function call or operation, reducing clutter in the code
and avoiding the need for cascading error codes.
Ans= n Java, access specifiers are used to defining the visibility and
accessibility of class members such as variables, methods, and inner
classes. Java has four access specifiers: public, private, protected, and
default (also known as package-private).
1) Private
In this example, we have created two classes A and Simple. A class contains
private data member and private method. We are accessing these private
members from outside the class, so there is a compile-time error.
1. class A{
2. private int data=40;
3. private void msg(){System.out.println("Hello java");}
4. }
5.
6. public class Simple{
7. public static void main(String args[]){
8. A obj=new A();
9. System.out.println(obj.data);//Compile Time Error
10. obj.msg();//Compile Time Error
11. }
12. }
2) Default
If you don't use any modifier, it is treated as default by default. The default
modifier is accessible only within package. It cannot be accessed from
outside the package. It provides more accessibility than private. But, it is
more restrictive than protected, and public.
1. //save by A.java
2. package pack;
3. class A{
4. void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4. class B{
5. public static void main(String args[]){
6. A obj = new A();//Compile Time Error
7. obj.msg();//Compile Time Error
8. }
9. }
3) Protected
The protected access modifier can be applied on the data member, method
and constructor. It can't be applied on the class.
1. //save by A.java
2. package pack;
3. public class A{
4. protected void msg(){System.out.println("Hello");}
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4.
5. class B extends A{
6. public static void main(String args[]){
7. B obj = new B();
8. obj.msg();
9. }
10. }
Output:Hello
4) Public
1. //save by A.java
2.
3. package pack;
4. public class A{
5. public void msg(){System.out.println("Hello");}
6. }
1. //save by B.java
2.
3. package mypack;
4. import pack.*;
5.
6. class B{
7. public static void main(String args[]){
8. A obj = new A();
9. obj.msg();
10. }
11. }
Output:Hello
Que-3- Develop a java program to create a Animal interface with a method called
bark() that takes no arguments and returns void. Create a Dog class that
implements Animal and overrides bark() to print “Dog is barking” (10 Marks)
Ans=
ava Code:
// Animal.java
// Interface Animal
void bark();
Copy
// Dog.java
// Class Dog
@Override
System.out.println("Dog is barking!");
Copy
// Main.java
// Class Main
dog.bark();
Copy
Sample Output:
Dog is barking!
Ans=
Total Abstraction
Multiple Inheritance
Loose-Coupling
So the Syntax of an Interface in Java is written as shown below.
Interface <Interface Name> {
//Declare Constant Fields;
//Declare Methods;
//Default Methods;
}
// interface
interface Animal {
public void animalSound(); // interface method (does
not have a body)
public void sleep(); // interface method (does not
have a body)
}
// Pig "implements" the Animal interface
class Pig implements Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
public void sleep() {
// The body of sleep() is provided here
System.out.println("Zzz");
}
}
class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
Output-
In this tutorial, we will learn about Java exceptions, it's types, and the
difference between checked and unchecked exceptions.
Que-7- Write a java program for linear search handling following exception (10
marks)
A) ArrayIndexOutOfBounds=
B)Input Mismatch=
Ans= Java finally block is a block used to execute important code such as
closing the connection, etc.
1. class TestFinallyBlock {
2. public static void main(String args[]){
3. try{
4. //below code do not throw any exception
5. int data=25/5;
6. System.out.println(data);
7. }
8. //catch won't be executed
9. catch(NullPointerException e){
10. System.out.println(e);
11. }
12. //executed regardless of exception occurred or not
13. finally {
14. System.out.println("finally block is always executed");
15. }
16.
17. System.out.println("rest of phe code...");
18. }
19. }
Que-10- Explain try and catch block with example (10 marks)
Java try block is used to enclose the code that might throw an exception. It must
be used within the method.
If an exception occurs at the particular statement in the try block, the rest of the
block code will not execute. So, it is recommended not to keep the code in try
block that will not throw an exception.
1. try{
2. //code that may throw an exception
3. }catch(Exception_class_Name ref){}
1. try{
2. //code that may throw an exception
3. }finally{}
Java catch block
Java catch block is used to handle the Exception by declaring the type of exception
within the parameter. The declared exception must be the parent class exception (
i.e., Exception) or the generated exception type. However, the good approach is to
declare the generated type of exception.