10 Essential Object Oriented Concepts for Java Developers

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 with 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 like 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 it is the best one we have got so far. You cannot write code outside Class, which standardizes the code organization.

You also have a package to further group related classes together. Apart from the four pillars two main actors of Object-oriented programming is Class and Object and we will learn plenty about them in these 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 it's 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, Java, in my opinion, is the best language you can use to learn OOP. 

I will start with basics like Inheritance, Encapsulation, Abstraction, and Polymorphism then we will learn about Class and Object, 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.



1. What is class and object in Java? (answer)
A class is a blueprint and an object is an actual house or product built on that blueprint. For example, you have a design of a car that shows that Car has four tires, the engine should be in front and it's automatic, but its design, it's not a car. 

The same is with class, it's just a design or specification about how a car will look like but an object is an actual car e.g. with a number plate, a chassis number and an engine number.

2. What is an abstract class and interface in Java? 
An interface is a 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 keeps things abstract about which he doesn't know much. See here to learn more differences between abstract class and interface in Java.


3. What is the 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 APIs.


4. What is Inheritance in Java? (answer)
Inheritance is an object-oriented concept which allows code reuse by creating parent and child relationship. A child class inherits the 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 about that. 

Here is a nice diagram to explain the concept of inheritance in Java.

OOPS Concept Tutorial in Java - Object Oriented Programming



5. What is Encapsulation in Java? (read more)
Encapsulation is another OOP fundamental concept that 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.


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


7. What is the difference between Abstraction and Polymorphism in Java?
Both Abstraction and Polymorphism are Object-oriented fundamentals one provides simplification of interfaces while the other provides flexibility and dynamic nature. Abstraction hides the complexity and presents a general interface. 

There are different levels of abstraction, at the highest level of abstraction you have the simplest concept, and at a lower level of abstraction you have more concrete things e.g. move() is at a higher level of abstraction than movingByCar(). It's facilitated by interface and abstract class in Java. See this tutorial to learn more


8. What is the 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 fields or that many types but with different values, which is also known as the state of the object.


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


10. Difference between Inheritance and Polymorphism in Java? (answer)
The main difference between Inheritance and Polymorphism is that Inheritance creates a relationship between two classes in Java i.e. parent-child relationship and Polymorphism takes 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


11. Difference between Aggregation, Composition, and Association in OOP? (answer)
All three are Object-oriented programming concepts to define the relationship between objects. When one object makes 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 objects in such that individual object has no value without its owner e.g. Human is a composition of body parts when Human dies, body parts also dies. 

Aggregation, on the other hand, is less strong form of composition where an individual object can survive without an owner e.g. City is an aggregation of People. People can survive without cities.


12. Difference between State and Strategy design patterns? (answer)
While the implementation of both State and Strategy patterns is similar they solve different problems i.e. differences 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.


13. Difference between Factory and Abstract Factory design patterns? (answer)
The main difference between Factory and AbstractFactory patterns is that latter is more flexible than the former. 

The factory provides different products based upon argument passed to bit but AbstractFactory takes it to a 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.


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


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


16. What is the difference between static and dynamic binding in Java?
The main difference between static and dynamic binding is that one takes place at compile time while the other takes 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 an interview point of view but also from a coding point of view. If you know how to turn your requirement into a class and object but you don't know how to structure your code then you won't be able to write good programs which can pass the test of time.


Further Reading
Though most of my learning is through research e.g. searching the internet for relevant and quality articles and tutorials, I must say it's not the ideal way because it takes a long time. Instead of this, rather sticking to a couple of quality books gives much better results in a short time. 

If you love to read books, then I suggest you take the following books to learn Object-Oriented Programming in Java.

1. SOLID Principles of Object-Oriented Design
2. Absolute Introduction to Object-Oriented Programming in Java
3. Java - Object Oriented Programming [For Absolute Beginners]
4. Head First Object-Oriented Analysis and design
5. Head First design Pattern by Kathy Sierra

You can also try solving object-oriented design-based problems on 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 the requirement given here. That will give you an opportunity to use the knowledge you have acquired from these tutorials.

Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

And lastly one question for you? What is SOLID principle? Can you name all the 5 object oriented principle which are part of SOLID? 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.