Question

In: Computer Science

Java Questions involving OOP: (Include java coding examples if possible) (1) What is an exception type...

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?

Solutions

Expert Solution

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

Related Solutions

1.Explain to a layperson how medical coding works. What is the purpose of medical coding? Include...
1.Explain to a layperson how medical coding works. What is the purpose of medical coding? Include CPT codes, HCPCS, and ICD-10 codes. 2. explain medical coding and billing to a person not in the healthcare field
java script coding to create a math test program with 5 questions.
java script coding to create a math test program with 5 questions.
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of...
Give examples showing how "super" and "this" are useful with inheritance in Java. Include examples of using "super" and "this" both as constructors and as variables.
Give examples showing how "super" and "this" are useful with inheritance in Java. **Include examples of...
Give examples showing how "super" and "this" are useful with inheritance in Java. **Include examples of using "super" and "this" both as constructors and as variables.**
Answer the following questions: What strengths do you have as an innovator? Include examples. What areas...
Answer the following questions: What strengths do you have as an innovator? Include examples. What areas could use improvement/strengthening? Include examples. List at least 3 actions you will take to improve upon the weaknesses you identified above.
Type a 1 page notes or information on Types of RNA- Particularly Non Coding RNA. Type...
Type a 1 page notes or information on Types of RNA- Particularly Non Coding RNA. Type do not write the notes.
CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create...
CREATE A NEW Java PROGRAM that demonstrates : 1.1 numeric and 1 string exception 2. Create your own user defined exception, COMMENT it well, and add code so that it is thrown.
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
Locate a news article that depicts an ethical dilemma in the workplace. Possible examples may include...
Locate a news article that depicts an ethical dilemma in the workplace. Possible examples may include the following: Employees facing backlash at work for social media posts A business discriminating against a customer An employer with controversial policies You are more than welcome to explore beyond this list of topics to find an issue or example that interests you. Only Answer the following: Find an article. Relate the situation to at least 1 of the ethical theories from this course....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT