Abstract


OOP Class


  • An Abstraction over a group of similar items that share the same properties and actions

Inner Class

Anonymous Inner Class

Important

Anonymous classes are useful when you need to create a class that implements an OOP Interface or extends a class, but you don’t need to reuse the class elsewhere in your code.

OOP Object

  • An actual instance of OOP Class that occupies some space in the Heap Segment
  • The variable holds a memory pointer to the OOP Object, not the values inside the OOP Object

Abstract Class

abstract class Shape {
  public abstract double getArea();
}
  • A blueprint for other classes, thus can’t be used to create objects directly
  • It is useful when we only know certain properties

Properties

  • Can be a subclass of other concrete class
  • Can have fields
  • Can have abstract methods (doesn’t have an implementation). Subclasses must provide the implementation
  • Can have concrete methods
  • Not required to have abstract methods

OOP Interface


  • Defines a set of OOP Abstract Method that a OOP Class must implement if it chooses to implement that interface. It doesn’t provide any concrete implementations for these methods
  • Focus on what a class should do rather than how it should do it

Pure interface

An interface should only contain abstract methods. It should not hold any fields too!

Java Interface

  • All the methods inside are automatically public

Casting with Interfaces

You can cast an object to an interface type, even if its class doesn’t directly implement it. This is because a subclass might implement the interface.

If we create an instance with the interface as its data type, then we cast it to a data type that is not in a subtyping relationship, this may still work, but it is not recommended and can lead to runtime errors.

Interface Inheritance

Interfaces can also inherit from other interfaces.

In Java, an interface can extend zero or more interfaces.

Functional Interface