Class v/s Type

2 minute read

Class of an object v/s #ype of object

Type -> Abstraction –> Interface

Interface: is the ability to describe a behavior or a contract between a client and provider without bothering about implementation symantics

Class: Blueprint of an object

  • codification of logic &
  • encapsulation of state
  • STATE + BEHAVIOUR

Runtime object will have both type and class

In java, first class support for abstraction like interface

**LHS = type RHS = Class**
List<Integer> list = new ArrayList<Integer>();

Key takeaway & basis for most GOF patterns

  • program to an interface, not an implementation
  • favor object composition over class inheritance
    • Better to do delegation using object composition rather than use class inheritance

Interface

  • 100% Abstraction - except default methods
  • only constants and abstract methods - except default methods
  • Intent : to specify behavior of object
  • CANNOT BE INSTANTIATED
  • All data fields are public final & static
  • All methods are public and abstract
  • Multiple inheritance is possible via Interfaces, can extend (implement) two interfaces in java
  • An interface can extend only an interface
  • Are used to create an API which can be implemented by different parties. - JPA

Interfaces are a good substitute for multiple inheritance

Abstract Class

  • Can’t be instantiated
  • public because need to be given out
  • still define its constructors => invoked in constructors of subclass.
  • 0 to 100% abstraction
  • eg: java.lang.Number class

You Program for Interfaces (interfaces as well as abstract class to provide abstraction) rather than implementation

Abstraction is applied in the process of identifying s/w artifacts to model the problem domain

The Chef analogy : Customer -> (Menu) + Waiter (can be a cook) -> Head chef -> {indian, chinese, Italian, Mexican} -» multiple cooks

  • any0one can leave from the junior chefs and new can come. Customers need not be worried

REAL AIM : FLEXIBILITY (code can be changed but the abstraction remains the same) and not hiding Segregation of code, free to change without affecting the business Constructor chaining <=> for inheritance

IT IS POLYMORPHISM WHICH LINKS ABSTRACT CODE TO CONCRETE IMPLEMENTATION.

List<String> stringList = new ArrayList<>();

Constructors

whenever an object of a class is created, implicitly default no-arg constructor of class and its superclass constructor is called.

Implicitly, the first statement of constructor is super().

4 OOPS principle

Encapsulation - encapsulation means Data Hiding + Abstraction

Abstraction - Abstraction means hiding the implementation,

Inheritance - Inheritance is a process where child class acquires the properties of super class, and (both interface and abstract class)

Polymorphism - Property of an object to take on different forms

  • Compile time polymorphism - using Method overloading by implements
    • Static polymorphism or Compile time polymorphism or early binding in Java is achieved by method overloading
  • Runtime polymorphism - Method overriding - by extends (using an object of type parent)