Posts

Showing posts with the label Java

[Java] Interface

In Java, interfaces are a special reference type that has the following properties: May contain constants (variables that are implicitly public, static, and final). May contain method signatures, but no method bodies. Interfaces cannot be instantiated, but can be implemented. In other words, an interface provides a blueprint for its implementing classes. When a group of programmers work on a software, it's often useful to specify a contract that defines how different components of the software interact, so that the programmers can work on separate parts of the software concurrently. Interfaces serve perfectly for this purpose. To define an interface, we use the interface keyword: 1 2 3 4 5 6 7 8 public interface Animal { public void eatSomething (); public String makeSomeNoise (); public int getSize (); } Here, I just defined an Animal interface which has three methods. To implement this interface, we can use the implements keywor...

[Java] Exception Handling

There are three types of exceptions in Java: error, runtime exception, and checked exception. Only checked exception requires handling. What are these exceptions? Error: Error refers to system errors, bugs in development tools, etc. In other words, errors are external to the application and out of the programmer's control. Examples include disk corruption, hardware malfunction, and system out of memory. Recently my Eclipse has been refusing to compile my programs because it cannot find a certain file which comes with the software. It's a weird bug and every time I had to restart it to get it working. That is an example of an error. Runtime Exception: As the name suggests, runtime exception occurs at runtime, which means the compiler cannot catch it. Runtime exception typically indicates a bug in the code. Examples include NullPointerException, ArrayIndexOutOfBoundsException, etc. In other words, these are the exceptions that crash your program.  Runtime e...