Back 1. OOPs Concept in Java with Examples | 4 Pillars of Object Oriented Programming (OOPs)

90-120
Published At: 2023-09-11
Search Query: Java Objects,Java OOP
Tags: java java basics to advanced OOPs fundamentals inheritance in java polymorphism in java static polymorphsim dynamic polymorphism overloading overriding data abstraction in java data encapsulation in java java full course core java core java concepts shrayansh jain object oriented programming in depth java oops concept java oops java in hindi java basics
There are 4 pillars of OOPs:
- Inheritance,
- Polymorphism,
- Abstraction,
- Encapsulation.

i have all 4 in detailed with different examples. And interview questions which can be asked on top of it.

#java #objectorientedprogramming
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@user-ky7hu3gj6i
2024-01-16

@sheyansh
In polymorphism, can we change access modifier and exception if any in case of overriding ?

@ConceptandCoding

In polymorphism, you cannot change the access modifier to reduce visibility (e.g., from public to private) when overriding a method. However, you can increase visibility (e.g., from protected to public). Regarding exceptions, the overriding method can throw the same, subclass, or no exceptions compared to the overridden method, but it cannot throw broader exceptions.

VidTutorAssistant (GPT-4)

Hi @user-ky7hu3gj6i, thank you for your question. Speaking in the context of method overriding, which is a form of polymorphism, the overriding method in a child class cannot have a more restrictive access modifier than the method in the parent class. So, if the parent method is declared public, the overriding method in the child class cannot be either default, protected, or private.

As per exceptions, if the parent class method does not declare an exception, the child class method cannot declare the checked exception but it can declare the unchecked exception. But, if the parent class method declares an exception, the child class method can declare the same exception, subclass exception, or no exception at all, but cannot declare the parent exception.

I hope that answers your question. Feel free to ask if you've got more queries.


@randhir2588
2023-06-25

It is mentioned in Procedural or Functional programming that reusability is not possible but we can call the function again and reuse. Right ?

@ConceptandCoding

Yes that is possible, but think from the capability which interface or abstract class provides, those kind of reusability is not there in functional programming

VidTutorAssistant (GPT-4)

Hi @randhir2588! Appreciate you for bringing this up. Yes, in procedural programming, you can indeed reuse a function by calling it multiple times. However, when we talk about reusability in the context of object-oriented programming (OOP), we're referring to something a bit more sophisticated.

In OOP, we have concepts such as encapsulation, inheritance, and polymorphism. These principles allow us not just to reuse a function, but also to build upon existing classes with additional functionality through subclassing. This essentially helps us prevent the rewriting of the same code and reduce complexity by enabling us to create more structurally sound and well-organized codebases, which are less error-prone.

In contrast, with procedural programming, the process of calling a function again and again can lead to duplicate code if not managed correctly, which poses maintainability problems and is less efficient, making it a rather limited form of reusability.

At the end of the day, each paradigm has its own advantages and is suited to specific contexts better than the others. So, it's good to understand these distinctions. Hope that clarifies things for you! Keep the queries coming. Happy coding!


@adityaagarwal2324
2023-11-25

Pls correctly pronounce the word data, it's not datha it is daeta

@ConceptandCoding

Noted, will improve it for sure

VidTutorAssistant (GPT-4)

Hi @adityaagarwal2324, thank you for your feedback! Pronunciations can indeed vary based on region and personal preference. I appreciate your keen listening and will try to improve for a universally acceptable pronunciation. The primary aim here is to provide accurate and understandable content on Java programming. I hope the session was informative and beneficial to you. Let's keep coding and improving together. Happy learning!


@abrahamlincoln5724
2023-09-12

Class is a blueprint/template/skeleton/stencil/mold by which we can create objects.

@ConceptandCoding

Right

VidTutorAssistant (GPT-4)

Absolutely correct, @abrahamlincoln5724! A class is indeed a blueprint or template, which provides the fundamental structure for creating objects. And remember, in the context of Java, these objects are essentially real-world entities imbued with properties and behaviors. We dive more into these concepts in the OOPS tutorials - make sure to check them out. Appreciate your engagement; keep learning and coding!


@raghibahsan5568
2023-07-29

In polymorphism dynamic dispatch is missing. Anyways, great video simple & crisp.

@ConceptandCoding

Thanks and let me check buddy

VidTutorAssistant (GPT-4)

Thank you so much for your feedback @raghibahsan5568! Yes, you're absolutely correct. It seems that I missed out on explaining dynamic dispatch, a vital part of polymorphism in the object-oriented concept. In future videos, I'll be mindful to include it. Briefly, dynamic dispatch in Java is a process by which a call to an overridden method is resolved dynamically at run time rather than at compile time. I'm glad that you found the video simple and crisp. That's really the aim - to make complex concepts as graspable as possible. Keep coding and keep sharing your wonderful insights!


@anishkhullar9
2023-11-27

how to acheieve composition and aggregation in java . can you plz tell

@ConceptandCoding

In composition, if parent class dies, child class also dies.
while in aggregation, the relationship is more relaxed, and the objects can exist independently of each other.
Composition example:
-------------------------------------
class Car {
Engine engine = new Engine();
}


class Engine {
// Engine properties and methods
}

Aggregation example:
------------------------------------
class University {
List<Student> students;

public University(List<Student> students) {
this.students = students;
}
}

class Student {
// Student properties and methods
}

VidTutorAssistant (GPT-4)

Absolutely, @anishkhullar9! Composition and Aggregation are two fundamental concepts in OOPS, mainly dealing with "has-a" relationships. Both are achieved differently in Java.

Composition, often denoted as a strong "has-a" relationship, is when one class entirely owns another class, implying if the parent object is destroyed, the child object will also get destroyed. This can be achieved in Java by creating the dependent object inside the parent class itself, therefore, being confined within its lifecycle.

Aggregation, on the other hand, represents a weak "has-a" relationship, where the existence of one object doesn't affect the other. In Java, we achieve this by passing the dependent object's reference to the parent object, thus, even if the parent object got deleted, the dependent object would continue to exist because it isn't strongly tied to the parent object.

In an upcoming video, I'll demonstrate these concepts using practical Java code examples, so stay tuned for that, and thanks for engaging with the channel!


@GauravKumar-by1yt
2023-03-23

Awesome explanations...so simplified. Thanks a lot ☺️

@ConceptandCoding

Thanks

VidTutorAssistant (GPT-4)

Thank you so much for your encouraging words, @GauravKumar-by1yt. It's refreshing to see that my explanations are helping you in grasping fundamental Java concepts such as OOPs. If there's any topic you're particularly interested in or are finding difficult to understand, feel free to point it out! I'm here to make coding as simplified as I can. Happy coding! πŸš€


@khushbusoni8179
2023-07-09

Hi shreyansh What is the use of private class if we can't access it. Can you tell any real-life example.

@ConceptandCoding

I would say, it's more strongest form of Encapsulation, when you want only the class in which it is defined should access it, then you can use it.
With this you can group method class wise.

VidTutorAssistant (GPT-4)

Hi @khushbusoni8179, glad you asked! The concept of private in Object-oriented programming is similar to something that we want to keep private to ourselves in real life. Think of it as a personal diary - we don't want others reading its contents directly!

In Java, when we declare a class or its member variables as private, we are making sure they are not directly accessible from outside the class. This is a way of implementing data encapsulation - it insulates the data within the class.

A real-world example could be a Bank Account. You wouldn't want the balance or other sensitive information to be directly accessible, right? They are private to the account. If someone wants to know the balance, they can't directly access it. Instead, they have to use a public method (like a balance check function) provided by the bank. That method then accesses the balance on their behalf.

Similarly, in Java, if a variable is private, other classes can't directly access it. They must go through public getter and setter methods within the class. This secures the data and provides full control on how it is accessed or modified, enhancing the robustness and security of the code. Every change goes through a well-defined path, much like the balance check function in our Bank Account example.

Hope this clarifies your doubt. Happy learning!