Thursday, March 31, 2016

Java OOP - Inheritance


Inheritance is one of Java OOP concepts. Inheritance allows a class to use the properties and methods of another class. In other words, the derived class inherits the states and behaviors from the base class.

That is to say, the idea behind inheritance in java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also. So the key word here is "reuse code", doesn't sound like a good idea?

Imagine you are sitting at work and get a call your rich uncle died leaving you 1 Billion dollars. What is the first thing you do? Probably SPEND THE MONEY!! HOORAY!!!!...well, with Inheritance, is something similar, but in this case your uncle didn't die but is letting you use his money (methods and fields)

Definitely Inheritance is super useful from a developer perspective, think about it:
  • Reusability -- facility to use public methods of base class without rewriting the same 
  • Extensibility -- extending the base class logic as per business logic of the derived class 
  • Data hiding -- base class can decide to keep some data private so that it cannot be altered by the derived class 
  • Overriding--With inheritance, we will be able to override the methods of the base class so that meaningful implementation of the base class method can be designed in the derived class. 
Here is an example, so we can understand better the idea of Inheritance when coding:

class Parent
{
    public void p1()
    {
        System.out.println("Parent method");
    }
}
public class Child extends Parent {
    public void c1()
    {
        System.out.println("Child method");
    }
    public static void main(String[] args)
    {
        Child cobj = new Child();
        cobj.c1();   //method of Child class
        cobj.p1();   //method of Parent class 
    }
}

Output

Child method
Parent method

Wednesday, March 30, 2016

Java Object Oriented Programming (OOP)



Java is so popular nowadays thanks to many features that help developers code faster and efficiently.
One of those important features is : Object Oriented Programming or OOP

But What does it mean? Basically, in Java, everything is an Object. Java can be easily extended since it it based on the Object model. Something like picturing everything as it happens in real world.

For example:

A car is assembled from parts and components, such as chassis, doors, engine, wheels, brake and transmission. The components are reusable, e.g., a wheel can be used in many cars (of the same specifications).

Hardware, such as computers and cars, are assembled from parts, which are reusable components.

How about software? Can you "assemble" a software application by picking a routine here, a routine there, and expect the program to run? The answer is obviously no! Unlike hardware, it is very difficult to "assemble" an application from software components. Since the advent of computer 70 years ago, we have written tons and tons of programs. However, for each new application, we have to re-invent the wheels and write the program from scratch.

As a solution for all this "assemble" and sharing issues. we have Java OOP features. yes! Java saving our life, one more time!!!!

Some of Java OOP concepts are:
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction



oops concept in java




But,"hold on, Rolo, are you saying that we need to change our reliable way to code and leave our comfort and start coding in OO way?"...No! We live in a free country, you don't have to,.lol...However, there are some huge advantages about coding in Java and using its OOP features.....

Benefits of OOP:
  • OOP provides a clear modular structure for programs. 
  • It is good for defining abstract data types. 
  • Implementation details are hidden from other modules and other modules has a clearly defined interface. 
  • It is easy to maintain and modify existing code as new objects can be created with small differences to existing ones. 
  • It implements real life scenario. 
  • More reliable software development is possible. 
  • Much suitable for large projects.

Well, hope you got a good overview about what OOP is. I'll post about every OO concept later....

Tuesday, March 29, 2016

Java and Dependency Injection


Java and Dependency Injection

Holy cows! "Dependency Injection" sounds like an action movie, like the second part of "Independency Day! yeah!!!....(don't get too excited -__- , it isn't)

When I started programming in Java everything was happiness, everything was beautiful and nothing hurt. You code, you run it, it works, you love it......but, (there is always a but) one day I was asked to work with "Dependency Injection ", so I gave it a try. Have to be honest, I got super confused. The whole idea about "injecting" something scared me (just kidding =) ) Well, the point is that, at the beginning, it was hard to totally understand this concept. It was like I knew what to do and how to make it work, but didn't really understand how and why.

However, one day, my best friend google spoke and showed me the truth!

I want to share an interesting article I found with a nice example. This helped me to understand what Dependency Injection is and why we need it. Enjoy!

------------

Understanding Dependency Injection and its Importance, A tutorial
 

Any application is composed of many objects that collaborate with each other to perform some useful stuff. Traditionally each object is responsible for obtaining its own references to the dependent objects (dependencies) it collaborate with. This leads to highly coupled classes and hard-to-test code.

For example, consider a Car object.

A Car depends on wheels, engine, fuel, battery, etc. to run. Traditionally we define the brand of such dependent objects along with the definition of the Car object.

Without Dependency Injection (DI):

class Car{
  private Wheel wh= new NepaliRubberWheel();
  private Battery bt= new ExcideBattery();

  //The rest
}

Here, the Car object is responsible for creating the dependent objects.

What if we want to change the type of its dependent object - say Wheel - after the initial NepaliRubberWheel() punctures? We need to recreate the Car object with its new dependency say ChineseRubberWheel(), but only the Car manufacturer can do that.

Then what does the Dependency Injection do us for...?

When using dependency injection, objects are given their dependencies at run time rather than compile time (car manufacturing time). So that we can now change the Wheel whenever we want. Here, the dependency (wheel) can be injected into Car at run time.

After using dependency injection:
class Car{
  private Wheel wh= [Inject an Instance of Wheel at runtime]
  private Battery bt= [Inject an Instance of Battery at runtime]
  Car(Wheel wh,Battery bt) {
      this.wh = wh;
      this.bt = bt;
  }
  //Or we can have setters
  void setWheel(Wheel wh) {
      this.wh = wh;
  }
}


Source: Understanding dependency injection

Monday, March 28, 2016

Time to CODE SMART


If you are really involved in coding, you know that sometimes you want to go straight to your favorite IDE and start typing code. If the code fails, well, we keep trying and trying until we get the desire result. However, something that I have learned while working in a big company, with multiple teams is that........I need to perform some steps before typing any code, and these are:

  • Understand
  • Analyze
  • Run
  • Check the logs
  • REPEAT

I know, I know, you learned all those fancy ninja programming techniques and you feel like don't need to waste time reading and analyzing, plus you heard that Facebook was created in only one night, and you want to be the next Mark Z., blah blah, blah.....but believe me, those steps are a must.

Of course, when you work at home, maybe in some personal projects, you know the code, you know how to do your stuff and you have no  pressure on finishing something, but when you work on big projects and there are people waiting for your code....everything changes, hell yeah

First of all, It is important to UNDERSTAND what you need to do with the code and have a clear idea of the tasks you are being assigned. Yeah I know, it sounds obvious, but it is the first step

Second, ANALYZE the code. Yes, ANALYZE . Make sure you start going line by line before touching anything, Make sure you know how things work and from where you are getting values, variables, sources, etc....A - N - A - L - Y - Z - E . =)

Third, RUN!, not like Forest Gump of course, RUN the code only, if you can "run" and RUN the code at the same time cool for you, working out and typing code at the same sounds healthy, lol.. Once you understood what you need to do, analyzed the code you have to work on, then you would need to RUN the code and see how it works. You can reproduce any scenario and understand the current result and why something needs to be changed.

Fourth, CHECK THE LOGS. this is really important. There you can see any exception or error message that can guide you when coding. Yeap, don't be scared, logs files are friendly, they don't bite.

Last but not least, REPEAT all the steps above mentioned. "Hold on Rolo, did you say REPEAT all the steps?" Yes!, Yes!, as many times as you need. Believe me, you will waste more time if you start coding without performing all the steps I have mentioned. You will have to go back and forth fixing stuff and remember that other people are waiting for your code.


Saturday, March 26, 2016

Nerdy Intro


Hello there,

My friends call me “Rolo” and I’m a Software Developer (that sounds like an A.A intro, right? lol). Originally from Peru, but living in South Florida since 2005. I’ve been programming for a while, not an IT guru, but I love being involved in coding. I try to learn a little bit of everything, from Photoshop and Web Design to coding in VB, Java and configuring WebSphere Application Servers. Through all these years working with computers I have faced so many challenges, had so much fun, and of course soooooooooooo much stress! But, you know, that's part of the game. Did I mention stress? =)

I’m going to use this Blog to share some experiences, tips, info and whatever crosses my mind, but all related to, software, hardware and other nerdy things. Did I mention stress when coding?

Well, I hope you enjoy this Blog. I’m a super busy guy, but I’ll try to manage my time and post interesting stuff here…..