You are on page 1of 8

JAVA ASSIGNMENT(3)

NAME-ASHTOSH KUMAR PANDEY


ROLL-18/BCS/017

Q1. Explain inheritance, its type with programming example?

Sol:-Terms used in Inheritance


1.Class: A class is a group of objects which have common properties. It is a template or
blueprint from which objects are created.

2. Sub Class/Child Class: Subclass is a class which inherits the other class. It is also
called a derived class, extended class, or child class

3.Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.

4.Reusability: As the name specifies, reusability is a mechanism which facilitates you to


reuse the fields and methods of the existing class when you create a new class. You can
use the same fields and methods already defined in the previous class.

The syntax of Java Inheritance


class Subclass-name extends Superclass-name
{
Methord and members
}  
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.

Java Inheritance Example

class Employee{  
 float salary=40000;  
}  
class Programmer extends Employee{  
 int bonus=10000;  
 public static void main(String args[]){  
  Programmer p=new Programmer();  
   System.out.println("Programmer salary is:"+p.salary);  
   System.out.println("Bonus of Programmer is:"+p.bonus);  
}  }  
Types of inheritance in java
On the basis of class, there can be three types of inheritance in java: single, multilevel
and hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface


only.
When one class inherits multiple classes, it is known as multiple inheritance. For
Example:

Example to implement inheritance


1. class Animal{  
2. void eat(){System.out.println("eating...");}  
3. }  
4. class Dog extends Animal{  
5. void bark(){System.out.println("barking...");}  
6. }  
7. class Cat extends Animal{  
8. void meow(){System.out.println("meowing...");}  
9. }  
10. class TestInheritance3{  
11. public static void main(String args[]){  
12. Cat c=new Cat();  
13. c.meow();  
14. c.eat();  
15. //c.bark();//C.T.Error  
16. }}  

 Q2. Explain exception handling in java with the help of


example(program).

Exception Handling
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.
Advantage of Exception Handling
The core advantage of exception handling is to maintain the normal flow of the
application. An exception normally disrupts the normal flow of the application that is
why we use exception handling. Let's take a scenario:

Types of Java Exceptions


There are mainly two types of exceptions: checked and unchecked. Here, an error is
considered as the unchecked exception. According to Oracle, there are three types of
exceptions:

1. Checked Exception
2. Unchecked Exception
3. Error

Difference between Checked and Unchecked Exceptions


1) Checked Exception

The classes which directly inherit Throwable class except RuntimeException and Error are
known as checked exceptions e.g. IOException, SQLException etc. Checked exceptions
are checked at compile-time.

2) Unchecked Exception

The classes which inherit RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Unchecked exceptions are not checked at compile-time, but they are checked at
runtime.

Java Exception Keywords


There are 5 keywords which are used in handling exceptions in Java.

Keywor Description
d

try The "try" keyword is used to specify a block where we should place exception code. The try block must be
followed by either catch or finally. It means, we can't use try block alone.

catch The "catch" block is used to handle the exception. It must be preceded by try block which means we can't use
catch block alone. It can be followed by finally block later.

finally The "finally" block is used to execute the important code of the program. It is executed whether an exception is
handled or not.

throw The "throw" keyword is used to throw an exception.


throws The "throws" keyword is used to declare exceptions. It doesn't throw an exception. It specifies that there may
occur an exception in the method. It is always used with method signature.

Java Exception Handling Example


Let's see an example of Java Exception Handling where we using a try-catch statement
to handle the exception.

public class JavaExceptionExample{  
  public static void main(String args[]){  
   try{  
      //code that may raise exception  
     int data=100/0;  
   }catch(ArithmeticException e){System.out.println(e);}  
   //rest code of the program   
   System.out.println("rest of the code...");    }  
}  

Q3. Explain applet and its life cycle with example (program)

Java Applet
Applet is a special type of program that is embedded in the webpage to generate the
dynamic content. It runs inside the browser and works at client side.

Advantage of Applet
1. There are many advantages of applet. They are as follows:
2. It works at client side so less response time.
3. Secured
4. It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc.

Drawback of Applet
o Plugin is required at client browser to execute applet.

Lifecycle of Java Applet


1. Applet is initialized.
2. Applet is started.
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
Simple example of Applet by html file:
To execute the applet by html file, create an applet and compile it. After that create an
html file and place the applet code in html file. Now click the html file.

1. import java.applet.Applet;  
2. import java.awt.Graphics;  
3. public class First extends Applet{   
4. public void paint(Graphics g){  
5. g.drawString("welcome",150,150);  }    }  

Q4. Explain multithreading with the help of example( program).

Multithreading in Java
Multithreading in Java is a process of executing multiple threads simultaneously.

A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing


and multithreading, both are used to achieve multitasking.

However, we use multithreading than multiprocessing because threads use a shared


memory area. They don't allocate separate memory area so saves memory, and context-
switching between the threads takes less time than process.

What is Thread in java


A thread is a lightweight subprocess, the smallest unit of processing. It is a separate
path of execution.Threads are independent. If there occurs exception in one thread, it
doesn't affect other threads. It uses a shared memory area.
As shown in the above figure, a thread is executed inside the process. There is context-
switching between the threads. There can be multiple processes inside the OS, and one
process can have multiple threads.

Java thread class


Programig. Thread class provides constructors and methods to create and perform
operations on a thread. Thread class extends Object class and implements Runnable
interface

Example of thread
1. class Multi3 implements Runnable{  
2. public void run(){  
3. System.out.println("thread is running...");  
4. }    
5. public static void main(String args[]){  
6. Multi3 m1=new Multi3();  
7. Thread t1 =new Thread(m1);  
8. t1.start();  
9.  }  
10. }  

Q5.  Explain access modifiers in java?

Access Modifiers in Java


There are two types of modifiers in Java: access modifiers and non-access
modifiers.

The access modifiers in Java specifies the accessibility or scope of a field, method,
constructor, or class. We can change the access level of fields, constructors, methods,
and class by applying the access modifier on it.

There are four types of Java access modifiers:


1. Private: The access level of a private modifier is only within the class. It cannot
be accessed from outside the class.
2. Default: The access level of a default modifier is only within the package. It
cannot be accessed from outside the package. If you do not specify any access
level, it will be the default.
3. Protected: The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child class, it
cannot be accessed from outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed
from within the class, outside the class, within the package and outside the
package.

There are many non-access modifiers, such as static, abstract, synchronized, native,
volatile, transient, etc. Here, we are going to learn the access modifiers only.

You might also like