0% found this document useful (0 votes)
5 views

Notes Unit 1 and 2

Uploaded by

psbhawke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Notes Unit 1 and 2

Uploaded by

psbhawke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Unit 1 Foundations of OOPs

1. Explain the features of object-oriented programming with examples.


2. Difference between static binding and late binding.
3. Write a program in Java to check whether the given number is palindrome or not.
4. Explain the four pillars of object- oriented programming with an example.
5. Difference between object- oriented programming and proceduraloriented programming.
6. Write a program in Java to check whether the given number is even or odd
7. Differentiate between object oriented programming and procedural oriented programming.
8. Model a real-world scenario ‘vehicle’ class using object oriented paradigm.
9. Write a short notes on:
Data Abstraction
ii) Inheritance
10. Describe the characteristics of object oriented programming.
11. Write a short notes on:
Polymorphism
ii) Data Encapsulation
12. What are the limitations of procedural oriented programming?

UNIT 2 Class ,Object ,Methods


1. Elaborate any two uses of ‘this’ keyword in object oriented programming.
2. How is method overloading achieved in object oriented programming? Explain with an
example.
3. Identify classes, objects, and methods for adding two complex number
4. Write a program demonstrating passing an object as a parameter to a method.
5. What are the characteristics of static variables and methods? Explain with an example.
6. Design a class ‘student’ with suitable instance variables and methods. Create the database of
the students and display the records using an array of objects.
7. What are the different access modifiers? Explain its scope with a suitable example.
8. What is the need for using “this” pointer? How do we access the variables using “this”
pointer?
9. Explain the concept of memory allocation and de- allocation with respect to OOP.
10. Explain the concept of Class as an Abstract Data Type.
11. How do we use the static variables and static functions in the class? Explain with a suitable
example.
12. Why do we need method overloading? Write a program to demonstrate method overloading.
Unit 1 Foundations of OOPs

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.

The above figure best describes the concepts of polymorphism. A person


plays an employee role in the office, father and husband role in the home.

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.

Aspect Static Binding Dynamic Binding

Binding Compile Time Binding Runtime Binding (Dynamic


Time (Static Binding): The Binding): The linkage between
linkage between method call method call and implementation
and implementation is is resolved at runtime.
resolved at compile time.

Type of Early Binding: The binding Late Binding: The actual


Binding is determined by the method to be invoked is
compiler based on the type determined during program
of the reference variable. execution based on the actual
type of the object.

Flexibility Less Flexible: Relies on the More Flexible: Adapts to


declared type of the changes in the actual type of the
reference variable. object at runtime.

Performan Generally More May Incur


ce Efficient: Early resolution at Overhead: Resolution occurs at
compile time leads to faster runtime, potentially introducing
execution. a slight performance cost.

Use Cases Ideal for Known Adaptable Scenarios: Suited


Methods: Suited for for situations where the actual
scenarios where the method method to be called can vary at
to be invoked is known at runtime based on different
compile time and unlikely to object types.
change.

Difference between procedural programming


and object-oriented programming

S.no On the Procedural Object-oriented programming


. basis of Programming

1. Definition It is a programming Object-oriented programming


language that is is a computer programming
derived from structure design philosophy or
programming and methodology that organizes/
based upon the models software design
concept of calling around data or objects rather
procedures. It follows than functions and logic.
a step-by-step
approach in order to
break down a task into
a set of variables and
routines via a
sequence of
instructions.

2. Security It is less secure than Data hiding is possible in


OOPs. object-oriented programming
due to abstraction. So, it is
more secure than procedural
programming.

3. Approach It follows a top-down It follows a bottom-up


approach. approach.

4. Data In procedural In OOP, objects can move and


movemen programming, data communicate with each other
t moves freely within via member functions.
the system from one
function to another.

5. Orientatio It is It is object-oriented.
n structure/procedure-
oriented.

6. Access There are no access The access modifiers in OOP


modifiers modifiers in are named as private, public,
procedural and protected.
programming.

7. Inheritanc Procedural There is a feature of


e programming does not inheritance in object-oriented
have the concept of programming.
inheritance.

8. Code There is no code It offers code reusability by


reusabilit reusability present in using the feature of
y procedural inheritance.
programming.

9. Overloadi Overloading is not In OOP, there is a concept of


ng possible in procedural function overloading and
programming. operator overloading.

10. Importanc It gives importance to It gives importance to data


e functions over data. over functions.

11. Virtual In procedural In OOP, there is an


class programming, there appearance of virtual classes
are no virtual classes. in inheritance.

12. Complex It is not appropriate It is appropriate for complex


problems for complex problems. problems.
13. Data There is not any There is a possibility of data
hiding proper way for data hiding.
hiding.

14. Program In Procedural In OOP, a program is divided


division programming, a into small parts that are
program is divided referred to as objects.
into small programs
that are referred to as
functions.

15. Examples Examples of The examples of object-


Procedural oriented programming are -
programming include .NET, C#, Python, Java,
C, Fortran, Pascal, and VB.NET, and C++.
VB.

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.

Elaborate any two uses of ‘this’ keyword in object oriented programming.

his keyword in Java


There can be a lot of usage of Java this keyword. In Java, this is
a reference variable that refers to the current object.

Usage of Java this keyword


this: to refer current class instance variable
The this keyword can be used to refer current class instance variable. If
there is ambiguity between the instance variables and parameters, this
keyword resolves the problem of ambiguity.

this: to invoke current class method


You may invoke the method of the current class by using the this
keyword. If you don't use the this keyword, compiler automatically adds
this keyword while invoking the method. Let's see the example
this: to pass as argument in the constructor call
We can pass the this keyword in the constructor also. It is useful if we
have to use one object in multiple classes.

this: to pass as an argument in the method


The this keyword can also be passed as an argument in the method. It is
mainly used in the event handling

this() : to invoke current class constructor


The this() constructor call can be used to invoke the current class
constructor. It is used to reuse the constructor.

What are the different access modifiers? Explain its scope with a suitable example.

The access modifiers in Java specifies the accessibility or scope of a field,


method, constructor, or class. We can change the access level of fields,
constructors, methods, and class by applying the access modifier on it.

There are four types of Java access modifiers:

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.

Access within within outside package by outside


Modifier class package subclass only package

Private Y N N N
Default Y Y N N

Protected Y Y Y N

Public Y Y Y Y

Explain the concept of memory allocation and de- allocation with


respect to OOP.

In computer programming, memory allocation is a fundamental concept.


Its play a vital role in optimizing and managing the memory usage of
computer. In object-oriented programming (OOP), memory allocation has
a more significant role as objects, as these are the basic building blocks of
OOP languages such as Java, C++, Python, and C#. In this article, we are
going to learn about what memory allocation for objects means, how it
works, and its implications in computer programming.

Memory Allocation for Objects


In object-oriented programming (OOP), the memory allocation for an
object means it is the process to reserve the memory block in the memory
of the computer for storing an object during the runtime. In object-
oriented programming (OOP), objects are also known as instances. These
instances represent real-world entities such as a person, a car, or a bank
account. When an object is created, the memory needs to be allocated to
store its data members (variables) and member functions (methods) so
that these objects can be accessed and manipulated during program
execution.
1. Memory Allocation:
In object-oriented programming (OOP), memory must be allocated to store
its data member and function. The amount of memory required for an
object depends on the size and type of its data members and the size and
complexity of its member functions. In some languages, such as C++,
memory for objects can be allocated on the stack or the heap area.

o Stack Memory Allocation: In stack memory allocation, memory is


allocated from the stack. Here stack is a region of memory that is
automatically managed by the compiler. In stack memory allocation, the
memory gets allocated in the last-in, first-out (LIFO) manner. So the
memory allocation in the stack is very fast and efficient. However, the size
of stack memory is limited, and it is generally used for objects with small
sizes or short lifetimes.
o Heap Memory Allocation: In heap memory allocation, memory is
allocated from the heap. Here heap is a region of memory that is managed
by the programmer. Heap memory allocation allows the object to have a
long lifetime and large size, making the heap memory allocation more
flexible. However, heap memory allocation is relatively slower and can be
more complex as the programmer needs to manage memory deallocation
manually to avoid memory leaks.

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.

Implications of Memory Allocation for Objects


In computer programming, memory allocation of the object has several
implications. Understanding memory allocation is very necessary to avoid
bugs in the code. Some of the key implications of memory allocation of
the object are explained below.
1. Memory Efficiency:
When the programmer allocates memory inefficiently manner then it has
a great impact on the performance of the program. When we compare the
memory allocation in the stack area in comparison to the heap area, the
stack area is very fast and inefficient in memory management. In the
stack area, the memory is managed automatically with the help of a
compiler; on the other hand in, the heap area requires memory to be
managed manually, which can be error-prone and may result in memory
leaks if not handled properly.

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.

4. Lifetime and Scope of Objects:


It is closely related to memory allocation. The object that is allocated over
the stack has a limited lifetime. After completion of the lifetime of the
object, it is automatically deallocated when they go out of scope. The
objects are allocated on the heap, which have longer lifetimes, and the
programmer has to deallocate the memory manually. Understanding the
lifetime and scope of objects is important for managing memory efficiently
and avoiding dangling pointers or references, which can result in
unexpected behavior and bugs in a program.
5. Memory Fragmentation:
When the memory is allocated and deallocated in a contiguous manner,
and it leaves gaps of unused memory that are too small to be used for
new allocations, then the fragmentation of memory occurs. It causes
inefficient memory usage and reduced performance. Some languages and
memory allocation techniques, such as garbage collection in Java and C#,
can help mitigate memory fragmentation.

However, in some languages like C++, the memory is managed by the


programmer manually, and it can result in fragmentation if not handled
properly. Understanding and managing memory fragmentation is
important to ensure efficient memory allocation and usage in a program.

Advantages of Memory Allocation for Objects


There are several advantages to understanding and effectively managing
memory allocation for objects in a program:

Efficient Memory Usage:


When the programmer properly does the memory allocation and
deallocation, then it helps to optimize the program. The programmers can
ensure that memory is used efficiently, reducing memory overhead, and
preventing memory wastage by allocating only the necessary amount of
memory and releasing it when it is no longer needed. Efficient memory
usage can result in improved performance and reduced resource
consumption, making programs more efficient and scalable.

Prevention of Memory Leaks:


When the memory is allocated but does not deallocate properly, then it
causes memory leaks that result in occupied memory that cannot be
reused by the system. Understanding memory allocation and deallocation
techniques helps to prevent memory leaks, ensuring that memory is
effectively released when it is no longer needed. This also helps to
prevent the accumulation of unused memory and helps maintain the
stability and reliability of a program.

Explain the concept of Class as an Abstract Data Type.

Abstract class in Java


A class which is declared as abstract is known as an abstract class. It
can have abstract and non-abstract methods. It needs to be extended and
its method implemented. It cannot be instantiated.
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change the
body of the method.

Example of abstract class

1. abstract class A{}

Abstract Method in Java


A method which is declared as abstract and does not have
implementation is known as an abstract method.

Example of abstract method

1. abstract void printStatus();//no method body and abstract

Method Overloading in Java


If a class has multiple methods having same name but different in
parameters, it is known as Method Overloading.
If we have to perform only one operation, having same name of the
methods increases the readability of the 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.

Advantage of method overloading

Method overloading increases the readability of the program.

Different ways to overload the method

There are two ways to overload the method in java

1. By changing number of arguments


2. By changing the data type

1) Method Overloading: changing no. of arguments


In this example, we have created two methods, first add() method
performs addition of two numbers and second add method performs
addition of three numbers

2) Method Overloading: changing data type of


arguments
In this example, we have created two methods that differs in data type.
The first add method receives two integer arguments and second add
method receives two double arguments

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

Java static keyword


The static keyword in Java is used for memory management mainly. We
can apply static keyword with variables, methods, blocks and nested
classes. The static keyword belongs to the class than an instance of the
class.

The static can be:

1. Variable (also known as a class variable)


2. Method (also known as a class method)
3. Block
4. Nested class

1) Java static variable


If you declare any variable as static, it is known as a static variable.

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.

You might also like