Java – Exception handling

Java  – Exception handling

Exception handling separates the normal processing from the “anomalous” processing. If an error occurs an exception can be thrown to notify that there is a problem, catches the exception and act appropriately, it can log, exit or interact with the user. You must use a try block to catch any exceptions. syntax: try{ statements that may throw an exception } catch (Exception e){ statements that handle the exception. }finally{ statements that will always run regardless of throwing an exception or not } to catch every possible type of error use:…

Java – Exception handling Read More

Java – Abstract classes & methods

Java – Abstract classes & methods

Abstract class is a class where its implementation is incomplete or non-existent. Abstract class cannot be instantiated but can be extended. A subclass that implements an abstract class can be instantiated. Abstract class is similar to interface but also have some differences. Both cannot be instantiated and both can contain methods without implementation. The differences are: a class can extend only one class but can implement many interfaces separating them with “,” An abstract class can define fields that are not static nor final. An interface all fields must be…

Java – Abstract classes & methods Read More

Java – Interface

Java – Interface

Interface in Java is a collection of methods that are not implemented and they have to be implemented. It acts as a form for methods. An interface should be stored in .java file, and should start with capital letter, like classes. It does not have constructors or method bodies, just method signatures and fields. syntax: public interface InterfaceNAme{ field(s) method signature(s) } Then you implement it. First add implement and interface name at the class signature: Now you can see a red line that reminds you that you have to…

Java – Interface Read More

Java – class inheritance | superclass and subclass

Java – class inheritance | superclass and subclass

In Java a class can inherit another class. The inherited class is called Superclass and the class that inherits another class it is called subclass. syntax: puplic class subclassName extends superclassName{ additional fields and methods or method overriding } The subclass should only declare new fields, implement new methods or override superclass methods. All the variables and methods of the superclass are inherited automatically. As you see on the example above, the subclass’s constructor called the superclass constructor using the super keyword. This is important. The subclass must initialize the…

Java – class inheritance | superclass and subclass Read More

Java – Generics

Java – Generics

A generic class/method is the class/method that declares one or more type variables. With generic classes/method we skip the creation of multiple classes/method because of different type variables Generic Method Example One generic method instead overloading a method like this below: Generic Class Example: Restrictions: You cannot instantiate primitives, you should use their wrapper class. ex: use Integer instead of int You cannot declare static fields whose types are type parameters You cannot create new instances of type parameters. Diamond parameter <>: You can just use an empty pair of…

Java – Generics Read More