Notes Unit 1 and 2
Notes Unit 1 and 2
Object-Oriented Programming
The word object-oriented is the combination of two words
i.e. object and oriented. The dictionary meaning of the object is an
article or entity that exists in the real world. The meaning of oriented is
interested in a particular kind of thing or entity. In layman's terms, it is a
programming pattern that rounds around an object or entity are
called object-oriented programming.
Pillars of OOPs
The major concepts that we have discussed above are known as pillars
of OOPs. There are four pillars on which OOP rests.
o Abstraction
o Encapsulation
o Inheritance
o Polymorphism
Abstraction
The concept allows us to hide the implementation from the user but
shows only essential information to the user. Using the concept developer
can easily make changes and added over time.
There are the following advantages of abstraction:
o It reduces complexity.
o It avoids delicacy.
o Eases the burden of maintenance
o Increase security and confidentially.
Encapsulation
Encapsulation is a mechanism that allows us to bind data and functions of
a class into an entity. It protects data and functions from outside
interference and misuse. Therefore, it also provides security. A class is the
best example of encapsulation.
Inheritance
The concept allows us to inherit or acquire the properties of an existing
class (parent class) into a newly created class (child class). It is known
as inheritance. It provides code reusability.
Polymorphism
The word polymorphism is derived from the two words
i.e. ploy and morphs. Poly means many and morphs means forms. It
allows us to create methods with the same name but different method
signatures. It allows the developer to create clean, sensible, readable, and
resilient code.
Object
An object is a real-world entity that has attributes, behavior, and properties. It is referred
to as an instance of the class. It contains member functions, variables that
we have defined in the class. It occupies space in the memory. Different
objects have different states or attributes, and behaviors.
Class
A class is a blueprint or template of an object. It is a user-defined data
type. Inside a class, we define variables, constants, member functions,
and other functionality. it binds data and functions together in a single
unit. It does not consume memory at run time. Note that classes are not
considered as a data structure. It is a logical entity. It is the best example
of data binding. Note that a class can exist without an object but vice-
versa is not possible.
The following figure best illustrates the class and object in OOP.
Static Binding
Static binding, also known as early binding or compile-time binding,
involves the linkage between a method call and its implementation being
resolved at compile time. This implies that the binding is established and
determined by the compiler based on the type of the reference variable.
Let's see an example of static binding.
Dynamic Binding
Dynamic binding, also known as late binding or runtime binding, involves
the linkage between a method call and its implementation being resolved
at runtime. The determination of the actual method to be invoked occurs
during program execution based on the actual type of the object. Let's see
an example of dynamic binding.
5. Orientatio It is It is object-oriented.
n structure/procedure-
oriented.
Applications of OOPs
o Computer graphics applications
o Object-oriented database
o User-interface design such as windows
o Real-time systems
o Simulation and modeling
o Client-Server System
o Artificial Intelligence System
o CAD/CAM Software
o Office automation system
Limitations of OOP
o Requires intensive testing processes.
o Solving problems takes more time as compared to Procedure
Oriented Programming.
o The size of the programs created using this approach may become
larger than the programs written using the procedure-oriented
programming approach.
o Software developed using this approach requires a substantial
amount of pre-work and planning.
o OOP code is difficult to understand if you do not have the
corresponding class documentation.
o In certain scenarios, these programs can consume a large amount of
memory.
o Not suitable for small problems.
o Takes more time to solve problems.
What are the different access modifiers? Explain its scope with a suitable example.
1. Private: The access level of a private modifier is only within the class. It
cannot be accessed from outside the class.
2. Default: The access level of a default modifier is only within the package.
It cannot be accessed from outside the package. If you do not specify any
access level, it will be the default.
3. Protected: The access level of a protected modifier is within the package
and outside the package through child class. If you do not make the child
class, it cannot be accessed from outside the package.
4. Public: The access level of a public modifier is everywhere. It can be
accessed from within the class, outside the class, within the package and
outside the package.
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y
2. Memory deallocation:
Memory deallocation is necessary to free up the memory occupied by the
object once the object is no longer needed. In some languages, such as
Java and C#, memory deallocation is done automatically by a process
called garbage collection, which identifies and removes objects that are
no longer reachable or in use. In other languages, such as C++, memory
deallocation needs to be done manually using techniques such as delete
(for single objects) or delete[] (for arrays) to release the memory occupied
by objects allocated on the heap.
2. Memory Overhead:
The memory allocation in the object can lead to overhead in the memory.
This means the program consumes some extra memory space due to the
memory management mechanism. For example, some languages, such as
C ++, require explicit memory deallocation using delete or delete[]
statements, which can introduce code complexity and maintenance
overhead. Garbage collection in languages like Java and C# can also
introduce overhead in terms of runtime performance as the garbage
collector needs to periodically scan and clean up objects that are no
longer needed. Understanding and managing memory overhead is
important to ensure efficient memory usage in a program.
3. Memory leaks:
When the programmer tries to allocate and deallocate the memory
improperly, there is a chance of memory leaks. It also occurs when the
programmer allocates the memory but does not deallocate the memory in
a proper way. It occupies the memory indefinitely, but this memory was
not available for the other part of the program. A memory leak can also
degrade the performance of the program over time, and after some time,
it shows an error like insufficient memory space. So properly managing
memory allocation and deallocation is essential to prevent memory leaks
and ensure the efficient use of memory in a program.
Suppose you have to perform addition of the given numbers but there can
be any number of arguments, if you write the method such as a(int,int) for
two parameters, and b(int,int,int) for three parameters then it may be
difficult for you as well as other programmers to understand the behavior
of the method because its name differs.
lass Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b){return a+b;}
}
class TestOverloading2{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}}
Output:
22
24.9
ADVERTISEMENT
o The static variable can be used to refer to the common property of all
objects (which is not unique for each object), for example, the company
name of employees, college name of students, etc.
o The static variable gets memory only once in the class area at the time of
class loading.