Posted on Leave a comment

object oriented – Does composition increase or decrease the coupling – Software Engineering Stack Exchange

To keep it simple, and as a rule of thumb (and not as laws graved in the marble), for lose coupling:

  • prefer composition over inheritance, because inheritance is always a strong coupling (any change in the parent class might require a change in all the child classes) and furthermore, it’s defined at compile time.
  • use interface segregation for the type you refer to, in order not to have a dependency on something you shouldn’t need to care about.
  • inject dependencies, in order to avoid implicit dependencies to specific constructors of specific types

Source: object oriented – Does composition increase or decrease the coupling – Software Engineering Stack Exchange

Posted on Leave a comment

Difference Between throw, throws and Throwable In Java

What is the difference between throw, throws and Throwable in java?
This is another most confusing java interview question asked to java freshers. Interviewer ask this type of questions to confuse the candidate because all three spell similar. But, all three serve different purpose in java. In this article, we will see the differences between throw<>, throws<> and Throwable<> in java.
throw In Java :
throw is a keyword in java which is used to throw an exception manually. Using throw keyword, you can throw an exception from any method or block. But, that exception must be of type java.lang.Throwable class or it’s sub classes. Below example shows how to throw an exception using throw keyword.
class ThrowAndThrowsExample {
oid method() throws Exception
xception e = new Exception();

E throw e; //throwing an exception using ‘throw’ }
throws In Java :
throws is also a keyword in java which is used in the method signature to indicate that this method may throw mentioned exceptions. The caller to such methods must handle the mentioned exceptions either using try-catch blocks or using throws keyword. Below is the syntax for using throws keyword.
return_type method_name(parameter_list) throws exception_list { //some statements
Below is the example which shows how to use throws keyword.
class ThrowsExample {
oid methodOne() throws SQLException
/This method may throw SQLException }
void methodTwo() throws IOException

{ //This method may throw IOExceptio n}
d methodThree() throws ClassNotFoundException {
vo i //This method may throw ClassNotFoundException }
Throwable In Java :
Throwable is a super class for all types of errors and exceptions in java. This class is a member of java.lang package. Only instances of this class or it’s sub classes are thrown by the java virtual machine or by the throw statement. The only argument of catch block must be of this type or it’s sub classes. If you want to create your own customized exceptions, then your class must extend this class. Click here<> to see the hierarchy of exception classes in java.
Below example shows how to create customized exceptions by extending java.lang.Throwable class.
class MyException extends Throwable {
//Customized Exception class }

class ThrowAndThrowsExample {
{ MyException e = new MyExcep void method() throws MyExceptio ntion();
throw e; }
Sent from my iPhone

Posted on Leave a comment

throw and throws in Java – GeeksforGeeks

throw and throws in Java
The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exception<>. The throw keyword is mainly used to throw custom exceptions. Syntax:
throw Instance Example: throw new ArithmeticException(“/ by zero”);
But this exception i.e, Instance must be of type Throwable or a subclass of Throwable. For example Exception is a sub-class of Throwable and user defined exceptions typically extend Exception class<>. Unlike C++, data types such as int, char, floats or non-throwable classes cannot be used as exceptions.
The flow of execution of the program stops immediately after the throw statement is executed and the nearest enclosing tryblock is checked to see if it has a catchstatement that matches the type of exception. If it finds a match, controlled is transferred to that statement otherwise next enclosing try block is checked and so on. If no matching catch is found then the default exception handler will halt the program.
// Java program that demonstrates the use of throw class ThrowExcep { static void fun() { try { throw new NullPointerException(“demo”); } catch(NullPointerException e) { System.out.println(“Caught inside fun().”); throw e; // rethrowing the exception } }
public static void main(String args[]) { try { fun(); } catch(NullPointerException e) { System.out.println(“Caught in main.”); } } }
Caught inside fun(). Caught in main.
Sent from my iPhone

Posted on Leave a comment

unchecked exception – Google Search

Unchecked Exception in Java is those Exceptions whose handling is NOT verified during Compile time . These exceptionsoccurs because of bad programming. The program won’t give a compilation error. All Unchecked exceptions are direct sub classes of RuntimeException class.
Sent from my iPhone