Java Tutorial for Beginners : Part-1 OOP Concepts

 These tutorials will provide you a beginner's perspective of Java programming. The first few tutorials would be irrespective of programming language, but at the latter half, I would be stressing more on Web application development in Java. I am preparing these tutorials on the basis of my own experience of learning at TCS. Your suggestions and queries would help me build on this tutorial

 OOP - A term that should flow in your blood if you have to be a good programmer. It is an essential perspective, visualization and an art to understand the concept as a whole. OOP concepts redefine our thoughts(in programming paradigm) and the way we code. The concepts are very easy in theory, but a bit difficult to grasp in real world scenarios. And once you master this perspective, coding in any language would be a piece of cake. So lets get to the basics first(Mind you, the theory is really simple. Its gets confusing when you try to implement these concepts)

 I guess its high time that I mention what OOP is all about. OOP - Object Oriented Programming, is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. - Wikipedia
 Got any idea? Bet you haven't. Let me put it into simple words. Object Oriented Programming is
  • a way to code, 
  • its an approach towards coding, 
  • its an approach to problem solving,
  • is a real life simulation of programming methodology
Ring any bells? Well, you will be the end of this tutorial(Or atleast the next!)

Now that I have said OOP is an approach to programming, there are a few core concepts that make up this methodology. I am not listing them down at one go, instead I'll be explaining it one by one.
Please note that these concepts are applicable for most of the programming languages. This tutorial explains these concepts with respect to real life scenario.

Classes and Object
  What is the difference between C and C++? C++ is C with classes! So now what are Classes? Classes are a combination of related attributes(variables, properties) and functions. Going to a real life scenario, it would look like the following.
Car, Bike and Auto-rickshaw all have a set of properties and functions(methods). So these would constitute the concept of classes. It would have the variables Wheels, Doors, Colors etc. and a few set a functions like Steer, Accelerate, Brake etc. To go a bit into the coding part, the class car would look like this
class Car
             int wheel;
             int door;
             string color;
             steer()         { .. Function description .. }
             accelerate() { .. Function description .. }
             brake()        { .. Function description .. }
 This would how a sample class would look. So now on to the next term, Objects.
What is an Object? An Object is simply an instance of this class. For example, BMW X5 would be an object of class Car. I don't think more explanation on this is required, please leave a comment if you feel it is required.

Data Abstraction
 Tough word? Nah, not really! Its an easy concept. Data Abstraction means you hide data(keep data abstract) from the user. Or in other words, only essential information are exposed to a user. There is a popular example for data abstraction. When you drive a car, to shift gears, all you have to do is move the Gear stick. You don't have to know how the bearings are working in the background. Similarly which building an application, you need not show unnecessary details to the user. 
Keep it simple!

Data Encapsulation
  You already know this concept. No? Data Encapsulation can be defined as Wrapping up of Data and related functions. That was what we exactly did above in Classes and Objects. The properties(data) and their related functions are combined into a single entity called Class.

  This concept takes the literal meaning of Inheritance. Certain properties are inherited to us from our parents. This perspective is brought onto the programming methodology. I would explain this concept with the above example of Cars and Bikes. These classes share some common properties and functions, like Wheels, Doors, Color etc. So why not put these into a parent class called Vehicles and inherit these properties with specific values in each class? That's exactly what happens here. You write a class called Vehicle which would look like this
class Vehicle
             int wheels;
             int doors;
             string colors;
Note that you won't give the description for the functions over here, as each class(Car or Bike) have their own methods to Steer or Accelerate. Now if you write the class as
class Car extends Vehicle  (This is the Java syntax, might vary for other languages)
...Class description...
This class would have all the above mentioned properties, and can set their own values here. The hierarchy would look like shown in the image

 Don't get strangled by the name, its a simple concept. It's a concept which we can interpret a function in more than one way. To give an example, suppose a function to calculate the Area of a geometric figure(Circle, Square etc.). Ideally, one would  create separate functions with different names for the figures. Using the concept of Polymorphism, you can have the same name for the function, yet do different functions with it. You will get used to this concept at a later point of time, so I'm not going into its depth now.

  Messaging is the core concept of communication among classes. To give an idea of it, let me put up a scenario. Suppose we have two classes, Driver and Car. The Driver has to communicate with the Car to do functions like accelerate, brake etc. So how is this interaction done? Yes, Car has a function accelerate, but some message should be sent from Driver to Car right? That's exactly what messaging is all about. Once you get into this, you would be doing Messaging every now and then.

  Finally! Make sure that you are used to all these concepts. In the upcoming tutorials, you would be seeing these concepts implemented in a real life scenario. Before ending up, let me note down the points to remember.

  • OOP Concepts
  • Classes and Objects
  • Data abstraction
  • Data encapsulation
  • Inheritance
  • Polymorphism
  • Messaging
Let me know if I have to add something to this tutorial! And if you are reading this, thanks for your patience.

Java Tutorial for Beginners : Part -1 OOP Concepts
Java Tutorial for Beginners : Part -2 First Java Program