Object
An object is a basic unit of Object-Oriented Programming that represents
real-life entities. An object mainly consists of:
1. State: It is represented by the attributes of an object. It also reflects the
properties of an object.
2. Behaviour: It is represented by the methods of an object. It also reflects
the response of an object to other objects.
3. Identity: It is a unique name given to an object that enables it to interact
with other objects.
4. Method: A method is a collection of statements that perform some
specific task and return the result to the caller. A method can perform
some specific task without returning anything. Methods allow us
to reuse the code without retyping it, which is why they are
considered time savers
Class
Collection of objects is called class. It is a logical entity.
A class in Java is a set of objects which shares common characteristics/
behavior and common properties/ attributes. It is a user-defined blueprint or
prototype from which objects are created. Class doesn't consume any space.
When we create an object which is a non primitive data type, it’s always
allocated on the heap memory.
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. For
example: to convince the customer differently, to draw something, for
example, shape, triangle, rectangle, etc.In Java, we use method overloading
and method overriding to achieve polymorphism .
Abstraction
Hiding internal details and showing functionality is known as abstraction. For
example, phone call, we don't know the internal processing. In Java, we use
abstract class and interface to achieve abstraction.
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.
Aggregation
Aggregation is a way to achieve Association. Aggregation represents the
relationship where one object contains other objects as a part of its state. It
represents the weak relationship between objects. It is also termed as a has-
a relationship in Java. Like, inheritance represents the is-a relationship. It is
another way to reuse objects.
Composition
The composition is also a way to achieve Association. The composition
represents the relationship where one object contains other objects as a part
of its state. There is a strong relationship between the containing object and
the dependent object. It is the state where containing objects do not have an
independent existence. If you delete the parent object, all the child objects
will be deleted automatically.
Access Modifiers in Java
Access modifiers help to restrict the scope of a class, constructor, variable,
method, or data member. It provides security, accessibility, etc
Types of Access Modifiers in Java
1. Default – No keyword required
2. Private
3. Protected
4. Public
1. Default Access Modifier
When no access modifier is specified for a class, method, or data member –
It is said to be having the default access modifier by default. The data
members, classes, or methods that are not declared using any access
modifiers i.e. having default access modifiers are accessible only within the
same package.
2. Private Access Modifier
The private access modifier is specified using the keyword private. The
methods or data members declared as private are accessible only within the
class in which they are declared.
Any other class of the same package will not be able to access these
members.
Top-level classes or interfaces cannot be declared as private because
private means “only visible within the enclosing class”.
protected means “only visible within the enclosing class and any
subclasses”
3. Protected Access Modifier
The protected access modifier is specified using the keyword protected.
The methods or data members declared as protected are accessible
within the same package or subclasses in different packages.
4. Public Access modifier
The public access modifier is specified using the keyword public.
The public access modifier has the widest scope among all other access
modifiers.
Classes, methods, or data members that are declared as public
are accessible from everywhere in the program. There is no restriction on
the scope of public data members.
What is Polymorphism in Java?
Polymorphism allows us to perform a single action in different ways. In other
words, The word “poly” means many and “morphs” means forms, So it
means many forms.
Types of Java Polymorphism
In Java Polymorphism is mainly divided into two types:
Compile-time Polymorphism
Runtime Polymorphism
Compile-Time Polymorphism in Java
It is also known as static polymorphism. This type of polymorphism is
achieved by function overloading
Method Overloading
When there are multiple functions with the same name but different
parameters then these functions are said to be overloaded. Functions can
be overloaded by changes in the number of arguments or/and a change in
the type of arguments.
Runtime Polymorphism in Java
It is a process in which a function call to the overridden method is resolved at
Runtime. This type of polymorphism is achieved by Method Overriding.
Method overriding
occurs when a derived class has a definition for one of the member functions
of the base class. That base function is said to be overridden.