corejavanotes
corejavanotes
What is OOPs?
It stands for Object-Oriented Programming.
It is based on objects
It follows Bottom-up programming approach.
It is based on real world.
It provides data hiding so it is very secure.
It provides reusability feature.
What is a class?
A class is a collection of objects. Classes don’t consume any space in the memory.
It is a user defined data type that act as a template for creating objects
of the identical type.
A large number of objects can be created using the same class. Therefore, Class is
considered as the blueprint for the object.
What is an object?
An object is a real world entity which have properties and functionalities.
Object is also called an instance of class. Objects take some space in memory.
For eg .
Fruit is class and its object s are mango ,apple , banana
Furniture is class and its objects are table , chair , desk
Class Object
1. It is a collection of objects. It is an instance of a class.
4. Classes are declared just once Objects can be declared as and when required
What is the difference between a class and a structure?
Class Structure
1.Class is a collection of objects. Structure is a collection of variables of
different data types under a single unit
2. Class is used to combine data and methods Structure is used to grouping data.
together.
3. Class's objects are created on the heap Structure's objects are created on the
memory. stack memory.
Inheritance Abstraction
OOP
Encapsulation Polymorphism
Encapsulation
The main advantage of encapsulation is that data is hidden and protected from randomly access
by outside non-member methods of a class.
In encapsulation, data(variables) are declared as private and methods are declared as public.
What are access specifiers ?
It allows us to restrict the scope or visibility of a package, class, constructor, methods,
variables, or other data members.
There are three types of most common access specifiers, which are following.
• Private
• Public
• Protected
Public Modifiers :
means that class, variable or method is accessible throughout from within or outside
the class, within or outside the package, etc.
Private Modifiers :
means that class, variable or method is not accessible from within or outside the
class, within or
outside the package, etc.
Protected Modifiers :
means that class, variable or method is accessible from classes in the same package,
sub-classes in
the same package, subclasses in other packages but not accessible from classes in
other packages.
Allows to hide unnecessary data from the user. This reduces program complexity
efforts.
it displays only the necessary information to the user and hides all the internal
background details.
Eg.
-All are performing operations on the ATM machine like cash withdrawal etc.
but we can't know internal details about ATM
Abstract methods are those methods which have only declaration not the
implementation.
Inheritance is the procedure in which one class inherits the attributes and methods
of another class.
In other words It is a mechanism of acquiring properties or behaviors of existing
class to a new class
The Base Class, also known as the Parent Class is a class, from which other classes are
derived.
The Derived Class, also known as Child Class, is a class that is created from an existing
class
When more than class inherit properties and behaviour of only one class
In Hierarchical Inheritance there are only one parent and many child class
Multi-Level Inheritance
In this type of inheritance, a derived class is created from another derived class
Multiple Inheritance
When a class inherits the properties and the behaviour of more than one class
Java, C#, most of high level language don’t support Multiple Inheritance
Hybrid Inheritance
Ambiguity Around The Diamond Problem Multiple inheritance does complicate the
design and creates problem during casting, constructor chaining etc.
Polymorphism
Polymorphism
Many Forms
polymorphism is mainly divided into two types:
• Compile time Polymorphism (CTP)
Function overloading:
When there are multiple functions with same name but different parameters then these
functions are said to be overloaded. Functions can be overloaded by change in number of
arguments or/and change in type of arguments
multiple methods of same names performs different tasks within the same class.
2. Runtime polymorphism:
Runtime polymorphism refers to the process when a call to an overridden process is
resolved at the run time.
This type of polymorphism is achieved by Function Overriding.
Function Overriding:
on the other hand, occurs when a derived class has a definition for one of the member
functions of the base class.
methods having same name which can have different functionalities.
That base function is said to be overridden.
Different between Abstract Classes & Interfaces
Features Abstract Class Interface
Multiple Inheritance A class can inherit only one abstract A class can inherit multiple
class interfaces
Default Implementation Provide signature ,partial and full Provide only the signature of
implementation of its methods its methods ,variables
,variables and other members ,properties and other member
Access Modifier It allows to assign access modifier No access modifier can be
to its members assigned .All the members are
treated as public
Core VS Peripheral It defines the core identity of the It identify the peripheral
class and there is used for objects identity of the class .it means
of same type human and vehicle can inherit
from IMovable interface
Homogeneity If various implementation of same If various implementation of
nature which requires shared code different nature and requires
that represent same status or the member with same
behaviour , then use Abstract class signature ,then use interface
Performance It is faster to access the It takes time to find the
implemented class member members of the
corresponding class
Extensibility (Versioning) If any changes made to the abstract If any changes made to the
class ,not necessarily interfaces , changes should be
We need to change all the made in all the implemented
implementation classes classes
Field and Constants Fields and constants can be defined No fields and constants can be
defined
What is static function?
Static functions are those functions that can be called without creating an object of the
class. That means, Static methods do not use any instance variables of any object of the
class they are defined in.
Static methods can not be overridden. They are stored in heap space of the memory.
Virtual function defined in the base class and overridden in the inherited class.
The Virtual function cannot be private, as the private functions cannot be overridden. It is
used to achieve runtime polymorphism.
A pure virtual function have not definitions but we must override that function in the
derived class, otherwise the derived class will also become abstract class.
What is Constructor?
Constructor is a special type of member function which is used to initialize an object.
It is similar as functions but it's name should be same as its class name and must have no
explicit return type.
It is called when an object of the class is created. At the time of calling constructor,
memory for the object is allocated in the memory.
We use constructor to assign values to the class variables at the time of object creation.
What are the types of Constructor?
Constructor have following types –
• Default constructor
• Parameterized constructor
• Copy constructor
• Static constructor
• Private constructor
It copy variables from another object of the same class to create a new object.
What is destructor?
Destructor is a type of member function which is used to destroy an object.
It is called automatically when the object goes out of scope or is explicitly destroyed by a
call to delete.
A destructor has the same name as the class, preceded by a tilde (~).
Shallow Copy and Deep Copy
Shallow Copy and Deep Copy play important role in copying the objects in Prototype Design Pattern.
Shallow copy
In the case of Shallow copy, it will create the new object from the existing object and then copying the
value type fields of the current object to the new object.
But in the case of reference type, it will only copy the reference, not the referred object itself.
Therefore the original and clone refer to the same object in the case of reference type. In order to
understand this better, please have a look at the following diagram.
Deep Copy
In the case of deep copy, it will create the new object from the existing object and then copying the
fields of the current object to the newly created object. If the field is a value type, then a bit-by-bit copy
of the field will be performed. If the field is a reference type, then a new copy of the referred object is
created.
As shown in the above image, the Name and Department properties are value types so it creates a
copy of that and stores it in a different location. The EmpAddress is a Reference type property and in
Deep Copy there is a clone of the reference type field which also will be stored in a different location.
So, the point that you need to keep in mind is, in the case of Deep Copy the field type does not matter
whether it is a value type or reference type. It always makes a copy of the whole data and stores it in a
different memory location.
In C++ we can pass arguments into a function in different ways. These different ways are
Call by Value
Call by Reference
Call by Address
Sometimes the call by address is referred to as call by reference, but they are different in C++. In call by
address, we use pointer variables to send the exact memory address, but in call by reference we pass the
reference variable (alias of that variable). This feature is not present in C, there we have to pass the pointer
to get that effect. In this section we will see what are the advantages of call by reference over call by value,
and where to use them
Call by Value
In call by value, the actual value that is passed as argument is not changed after performing
some operation on it. When call by value is used, it creates a copy of that variable into the
stack section in memory. When the value is changed, it changes the value of that copy, the
actual value remains the same.
Example Code
#include<iostream>
using namespace std;
void my_function(int x) {
x = 50;
cout << "Value of x from my_function: " << x << endl;
}
main() {
int x = 10;
my_function(x);
cout << "Value of x from main function: " << x;
}
Output
Value of x from my_function: 50
Value of x from main function: 10
Call by Reference
In call by reference the actual value that is passed as argument is changed after performing
some operation on it. When call by reference is used, it creates a copy of the reference of
that variable into the stack section in memory. Is uses a reference to get the value. So
when the value is changed using the reference it changes the value of the actual variable.
#include<iostream>
using namespace std;
main() {
int x = 10;
my_function(x);
cout << "Value of x from main function: " << x;
}
Output
Value of x from my_function: 50
Value of x from main function: 50