Sie sind auf Seite 1von 3

1.

Define and explain the following concepts with appropriate examples: a) Superclass-: A superclass is a class that has been extended by another class. It allows the extending class to inherit its state and behaviors. Also Known As: base class, parent class Example:
Imagine you define a Person class: public class Person { } A new class can be created by extending this class: public class Employee extends Person { } The Person class is said to be the superclass of the Employee class.

b) Subclass:- A subclass is a class that extends another class. The subclass inherits the state and
behaviors of the class it extends. Also Known As: derived class, child class Examples: Imagine you define a Person class: public class Person { } A new class can be created by extending the Person class. Here the Employee class is a subclass of the Person class: public class Employee extends Person { }

c) Inheritance:- Inheritance enables programmers to define an "is-a" relationship between a class


and a more specialized version of that class. For example, if a Student class extends a Person class,

then it should be the case that Student is-a Person. The subclass Student builds on the functionality of the super class Person. Examples: //superclass public class Person{ } //subclass public class Student extends Person{ } The student class extends the person class and therefore inherits its state and behaviors.

d) Exceptions:- While a program is running, it may encounter an error that prevents it from
continuing; such an error is called an exception.

Examples: A program tries to read a file that does not exist. A file not found exception would occur.

e) Stream classes:- In reading and writing binary streams the ability to read a file byte by byte was
explored using the FileInputStream and FileOutputStream classes. When the file being read is a text file character steams can be used instead of byte streams. Again, it is typically better to use buffered streams to perform I/O for files but in this article we look at the most basic I/O for character streams by reading and writing a file one character at a time. Note: This article looks at reading character data from a untitled.txt file. If you try this code then simply replace the name of the untitled.txt with the path and name of a text file on your computer. Remember it has to be just a text file. For example, a word document contains formatting characters that would not be read as Unicode characters.

2. Write a program to explain the Exception Handling mechanisms in Java using the keywords: try, catch and finally. Ans-: A Java exception is an object that describes an exceptional condition which has
occurred in a piece of code. Many things can lead to exceptions , including hardware failures, resource exhaustion and bugs in the program. When an exceptional event occurs in the Java program, an exception is thrown. The code that is responsible for doing something about the exception is called exception handler. The Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. We have tell the JVM what code to execute when certain exceptional condition occurs in the program. To do this we use try and catch keywords. Within a try block we will write a code in which exception may occur. This block of code is called guarded region. One or more catch clauses match a specific exception to a block of code that handles it. Here is the basic form of exception handling block.

try{ //block of code }catch ( ExceptionType1 ex1){ // exception handler for ExcpetionType1. } catch(ExceptionType2 ex2){ //Exception handler for ExcpetionType2. throw(e); }finally{ } Java Exception Type Hierarchy:. Throwable class is used represent all exceptional conditions. This is superclass of all Exception types. Two immediate subclass of Throwable class is Error and Exception. Exception is used for exceptional conditions that user programs should catch. Exception class is used to create our own custom exception. Just we have to create a subclass of Exception and we create our exceptional conditions in this class that user program to catch. Example for Exception Class public class Propagate { public static void main(String[] args) { String str="pramila"; Propagate p=new Propagate(); try{ String revStr=p.reverse(str); System.out.println(revStr); }catch(StringIndexOutOfBoundsException stEx){ System.out.println("Exception"+stEx.getMessage()); }catch(IndexOutOfBoundsException ie){ System.out.println("Exception"+ie.getMessage()); }finally{ System.out.println("Exception occured in finally block"); try{ System.out.println(" In finally block try block Value"+5/0); }catch(ArithmeticException ie){ System.out.println("Exception occured"+ie.getMessage()); } } } public String reverse(String str){ String reveString = ""; for(int i=str.length();i>0;i--){ reveString += str.charAt(i); } return reveString; }

} 3. Write a Java program demonstrating the usage of all the primitive / standard data types. Ans-:

Das könnte Ihnen auch gefallen