Entity.. An Object Contains An Address and Takes Up Some Space in Memory
Entity.. An Object Contains An Address and Takes Up Some Space in Memory
Entity.. An Object Contains An Address and Takes Up Some Space in Memory
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
Apart from these concepts, there are some other terms which are used in Object-
Oriented design:
o Coupling
o Cohesion
o Association
o Aggregation
o Composition
A class can also be defined as a blueprint from which you can create an individual object.
Class doesn't consume any space.
o Fields
o Methods
o Constructors
o Blocks
o Nested class and interface
Inheritance-When one object acquires all the properties and behaviors of a parent
object, it is known as inheritance. It provides code reusability. It is used to achieve
runtime polymorphism.
Polymorphism-If one task is performed in different ways, it is known as polymorphism. In
Java, we use method overloading and method overriding to achieve polymorphism.
Encapsulation-Binding (or wrapping) code and data together into a single unit are
known as encapsulation. For example, a capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class
because all the data members are private here.
Cohession- Cohesion refers to the level of a component which performs a single well-
defined task. A single well-defined task is done by a highly cohesive method. The weakly
cohesive method will split the task into separate parts. The java.io package is a highly
cohesive package because it has I/O related classes and interface. However, the java.util
package is a weakly cohesive package because it has unrelated classes and interfaces.
Association- Association represents the relationship between the objects. Here, one
object can be associated with one object or many objects. There can be four types of
association between the objects:
o One to One
o One to Many
o Many to One, and
o Many to Many
Let's understand the relationship with real-time examples. For example, One country can
have one prime minister (one to one), and a prime minister can have many ministers
(one to many). Also, many MP's can have one prime minister (many to one), and many
ministers can have many departments (many to many).
What is it called if an object has its own lifecycle and there is no owner-Association
What is it called where object has its own lifecycle and child object cannot belong to another parent
object?-Aggregation
What is it called where child object gets killed if parent object is killed?-Composition
Advantage of oops
OOPs provides the ability to simulate real-world event much more effectively
Instance variable
A variable which is created inside the class but outside the method is known as an
instance variable. Instance variable doesn't get memory at compile time. It gets memory
at runtime when an object or instance is created. That is why it is known as an instance
variable.
Method in java
In Java, a method is like a function which is used to expose the behavior of an object.
Advantage of Method
o Code Reusability
o Code Optimization
new keyword in Java
The new keyword is used to allocate memory at runtime. All objects get memory in Heap
memory area.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B
classes. If A and B classes have the same method and you call it from child class object,
there will be ambiguity to call the method of A or B class.