June 15, 2021

Object Oriented Programming Concepts in Java

In this post, we are going to learn about the Object Oriented Programming (OOP) Concepts in Java. When talking about OOP, there are four main concepts.

  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction

Let’s discuss each of these concepts.

Inheritance

In general, we say that a child inherits behavior, qualities and appearance from their parents. That means it goes from the parent to the child by inheritance. In the same way in programming also, a child class can inherit from its’ parent class. It can inherits behavior(methods) and attributes(variables) contained in its’ parent class. Let’s move to an example.

We call the two classes as parent class and child class or the super class and the sub class. When we call a function belonging to the parent class by one of its’ sub classes, we use the keyword ‘super’. In the same way when creating a sub class in java, we use the keyword ‘extends’ to inherit from the super class.

Advantages

  • Can make the code reusable
  • Makes the code readable
  • Makes code more reliable
  • Reduces code redundancy

Encapsulation

When all the attributes and methods are bound into a single unit, we called that data is encapsulated.

Let’s consider a java class. It is an example for encapsulation. Within a class there are variables and also the methods related with various functionalities. All of them are wrapped inside it and that class then acts as a single functional unit. Here, these variables must be kept private and the methods must be implemented as public so that they can be accessed from outside of the class. These public methods are generally known as ‘getters and setters’. If we only use getters, we can make our code read-only. And if we use setters, we can make our code write-only.

In daily life, we can see many practical examples for this. Most of the people consider a medicinal capsule as the common example. Apart from that we’ll consider about a car as our example. Here, a car is made up of different parts. It contains tires, a steering wheel, seats, engine, fuel tank and so on. These parts are the examples for attributes. On the other hand a car has some behaviors. It can be driven by somebody, and also it can warn you if you haven’t put on your seat belts, and you can switch on the air conditioners inside it. These are like the methods inside a class.

Advantages

  • Helps to protect objects from unwanted access by users.
  • Makes the code reusable
  • Makes code understandable
  • Testing code becomes easy

Polymorphism

If we can perform a certain task in different ways, it is known as polymorphism.

There are two types of polymorphism as runtime polymorphism and compile time polymorphism. Runtime polymorphism is also known as dynamic polymorphism while compile time polymorphism is also known as static polymorphism. To acquire runtime polymorphism we use method overriding and to acquire compile time polymorphism we use method overloading.

Method overloading

This allows us to create more than one method with the same name. But they must have different method signatures. That means the parameters must be different.

Method overriding

In method overriding, parameters must be the same in each overridden method. This can be used in two classes which are having an inheritance between the two classes. By this we can do a specific implementation for a method which is already in the super class.

Below is a real world example for polymorphism. When we say speak. A dog sounds ‘Woof’, a cat sounds ‘Meow’ and a cow sounds ‘Moo’. All these different sounds belong to speaking.

Advantages

  • Makes code reusable
  • Helps to reduce coupling
  • Makes code readable

Abstraction

Hiding the implementation details from user and showing only the functionality is known as abstraction. This is achieved by using abstract classes and interfaces.

We use ‘abstract’ keyword to define an abstract class. This contains abstract methods inside it. An abstract method contains only the method signature. That means no method body implemented there. If a class has an abstract method, it definitely must be an abstract class. A 100% abstract class is called as an interface.

A mobile phone is a real world example where we can see abstraction. It does not show its’ internal hardware and other implementation details to the user. But the user can get use of its’ functionalities.

Advantages

  • Makes code reusable
  • Avoids duplication of code
  • Can perform changes to internal implementation without affecting the user
  • Reduces complexity of design and implementation

29 thoughts on “Object Oriented Programming Concepts in Java

  1. Do you mind if I quote a couple of your articles as
    long as I provide credit and sources back to your webpage?

    My blog site is in the very same niche as
    yours and my users would genuinely benefit from a lot of the information you present here.

    Please let me know if this alright with you. Regards!

  2. Someone essentially lend a hand to make severely
    posts I’d state. That is the first time I frequented your web page and
    up to now? I amazed with the analysis you made to make this actual submit amazing.
    Great activity!

  3. Hello, i read your blog from time to time and i own a similar
    one and i was just curious if you get a lot of spam remarks?
    If so how do you reduce it, any plugin or anything you can suggest?

    I get so much lately it’s driving me crazy so any assistance is very much appreciated.

  4. Wow that was strange. I just wrote an extremely long comment but after I clicked
    submit my comment didn’t show up. Grrrr… well I’m not writing
    all that over again. Anyway, just wanted to say great blog!

  5. What i don’t realize is in truth how you are now not actually a lot more smartly-appreciated than you
    may be right now. You are so intelligent. You know
    therefore significantly when it comes to this topic,
    made me in my view imagine it from numerous various angles.
    Its like women and men aren’t interested unless it’s one thing to do
    with Girl gaga! Your individual stuffs outstanding.
    At all times maintain it up!

  6. You actually make it appear really easy together with your presentation but I find
    this topic to be really one thing which I feel I would never understand.

    It sort of feels too complicated and extremely large
    for me. I am taking a look ahead in your subsequent submit,
    I’ll attempt to get the grasp of it!

  7. Greetings from Los angeles! I’m bored to death at work so I decided to browse
    your site on my iphone during lunch break. I love the info
    you provide here and can’t wait to take a look when I get home.
    I’m shocked at how quick your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyways, excellent site!

  8. Howdy I am so grateful I found your web site, I really found you by error,
    while I was looking on Google for something else, Anyhow I am here now and would just
    like to say thank you for a incredible post and a all round thrilling blog (I also love the theme/design), I don’t have time to look over it all at
    the moment but I have saved it and also added your RSS feeds, so when I have time I will be
    back to read a lot more, Please do keep up the excellent job.

  9. I’ve been browsing online more than 3 hours today, yet I never found any interesting article like yours.

    It is pretty worth enough for me. Personally, if all website owners and bloggers made good content as you did, the internet
    will be a lot more useful than ever before.

  10. Awesome blog! Is your theme custom made or did you download it
    from somewhere? A theme like yours with a few simple tweeks
    would really make my blog shine. Please let me know where you got your theme.
    Cheers

  11. Greate pieces. Keep writing such kind off info on your blog.

    Im really impressed by your site.
    Hello there, You’ve performed a fantastic job. I will definitely digg it and
    personally suggest to my friends. I’m sure they will be benefited frdom this website.

  12. This is the perfect webpage for anyone who wants to understand this topic.
    You understand a whole lot its almost hard to argue with you (not that I actually will need
    to…HaHa). You certainly put a fresh spin on a subject that’s been written about for a long time.
    Excellent stuff, just wonderful!

Leave a Reply

Your email address will not be published. Required fields are marked *