OOP Fundamentals (3 of 3)

Polymorphism is the third essential feature of OOP.

Polymorphism means many forms – an object can behave in different forms and the correct behavior will be evaluated during run-time only.

Upcasting

Taking the concept of inheritance further,  an object of derived class can be used as its own type or as a type of its base class. Although this statement might appear as incorrect – why would Java allow one type to behave like another type, but if we think in a object oriented manner it will make sense.

Inheritance is making a base class (generic) into a more specific derived class. So, derived class object still posses all the generic behavior of base class. By doing upcasting we are loosing specifics of a derived class but retaining generics of base class.

Let’s take this example

upcast

Output :

Car:Move

Although this is the desired output, but looking at the method park() it doesn’t make sense. In the park() method we are invoking move() of Vehicle but actually move() of Car is invoked.

Dynamic Binding

Connecting a method call to method body is called binding. When this binding happens during compile time, its called static binding. When this binding happens during run time (based on type of object), its called dynamic binding or runtime binding.

In the above example, even when Car object is upcasted to Vehicle in park(), it still knows its type information and behave in polymorphic manner to produce desired output.

Downcasting is opposite of upcasting. Here we cast a generic object into more specific one. While Upcasting is always safe and happens implicitly, Downcasting requires explicit type casting operator and throws a runtime ClassCastException if the instance to be downcasted does not belong to the correct subclass.

downcast

 

 

OOP Fundamentals (2 of 3)

Object Oriented Programming Fundamentals

An important aspect of OOP is reuse of classes using composition and inheritance.

Composition is using objects of existing classes into a new class. It represents ‘has-a‘ relationship. e.g. car has-a engine.

composition
Composition (has-a)

Inheritance is creating new classes as type of existing classes. It represents ‘is-a‘ relationship. In Java code, after specifying the name of derived (new) class use the keyword extends followed by name of base (existing) class .

With inheritance derived class gets all the public & protected attributes and methods of base class. There are two more access specifier (other than private and public) –

  • Protected – Like private, protected  entity is accessible to only the Class which defines it. Unlike private, protected entity of base class is available in the derived class.
  • Package – This is the default access where entity is accessible within the package but is private out of package.
inheritance
Inheritance (is-a)

The derived class may add more attribute & methods to the base class. Internally derived class has a object of base class but externally it exposes an extended interface of base class. So,  derived class is a wrapper over base class with some more specific functionality.

Initialization

Since base class is wrapped inside derived class, it becomes responsibility of derived class to do initialization of base class. Java helps here by calling the constructor of base class from the constructor of derived class, but this automatic help is available only in case of default constructors.

When dealing with constructors having arguments, the derived-class constructor needs to explicitly invoke the base-class constructor using super keyword. For example:

Class Furniture {

  Furniture(int i) {

    print(“Creating Furniture”);

  }

}

Class Table extends Furniture {

  Table(int i) {

    super (i);

    print(“Creating Table”);

  }

}

In the above example, constructor Table(int) invokes super(int) which is effectively Furniture(int). A derived class constructor must initialize the base class before doing anything else because initialization of derived may depend on the base class.

OOP Fundamentals (1 of 3)

Object Oriented Programming Fundamentals

OOP is a paradigm to solve real-world business problems by dividing the problem into a set of objects communicating with each other.

An Object consists of attributes and actions to be performed on the attributes. In programming jargon, attributes are called data members and actions are called member functions. Member functions are also referred as methods in Java.

In Java, Class is a type of objects which have same attributes & methods. In other words, a class is definition of attributes and methods. Once we have a class definition or blueprint ready, we can realize the class to create Objects. This realization is called instantiation in programming jargon.

Although Classes are blueprint and Objects are the instances of class, but the term ‘Object‘ is generally used to represent either of them.

Here’s an example of Account class and two of its instances having id 1010 and 1011.

Account class

Encapsulation and Abstraction

A class encapsulates data members and member functions into a single reusable entity. Re-usability of classes a benefit of using OOP; a class once written & tested can be re-used in different scenarios to solve similar business problems.

Abstraction is hiding internal details while exposing only relevant & required information to outside world. In java, abstraction is achieved by use of access specifiers. Here we are introducing only two of four access specifiers in Java –

Public – The entity (class, data member, method) is accessible to everyone.

Private – The entity is accessible to only the Class which defines it.

Let’s take real world example of a Car – which is made-up of a lot of attributes like engine, steering, brake pedal, gear-box, tyres, brake shoe, axle, etc. Car exposes interface to the user (methods) like changing gear, applying brake, etc. without telling the user that there is a brake shoe. Someone driving a car does not need to know that it has brake shoe but they need to know how to apply brake.

The benefit to using abstraction is that Car manufacturer can actually switch a mechanical brake with a hydraulic brake without any impact on the way people use or apply brake.

A software world example can be a AlarmLogger class – whose job is to write alarms in a file using a specific format. This class exposes a public method logAlarm and hides internal file-handing and formatting from clients calling this class by keeping them as private. This same class can be reused in multiple Programs like ecommerce app, banking app.

Setup SONAR with PostgreSQL DB

Objective: Setup a Sonar server pointing to a PostgreSQL DB server that can be used by multiple maven clients (developers) remotely

Software:

  1. Sonar 2.8
  2. PostgreSQL server 9.2
  3. Maven 3.3

Prerequisite: Maven and Sonar are already installed. Project repository contains sonar specific dependencies.

Procedure:

Setup PostgreSQL DB server

  1. Install postgreSQL 9.2
  2. Use pgAdmin III to create a new Login Role
    1. Username: sonar
    2. Password: sonar
  3. Create a new Database ‘sonar’ having owner ‘sonar’ login role.

Point SONAR to PostgreSQL DB

  • Update <sonar_home>\conf\sonar.properties
    • Comment sonar.jdbc.* properties to deactivate the default embedded database (derby)
    • Uncomment sonar.jdbc.* properties to use PostgreSQL

sonar.jdbc.url:                            jdbc:postgresql://<ip_address_of_postgresql_server>/sonar
sonar.jdbc.driverClassName:                org.postgresql.Driver
sonar.jdbc.validationQuery:                select 1

  • Copy postgresql-9.2-1002.jdbc3.jar to <sonar_home>\extensions\jdbc-driver\postgresql
  • Update <maven_home>\conf\settings.xml to add a profile for sonar

  <profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>
jdbc:postgresql://<ip_address_of_postgresql_server>/sonar
</sonar.jdbc.url>
<sonar.jdbc.driverClassName>org.postgresql.Driver</sonar.jdbc.driverClassName>
<sonar.jdbc.username>sonar</sonar.jdbc.username>
<sonar.jdbc.password>sonar</sonar.jdbc.password>
<sonar.host.url>
http://<ip_address_of_sonar_server>:9000
</sonar.host.url>
</properties>
</profile>

  • Add IP address of machine where maven build is going to be executed to <postgresql_home>\data\pg_hba.conf

host       all          all          <IP address of maven build machine>/32         trust