In: Computer Science
Java Questions involving OOP:
(Include java coding examples if possible)
(1) What is an exception type an instance of?
(2) What are the conditions when a class implements an interface?
(3) What is/are the conditions of creating an abstract class object from its derived concrete class?
Ans1) In programming there are chances when the program encounter the situation(error) which is unpredictable for program and its not an syntatic or logical error. So, programing langauges provides exception handling mechanism in order to handle unpredicatble error which may happen in future.
The whole code needs to be put in the try block and the after the exception is caught by the program, the relavant action which needs to be executed is placed in the catch block.
There are several types of exception, some are as follows:-
1. IOException :- While using file input/output stream related exception
2. SQLException :- While executing queries on database related to SQL syntax
3. DataAccessException :- Exception related to accessing data/database
4. ClassNotFoundException :- Thrown when the JVM can’t find a class it needs, because of a command-line error, a classpath issue, or a missing class file
5. InstantiationException :- Attempt to create an object of an abstract class or interface.
Code:-
class A{
public static void main(String args[])
{
int i;
try{
for(i=10; i<=0; i++)
System.out.println(100/i);
}
catch(ArithmeticException e ){
System.out.println("Divide by zero Exception");
}
}
}
Ans2 A interface is similar to class, but all the methods in the interface are abstract. Coding an interface is similar as writing a class. But a class describes the attributes and behaviors of an object and interface contains behaviors that a class implements. In simple words, interface only declares the methods and the class which is implementing the interface needs to define the methods.
Code:-
interface Calculator{
void add();
void sub();
void multiply();
void divide();
}
public class ABC implements Calculator{
int a=5,b=7;
public void add(){
System.out.println(a+b);
}
public void sub(){
System.out.println(a-b);
}
public void multiply(){
System.out.println(a*b);
}
public void divide(){
System.out.println(a/b);
}
}
Ans3)
When the class is declared along with the keyword abstract the class is known as abstract class. The abstract class can have abstracts methods and concrete methods. when ever the methods need to be abstract then abstract keyword is used along with the method declaration.
Let's take an example to understand more briefly about the abstract class. Lets assume there is class Animals, which have method sound(). The class need to be inherited by every animal, but the sound of each animal is different. So, the method sound() needs to be abstract method and the class which extends it will define the sound according to them.
CODE:-
//abstract parent class abstract class Animals{ //abstract method public abstract void sound(); } //Dog class extends Animal class public class Cat extends Animal{ public void sound(){ System.out.println("meow"); } public static void main(String args[]){ Animals obj = new Cat(); obj.sound(); } }