OOPS
OOPS
encapsulation, inheritance, and polymorphism. It allows users to create objects they want
and create methods to handle those objects.
The main aim of OOP is to bind together the data and the functions that operate on them so
that no other part of the code can access this data except that function.
Class = Vehicle
Object = A specific car model, such as Audi A4, BMW I8, Maruti Suzuki Vitara
Brezza, etc.
Characteristics = What is the color of your car? What is the Chassis number of
your car? etc
Behavior = How to start the car? How to change the gear of the car? etc.
Object - An object is an instance of a class. It has its own state, behavior, and
identity. For example, a chair, pen, table. An object contains an address and takes
up some space in memory. Objects can communicate without knowing the details of
each other's data or code.
Inheritence - When one object acquires all the properties and behaviors of a
parent object, it is known as inheritance. It provides code reusability.
When creating a class, instead of writing completely new data members and member functions, the
programmer can designate that the new class should inherit the members of an existing class. This
existing class is called the base class, and the new class is referred to as the derived class.
The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A
mammal hence dog IS-A animal as well, and so on.
If Inheritance applied to one class is called Single Inheritance, and if it depends on
multiple classes, then it is called multiple Inheritance. It is used to achieve runtime
polymorphism.
example of polymorphism: A person at the same time can have different characteristic. Like a man
at the same time is a father, a husband, an employee.
Compile time polymorphism, also known as Static Polymorphism, refers to the type of Polymorphism
that happens at compile time. What it means is the feature by which an object is linked with the
respective function or operator based on the values during the compile time. Static or Compile time
Polymorphism can be achieved through Method overloading or operator overloading.
Runtime polymorphism, also known as Dynamic Polymorphism, refers to the type of Polymorphism
in OOPs, by which the actual implementation of the function is decided during the runtime or
execution. The dynamic or runtime polymorphism can be achieved with the help of method
overriding.
Encapsulation – Binding (or wrapping) code and data together into a single
unit are known as encapsulation. & also it is the process of hiding unwanted information,
such as restricting access to any member of an object. A car is having multiple parts..like
steering,wheels,engine...etc..which binds together to form a single object that is car. So, Here
multiple parts of cars encapsulates itself together to form a single object that is Car.
A java class is the example of encapsulation. Java bean is the fully encapsulated
class because all the data members are private here.
Default constructor: The default constructor is the constructor which doesn’t take
any argument. It has no parameters.
Parameterized constructor: The constructors that take some arguments are known
as parameterized constructors.
Access modifiers are not used in procedural programming, which implies that the entire data
can be accessed freely anywhere in the program. In OOP, you can specify the scope of a
particular data by using access modifiers - public, private, internal, protected, and protected
internal.
An abstract class is a class that cannot be instantiated and is always used as a base class.. The
significance of abstract class is that the abstract methods inside it are not
implemented and only declared. So as a result, when a subclass inherits the abstract
class and needs to use its abstract methods, they need to define and implement
them.
Method Overloading involves the creation of two or more methods with the same name and same
signature in different classes (one of them should be parent class and other should be child).
An interface refers to a special type of class, which contains methods, but not their
definition. Only the declaration of methods is allowed inside an interface. To use an
interface, you cannot create objects. Instead, you need to implement that interface
and define the methods for their implementation.
A class acts as a blue-print that defines the properties, states, and behaviors that are common
to a number of objects. An object is an instance of the class. For example, you have a class
called Vehicle and Car is the object of that class. You can create any number of objects for
the class named Vehicle, such as Van, Truck, and Auto.
The new operator is used to create an object of a class. When an object of a class is
instantiated, the system allocates memory for every data member that is present in the class.
Class:
A class is a reference type. Such a variable does not contain an actual object, but rather a
reference to the class instance, or object, the variable refers to. Because using a class name
as a type declares a reference to an object.
While instantiating a class, CLR(vm that execute a program) allocates memory for its
instance in heap(is a memory to store global variables).
Classes support inheritance.
Variables of a class can be assigned as null.
Class can contain constructor/destructor.
Structure:
A structure is a value type means When a value-type instance is created, a single space in
memory is allocated to store the value .
In structure, memory is allocated on stack.
Structures do not support inheritance.
Structure members cannot have null values.
Structure does not require constructor/destructor and members can be initialiazed
automatically.
28) What are the access modifiers?
Access specifiers are a special type of keywords, which are used to control or specify
the accessibility of entities like classes, methods, etc. These access specifiers also
play a very vital role in achieving Encapsulation - one of the major features of OOPs .
There are five types of access modifiers, and they are as follows:
Private
Protected
Public
Friend
Protected Friend
Example
Call by Value – Value passed will get modified only inside the
function, and it returns the same value whatever it is passed
into the function.
Call by Reference – Value passed will get modified in both
inside and outside the functions and it returns the same or
different value.
The superclass is the parent class from which another class inherits.
All the methods inside an interface are always public, by default. You cannot specify any other
access modifier for them.
No, a class cannot inherit the constructor. Because Constructors are special and have same name as
class name. So if constructors were inherited in child class then child class would contain a parent
class constructor which is against the constraint that constructor should have same name as class
name.
It logically represents a single value, similar to primitive types (integer, double, and
so on).
It has an instance size smaller than 16 bytes.
It is immutable.
It will not have to be boxed frequently.
Cohesion - In object oriented design, cohesion refers all about how a single class is
designed. Cohesion is the Object Oriented principle most closely associated with making sure
that a class is designed with a single, well-focused purpose.
Coupling - in object oriented design, Coupling refers to the degree of direct knowledge
that one element has of another.
Local Variable - Local variables are those variables present within a block,
function, or constructor and can be accessed only inside them. The
utilization of the variable is restricted to the block scope. Whenever a local
variable is declared inside a method, the other class methods don’t have
any knowledge about the local variable.
Global variable - Instance variables are those variables that are accessible
by all the methods in the class. They are declared outside the methods and
inside the class. These variables describe the properties of an object and
remain bound to it at any cost.
Try and catch block - The try statement allows you to define a block of code to be
tested for errors while it is being executed. The catch statement allows you to define
a block of code to be executed, if an error occurs in the try block.
Static keyword - the keyword static indicates that the particular member belongs to a
type itself, rather than to an instance of that type. This means that only one instance of
that static member is created which is shared across all instances of the class.