OOPS Concept Tutorial in Java - Object Oriented Programming

I have written several OOP (Object Oriented Programming) concepts tutorials in past and I was thinking to bring them together so that anyone who wants to learn OOP basics can benefit from them easily. In this article, I will share you my collection of OOP tutorials and OOP concepts interview questions, which will not only help you to understand four pillars of Object Oriented programming e.g. Abstraction, Encapsulation, Inheritance, and Polymorphism but also powerful design techniques of Aggregation, Association and Composition, along with SOLID design principles, which are key to write flexible, extensible and object-oriented software. Java is also a great language to start with object oriented programming, though it's not a pure object oriented language but it is the best one we have got so far. You cannot write code outside Class, which standardize the code organization.

You also have a package to further group related classes together. Apart from four pillars two main actors of Object oriented programming is Class and Object and we will learn plenty about them in this OOP tutorials for Java Programmer. If you are looking for a book to complement your study, I would suggest grabbing a copy of Head First Object Oriented Analysis and design. It's a great book to learn Object-oriented programming in Java.



Java OOP (Object Oriented Programming) Tutorial

Though Object Oriented programming is a paradigm and its language-independent, it makes a huge difference to choose the right language when you are learning OOP. Though you can do Object oriented programming in Python also, but Java, in my opinion, is the best language you can use to learn OOP. I will start with basics e.g. Inheritance, Encapsulation, Abstraction, and Polymorphism then we will learn about Class and Object, after then we will discuss Composition, Aggregation and Association, Object Oriented design principles and finally understand the use of abstract class and interface to complete this OOP tutorial. As I have mentioned already, all examples are in Java, so it's most suited for Java programmers.


What is class and object in Java? (answer)
A class is a blue print and object is actual house or product built on that blue print. For example, you have a design of a car which shows that Car has four tires, engine should be in front and its automatic, but its design, its not a car. Same is with class, its just a design or specification about how a Car will look like but an object is the actual car e.g. with a number plate, a chesis number and an engine number.

What is abstract class and interface in Java? 
An interface is pure form of abstraction but an abstract class is a compromise between Abstraction and Concrete implementation. It provides a concrete implementation for those things which he knows more and keep things abstract about which he doesn't know much. See here to learn more differences between abstract class and interface in Java.


What is difference between class and interface in Java? (answer)
An interface defines a public API, nothing is private about it. It's like a contract, but a class contains both public and private details required to support those public API.


What is Inheritance in Java? (answer)
Inheritance is an object oriented concept which allows code re-use by creating parent and child relationship. A child class inherit parent's code and can modify it if not protected. Though Inheritance allows code reuse it is not the best way to reuse code, instead Composition is better than inheritance. See this article to learn more on that. Here is a nice diagram to explain the concept of inheritance in Java.

OOPS Concept Tutorial in Java - Object Oriented Programming



What is Encapsulation in Java? (read more)
Encapsulation is another OOP fundamental concept which provides data hiding and protection of your code. It allows you to hide implementation details e.g. how you are implementing a method, so that you can modify  or change the code later.


What is Polymorphism in Java? Overloading or Overriding? (answer)
Polymorphism is another object oriented concept which adds dynamic nature and flexibility in your code. Both overloading and overriding are based on Polymorphism but one is compile time Polymorphism while other is runtime Polymorphism.


What is difference between Abstraction and Polymorphism in Java?
Both Abstraction and Polymorphism is Object oriented fundamentals one provides simplification of interfaces while other provide the flexibility and dynamic nature. Abstraction hides the complexity and present a general interface. There are different level of abstraction, at highest level of abstraction you have simplest concept and at lower level of abstraction you have more concrete things e.g. move() is at higher level of abstraction than movingByCar(). It's facilitated by interface and abstract class in Java. See this tutorial to learn more


What is difference between Class and Object in Java or OOP? (answer)
A class is a blueprint for creating objects. You define how your object will look like e.g. how many fields they would have, what would be their data type etc. When an object is created they have that many field or that many type but with different values, which is also known as state of object.


Difference between Abstraction and Encapsulation in Java? (detailed answer)
Again both are OOP basic concepts, Abstraction provides generalization and simplification of interface while Encapsulation hides implementation details so that you can change it later without affecting client code which uses it.


Difference between Inheritance and Polymorphism in Java? (answer)
Main difference between Inheritance and Polymorphism is that Inheritance create relationship between two classes in Java i.e. parent child relationship and Polymorphism take advantage of that to provide flexibility. You cannot have runtime Polymorphism e.g. method overriding without Inheritance in Java.

Object Oriented Programming concepts in Java tutorial


Difference between Aggregation, Composition and Association in OOP? (answer)
All three are Object oriented programming concepts to define relationship between objects. When one object make use of another object to do the task they are said to be associated with each other and when an object is made of several object in such that individual object has no value without it's owner e.g. Human is composition of body parts, when Human dies, body parts also dies. Aggregation on the other hand is less stronger form of composition where individual object can survive without owner e.g. City is aggregation of People. People can survive without cities.


Difference between State and Strategy design patterns? (answer)
While the implementation both State and Strategy pattern is similar they solve different problems i.e. difference in their intent. The State pattern deals with what state an object is in - it encapsulates state-dependent behavior.  The Strategy pattern deals with how an object performs a certain task - it encapsulates an algorithm.


Difference between Factory and Abstract Factory design patterns? (answer)
Main difference between Factory and AbstractFactory pattern is that later is more flexible than former. Factory provides different products based upon argument passed to bit but AbstractFactory takes it to new level to use different Factory based upon configuration e.g. Pizza in India will have Indian taste because of IndianFactory but it will have totally different taste when created in America using AmericanFactory.


What is difference between Overloading and Overriding in OOP? (answer)
The overloading provides compile time Polymorhism while Overriding provides runtime Polymorphism. In order to achieve method overloading you don't need Inheritance but for Overriding you must have parent child relationship between two classes.


Difference between instance and object in Java?
Both refers to same thing, it's just the terminology which is popular in certain context e.g. instance is preferred over object in Java because it also a class called java.lang.Object. See this tutorial to learn more differences between object and instance in Java.


What is difference between static and dynamic binding in Java?
The main difference between static and dynamic binding is that one take place at compile time while other take place at runtime. See this tutorial to learn this concept in more detail.

That's in this list of Java Object Oriented Programming tutorials and articles for beginners and intermediate Java developers. Remember, OOP is very important not just from interview point of view but also on coding point of view. If you know how to turn your requirement into class and object but you doesn't know how to structure your code than you won't be able to write good programs which can pass test of time.


Further Reading
Though most of my learning is through research e.g. searching internet for relevant and quality articles and tutorials, I must say its not the ideal way because it takes long time. Instead of this, rather sticking to couple of quality book gives much better result in short time. If you love to read books, then I suggest you to take following books to learn Object Oriented Programming in Java.

1. Head First Object Oriented Analysis and design
2. Head First design Pattern by Kathy Sieara
3. UML for Java Programmers by Uncle Bob Martin

You can also try solving object oriented design based problems by your own e.g. how to build a traffic control system in Java, how to design a coffee machine in Java, or how to design a vending machine in Java as per requirement given here. That will give you an opportunity to use the knowledge you have acquired from these tutorials.


References
Object Oriented Programming
Object Oriented Programming Concepts



No comments:

Post a Comment