Object Oriented Programming
Object Oriented Programming
Topic
:
Object
Oriented
Programming
1. Objects are the real world entities about which we code. Objects have
properties and they perform functions. For example in a student management
system the real world entities about which the system revolves are – students,
instructor, course, batch etc.
2. A Class is a template or a blue print and the objects are specific copies of it.
For example a Vehicle class might look like :
Here this is a keyword that refers to current object, So this.price refers to the
data member (i.e. price) of this object and not the argument variable price.
One important point to note here is that as soon as we create our
constructor the default constructor goes off.
Now when we have defined the above constructor and if it is the only
constructor in the class, then we can’t create any object of Vehicle without
giving its price. In a way we can actually restrict users that they can’t create a
vehicle without giving its price.
We can have more than one constructors within the same class (i.e
constructor overloading), which constructor will be called will be decided on
runtime depending on the type and number of arguments specified while
creating the object.
3. Modifiers
1. Static and Non-Static : Static properties are those that belong to the
class rather each specific object. So their separate copies aren’t
created. They are shared by all the objects of the class. You need to
write static keyword before it in order to make it static.
For e.g :
2. Access Modifiers
3. Protected : It is accessible within the same package and outside
the package but only through inheritance.
4. Public : It is accessible everywhere.
Components Of OOPS
1. Encapsulation - Binding (or wrapping) code and data together into a single
unit i.e a class is known as encapsulation. It lets us hide the implementation
details using different access modifiers. Also it lets us change the implementation
without breaking the code of users.
Here car (sub class) extends Vehicle (base class / super class) since every car
is a vehicle. So car will now have all the properties and functions that a vehicle
has except the private fields of vehicle class(since they are not inherited , but
they can be accessed via functions of base class that aren’t private).
• What if both the base class and sub class have function with same signature
i.e same name and arguments ? Say even car has a printDescription function
as in vehicle.
then
Car c = new Car();
c.printDescription(); // This will call car’s printDescription
then Car, which extends Vehicle needs to have a constructor that passes value
to the vehicle constructor which is implicitly called when we create an object
of car.
3.1. Ability of a variable to take different forms – A base class’ reference can refer
to the object of its sub class i.e we can do something like this –
Since every car is a vehicle so a vehicle(i.e. reference of type vehicle) can
refer to a car. And not just the car, reference “v” here can refer to object
of any other class that extends vehicle. But through this refernce “v” we
can access only those properties of car which even a vehicle has i.e.
3.2. Overriding the base class functions(Virtual Functions) – We have already seen
its example above in inheritance. When both base class and sub class have
functions of same signature then base class’ function is overriden by the
subclass’ function.
Amongst these three add functions which add will be called finally, will
be decided on runtime based on the type of parameters.
Constructor overloading is similar to function overloading. At runtime
while creating an object the number and type of parameters passed will
decide that which constructor will be called.
public Vehicle(){
}!
3.4. Ability of a function to work with parameters of subtypes – This one is just an
extension of first type.
This print function expects a vehicle, so we can pass a car(or object of any
of its subtype) to it i.e.
Exceptions
Types of Exception
Exception Handling
Try block - The code which can cause an exception is enclosed within try block.
Catch block - The action to be taken when an exception has occurred is done in
catch block. It must be used after the try block only.
Finally block - Java finally block is a block that is used to execute important
code such as closing connection, stream etc. Java finally block is always executed
whether exception is handled or not.
Note :
1. Whenever an exception occurs statements in the try block after the
statement in which exception occurred are not executed
2. For each try block there can be zero or more catch blocks, but only one
finally block.
A user defined exception is a sub class of the exception class. For creating an
exception you simply need to extend Exception class as shown below :
Throwing an Exception
Sometimes, it's appropriate for code to catch exceptions that can occur within it. In
other cases, however, it's better to let a method further up the call stack handle the
exception. For example if input to the factorial method is a negative number, then it
makes more sense for the factorial to throw an exception and the method that has
called factorial method to handle the exception.
Here is the code for the factorial method :