Week 9-10 Oop Note
Week 9-10 Oop Note
▪ Abstract class
▪ Interface
Abstract class in Java
In this example, Bike is an abstract class that contains only one abstract method run. Its implementation
is provided by the Honda class.
Understanding the real scenario of Abstract class
• Mostly, we don't know about the implementation class (which is hidden to the
end user), and an object of the implementation class is provided by the
factory method.
Understanding the real scenario of Abstract class
In this example, if you create the instance of Rectangle class, draw() method of Rectangle class will be
invoked.
Another example of Abstract class in java
Abstract class having constructor, data member and methods
An abstract class can have a data member, abstract method, method body (non- abstract method),
constructor, and even main() method.
• If there is an abstract method in a class, that class must be abstract.
• If you are extending an abstract class that has an abstract method, you must either provide the
implementation of the method or make this class abstract.
Interface in Java
• An interface in java is a blueprint of a class.
• It has static constants and abstract methods.
• The interface in Java is a mechanism to achieve abstraction.
• There can be only abstract methods in the Java interface, not method body. It is used to achieve
abstraction and multiple inheritance in Java.
• In other words, you can say that interfaces can have abstract methods and variables. It cannot
have a method body.
• Java Interface also represents the IS-A relationship.
• It cannot be instantiated just like the abstract class.
• Since Java 8, we can have default and static methods in an interface.
• Since Java 9, we can have private methods in an interface.
Why use Java interface?
It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the
fields are public, static and final by default.
A class that implements an interface must implement all the methods declared in the interface.
Syntax:
Java 8 Interface Improvement
◾ Since
Java 8, interface can have default and static
methods.
Internal addition by the compiler
The Java compiler adds public and abstract keywords before the interface method. Moreover, it adds public, static
and final keywords before data members.
In other words, Interface fields are public, static and final by default, and the methods are public and abstract.
The relationship between classes and interfaces
As shown in the figure given below, a class extends another class, an interface
extends another interface, but a class implements an interface.
Java Interface Example
In this example, the Printable interface has only one method, and its implementation is
provided in the A6 class.
Java Interface Example: Drawable
As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of
ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its
implementation is provided by the implementation class. For example:
try The "try" keyword is used to specify a block where we should place
exception code. The try block must be followed by either catch or finally. It
means, we can't use try block alone.
catch The "catch" block is used to handle the exception. It must be preceded by
try block which means we can't use catch block alone. It can be followed by
finally block later.
finally The "finally" block is used to execute the important code of the program. It
is executed whether an exception is handled or not.
• The finally block follows a try block or a catch block. A finally block of
code always executes, irrespective of occurrence of an Exception.
• You just need to extend the predefined Exception class to create your
own Exception. These are considered to be checked exceptions.
Let's see a simple example of Java custom exception. In the following code, constructor of InvalidAgeException takes a string as an argument. This string is
passed to constructor of parent class Exception using the super() method. Also the constructor of Exception class can be called without using a parameter
and calling super() method is not mandatory.
// main method
// class representing custom exception public static void main(String args[])
class InvalidAgeException extends Exception {
{ try
public InvalidAgeException (String str) {
{ // calling the method
// calling the constructor of parent Exception validate(13);
super(str); }
} } catch (InvalidAgeException ex)
// class that uses custom exception InvalidAgeException {
public class TestCustomException1 System.out.println("Caught the exception");
{
// method to check the age // printing the message from InvalidAgeException object
static void validate (int age) throws InvalidAgeException{ System.out.println("Exception occured: " + ex);
if(age < 18){ }
// throw an object of user defined exception
throw new InvalidAgeException("age is not valid to vote"); System.out.println("rest of the code...");
} }
else { }
System.out.println("welcome to vote");
}
}