Oops What Is OOPS?: - Ajay Wankhade
Oops What Is OOPS?: - Ajay Wankhade
OOPS
=====
What is OOPS?
Object Oriented Programming is a programming paradigm in which
programs are considered as a collection of objects. Each object is nothing
but an instance of a class.
Alan Kay developed the first object oriented programming language.
Object :
● An object is an instance of a class. Data members and methods of a
class cannot be used directly. We need to create an object (or
- Ajay Wankhade
instance) of the class to use them. In simple terms, they are the
actual world entities that have a state and behavior.
● When the company manufactures a car using the blueprint, the
actual car produced is an object. Each car produced from the
blueprint is an instance of the car class.
What is Abstraction ?
● Hiding internal implementation and just highlighting the set of
services is called abstraction.
● By using abstract classes and interfaces we can implement
abstraction.
● Example : An ATM machine provides an interface for users to perform
transactions like withdrawing money, checking balance, and
depositing money without exposing the internal operations of the
bank’s database.
● In other words, we can say that abstraction is a mechanism to show “
what is does” to user but to hide “how it does” from user.
What is Encapsulation ?
- Ajay Wankhade
What is Polymorphism ?
● One entity having multiple forms is the concept of polymorphism.
● Polymorphism is the ability of objects of different classes to respond
to the same method call in different ways. It allows methods to do
different things based on the object it is acting upon, even though
they share the same name
● Can be achieved through overloading and overriding.
● Ex : A person can be an employee, a parent, or a friend. The behaviour
of the person varies based on the role they are in, but the person
remains the same.
What is Inheritance ?
● Inheritance is a mechanism in object-oriented programming where a
new class, known as a subclass or derived class, inherits the
properties and behaviors (methods) of an existing class, referred to
as a superclass or base class.
● Also called the IS-A relationship.
● Ex: A car can be considered as a superclass with general properties
such as color, model, and methods like start() and stop(). A sports
car can be a subclass that inherits these properties and methods but
also has additional features like turbo boost.
- Ajay Wankhade
1. Single Inheritance :
● Single inheritance is when a class (subclass/child class) inherits from
only one superclass (parent class).
● Ex : A class "Dog" inherits from a superclass "Animal".
● A Car inherits from a Vehicle.The Vehicle class contains common
properties and methods such as `speed`, `fuel`, and `start()`.The Car
class inherits these properties and methods and may add specific
features like `playMusic()`.
3. Multilevel Inheritance
● Multilevel inheritance is when a class is derived from a class
which is also derived from another class, forming a hierarchy of
classes.A Laptop inherits from a Computer, which in turn
inherits from an ElectronicDevice.ElectronicDevice: Contains
properties like `powerOn()` and `powerOff()`. Computer: Inherits
from ElectronicDevice and adds properties like `runProgram()`.
Laptop: Inherits from Computer and adds features like
`portable()`.
4. Hierarchical Inheritance
● Hierarchical inheritance is when multiple classes inherit from a single
superclass.
● Example : Animal (Superclass): Contains methods like `eat()` and
`sleep()`.Dog (Subclass): Inherits from Animal and adds `bark()`. Cat
(Subclass): Inherits from Animal and adds `meow()`.
The diamond problem occurs when a class inherits from two classes that
have common methods. This can lead to a situation where the child class
has two copies of the same method, one from each parent class. The JVM
will not know which method to call and this can lead to errors.
this super
Are class and structure the same? If not, what's the difference between a
class and a structure?
- Ajay Wankhade
No, class and structure are not the same. Though they appear to be similar,
they have differences that make them apart. For example, the structure is
saved in the stack memory, whereas the class is saved in the heap memory.
Also, Data Abstraction cannot be achieved with the help of structure, but
with class, Abstraction is majorly used.
Types of Constructor ?
1.Default Constructor ( 0-args constructor )
A default constructor is automatically generated by the compiler if no
constructor is explicitly defined in the class. It does not take any arguments
and simply initializes the object.
Real-life Example: Imagine a new employee joins a company, and they are
automatically assigned default values for their profile information like
"Name: Unknown" and "ID: 0"
2.No-Arg Constructor
A no-arg constructor is explicitly defined in the class and does not take any
arguments. It can be used to initialize the object with default or initial
values. Real-life Example: A bank account is created with a default balance
of $0 and no account holder name.
3.Parameterized Constructor
- Ajay Wankhade
4.Copy Constructor :
What is a destructor?
Contrary to constructors, which initialize objects and specify space for
them, Destructors are also special methods. But destructors free up the
resources and memory occupied by an object. Destructors are
automatically called when an object is being destroyed
public sealed class Shape permits Circle, Square, Rectangle { // class body }
interface if our object gets some ability such type of interfaces are called
Marker interface (or) Tag interface (or) Ability interface
Eg : public interface Demo
{
//empty
}
1. Runnable :run()
2. Callable ; call()
3. Comparable :compareTo()
What is Coupling:
The degree of dependency between the components is called coupling.
more dependency -> tightly coupling
less dependency -> loosely coupling
What is Cohesion:
For every component a clear well defined functionality is defined then that
ocmponent is said to be following high cohesion.
- Ajay Wankhade
Exception Handling
================
Exception:
An unwanted unexpected event that disturbs normal flow of the program is
called an exception.
It is highly recommended to handle exceptions. The main objective of
exception handling is graceful (normal) termination of the program.
Exception hierarchy:
Throwable acts as a root for exception hierarchy. Throwable class contains
the following two child classes.
1. Exception: Most of the cases exceptions are caused by our program
and these are recoverable.
2. Error: Most of the cases errors are not caused by our program these
are due to lack of system resources and these are non recoverable.
2. PenNotWorkingException
3. FileNotFoundException
The exceptions which are not checked by the compiler are called
unchecked exceptions.
1) BombBlastException
2) ArithmeticException
3) NullPointerException
Note: RuntimeException and its child classes, Error and its child classes are
unchecked and all the remaining are considered as checked exceptions.
Finally block:
It is never recommended to take clean up code inside try block because
there is no guarantee for the execution of every statement inside a try.
It is never recommended to place clean up code inside catch block
because if there is no exception then catch block won’t be executed.
We require some place to maintain clean up code which should be
executed always irrespective of whether exception raised or not raised and
- Ajay Wankhade
whether handled or not handled, such type of block is nothing but finally
block.Hence the main objective of finally block is to maintain cleanup code.
Return Vs Finally:
Even though return present in try or catch blocks first finally will be
executed and after that only return statement will be considered that is
finally block dominates return statement
finally
It is the block always associated with try catch to maintain clean up code
which should be executed always irrespective of whether exception raised
or not raised and whether handled or not handled.
finalize:
It is a method which should be called by the garbage collector always just
before destroying an object to perform cleanup activities.
Note: To maintain clean up code finally block is recommended over
finalize() method because we can’t expect exact behaviour of GC.
throw statement:
Sometimes we can create exception objects explicitly and we can hand
over to the JVM manually by using throw keyword.
throws statement:
in our program if there is any chance of raising checked exception
compulsory we should handle either by try catch or by throws keyword
otherwise the code won’t compile Hence the main objective of “throws”
- Ajay Wankhade
Summary:
1) try: To maintain risky code.
2) catch: To maintain handling code.
3) finally: To maintain cleanup code.
4) throw: To handover our created exception object to the JVM manually.
5) throws: To delegate responsibility of exception handling to the caller
method.