0% found this document useful (0 votes)
15 views36 pages

Object Oriented Procedure

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts, including classes, objects, inheritance, polymorphism, and methods in Java. It explains various OOP principles such as encapsulation, abstraction, and the significance of inheritance types, along with method overloading and overriding. Additionally, it covers advanced topics like upcasting, downcasting, the use of the super keyword, the final keyword, and the differences between call by value and call by reference.

Uploaded by

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

Object Oriented Procedure

The document provides a comprehensive overview of Object-Oriented Programming (OOP) concepts, including classes, objects, inheritance, polymorphism, and methods in Java. It explains various OOP principles such as encapsulation, abstraction, and the significance of inheritance types, along with method overloading and overriding. Additionally, it covers advanced topics like upcasting, downcasting, the use of the super keyword, the final keyword, and the differences between call by value and call by reference.

Uploaded by

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

Object Oriented Procedure

~Ganesh Jadhav
OOP’S Introduction

• Object-oriented programming is a core java which is used for designing a program


using classes and objects.
• Object-oriented programming is a model that provides different types of concepts,
such as inheritance, abstraction, polymorphism, etc.
• These concepts aim to implement real-world entities in programs.
• They create working methods and variables to reuse them without compromising
security.
• This emphasizes data rather than functions.
• This can also be characterized as data controlling for accessing the code.
• Many of the most widely used and significant object-oriented programming
languages include Java, C++, C#, JavaScript, Python, Ruby, Perl, Smalltalk etc.

Any OOP language supports the following features


• Classes
• Objects
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
Class

• It is a template or blueprint from which objects are created.


• It is a logical entity. It can't be physical.
• A class is a group of objects which have common properties.
A class in Java can contain:
• Fields
• Methods
• Constructors
• Blocks
• Nested class and interface
• Instance variable in Class
Object

• An object represents a real-life entity. In Java, an object is an instance of a class.


• So, a class which is a blueprint may be used to create many objects.
• These objects often communicate with each other by passing messages to each
other through methods.
An object typically has:
A state: The properties or attributes of an object at a particular time.
Behavior: Methods represent the behavior of an object. Methods also define how
the objects communicate.
Identity: Identifies the object by giving it a unique name.
Ex. Pen is an object. Its name is Reynolds; color is white, known as its state. It is
used to write, so writing is its behavior.
Java Methods

• A method is a block of code which only runs when it is called.


• You can pass data, known as parameters, into a method.
• Methods are used to perform certain actions, and they are also known as functions.
• In Java, a method is like a function which is used to expose the behavior of an
object.

Advantage of Method
• Code Reusability
• Code Optimization
Inheritance

• Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviors of a parent object.
• The idea behind inheritance in Java is that you can create new classes that are built
upon existing classes.
• When you inherit from an existing class, you can reuse methods and fields of the
parent class.
• Moreover, you can add new methods and fields in your current class also.
• Inheritance represents the IS-A relationship which is also known as a parent-child
relationship.
• The extends keyword indicates that you are making a new class that derives from
an existing class.
• The meaning of "extends" is to increase the functionality.
• A class which is inherited is called a parent or superclass, and the new class is
called child or subclass.

Why use inheritance in java


• For Code Reusability.
Terms used in Inheritance

• Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a
derived class, extended class, or child class.
• Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It
is also called a base class or a parent class.

Types of inheritance in java


• Single
• Multilevel
• Hierarchical
• Multiple
• Hybrid
Single Inheritance
• When a class inherits another class, it is known as a single inheritance.

Multilevel Inheritance
• When there is a chain of inheritance, it is known as multilevel inheritance.

Hierarchical Inheritance
• When two or more classes inherits a single class, it is known as
hierarchical inheritance.
Multiple Inheritance
• Multiple inheritances is a type of inheritance where a subclass can inherit features
from more than one parent class.

Hybrid Inheritance
• Hybrid inheritance is a type of inheritance that combines single inheritance and
multiple inheritances.
• As multiple inheritances is not supported by Java, hybrid inheritance can also be
achieved through interfaces only.
Q) Why multiple inheritance is not supported in java?

• To reduce the complexity and simplify the language, multiple inheritance is not
supported in java.
• Consider a scenario where A, B, and C are three classes. The C class inherits A and
B classes.
• If A and B classes have the same method and you call it from child class object,
there will be ambiguity to call the method of A or B class.
• Since compile-time errors are better than runtime errors, Java renders compile-time
error if you inherit 2 classes.
• So, whether you have same method or different, there will be compile time error.
Polymorphism

• Polymorphism in Java is a concept by which we can perform a single action in


different ways.
• Polymorphism is derived from 2 Greek words: poly and morphs. The word
"poly" means many and "morphs" means forms. So, polymorphism means
many forms.
• In Java polymorphism is mainly divided into two types:
1. compile-time polymorphism / static polymorphism/ method overloading
2. runtime polymorphism / Dynamic polymorphism / method overriding
Method Overloading/static/compile-time polymorphism

• If a class has multiple methods having same name but different in parameters, it
is known as Method Overloading.
• Suppose you have performed 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
Method Overloading: changing no. of arguments
• we have created two methods, first add() method performs addition of two numbers and
second add method performs addition of three numbers.
• In this example, we are creating static methods so that we don't need to create instance
for calling methods.

class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}}
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.

class 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));
}}
Q) Why Method Overloading is not possible by changing the return type of
method only?
• In java, method overloading is not possible by changing the return type of the
method only because of ambiguity. Let's see how ambiguity may occur:

class Adder{
static int add(int a,int b){return a+b;}
static double add(int a,int b){return a+b;}
}
class TestOverloading3{
public static void main(String[] args){
System.out.println(Adder.add(11,11));//ambiguity
}}
Can we overload java main() method?

• Yes, by method overloading. You can have any number of main methods in a class
by method overloading. But JVM
• calls main() method which receives string array as arguments only.

class TestOverloading4{
public static void main(String[] args){System.out.println("main with String[]");}

public static void main(String args){System.out.println("main with String");}


public static void main(){System.out.println("main without args");}
}
Runtime polymorphism / Dynamic polymorphism/method overriding

• Overriding is a feature that allows a subclass or child class to provide a specific


implementation of a method that is already provided by one of its super-classes or
parent classes.
• When a method in a subclass has the same name, same parameters or signature, and
same return type(or sub-type) as a method in its super-class, then the method in the
subclass is said to override the method in the super-class.

Rules for Java Method Overriding


• The method must have the same name as in the parent class
• The method must have the same parameter as in the parent class.
• There must be an IS-A relationship (inheritance).
Java Runtime Polymorphism Example: Bank
• Consider a scenario where Bank is a class that provides a method to get the rate of
interest.
• However, the rate of interest may differ according to banks. For example, SBI, ICICI,
and AXIS banks are providing 8.4%, 7.3%, and 9.7% rate of interest.
Understanding the problem without method overriding
• Let's understand the problem that we may face in the program if we don't use method
overriding.
• Problem is that I must provide a specific implementation of run() method in subclass that is
why we use method overriding.

Can we override static method?


• No, a static method cannot be overridden. It can be proved by runtime polymorphism,.

Why can we not override static method?


• It is because the static method is bound with class whereas instance method is bound with an
object. Static belongs to the class area, and an instance belongs to the heap area.

Can we override java main method?


• No, because the main is a static method.
No. Method Overloading Method Overriding
1. Method overloading is used to increase the Method overriding is used to provide the
readability of the program. specific implementation of the method that
is already provided by its super class.

2. Method overloading is performed within class. Method overriding occurs in two


classes that have IS-A (inheritance)
relationship.
3. In case of method overloading, parameter must be In case of method overriding, parameter
different. must be same.
4. Method overloading is the example of compile time Method overriding is the example of run
polymorphism. time polymorphism.
5. In java, method overloading can't be performed by Return type must be same or covariant in
changing return type of the method only. Return type method overriding.
can be same or different in method overloading. But
you must have to change the parameter.
Upcasting/Downcasting in Java

• Typecasting is the process of conversion of the type of data.


• Object Typecasting can be of two types, depending on whether we are converting
from parent type to child or going in the other way.
• We have two type of type casting.
• Upcasting
• Downcasting

Upcasting
• Upcasting gives us the flexibility to access the parent class members, but it is not
possible to access all the child class members using this feature.
• Object of child class is assigned to reference variable of parent type.
• Ex. Parent p = new Child()
• If the method is not overridden in child’s class, then only parent’s method which
will be inherited to child will be called.
• But same is not applicable to variables because variables decision happens at a
compile time, so always class A’s variables (not child’s inherited variables) will be
accessed.
• As of now there is no use of upcasting in java.
Downcasting

• Downcasting means the typecasting of a parent object to a child object.


• Implicitly Downcasting is not possible.
• Ex. Parent p = new Child() -> UpCasting
Child c = new Parent() -> Downcasting (compile time error we get)
Child c = (Child)p;

Why do we need Downcasting in Java?


• We use Downcasting whenever we want to access behaviors of the subtypes.
Super Keyword in Java

• The super keyword in Java is a reference variable which is used to refer immediate parent class
object.
• Whenever you create the instance of subclass, an instance of parent class is created implicitly
which is referred by super reference variable.

Usage of Java super Keyword


• super can be used to refer immediate parent class instance variable.
• super can be used to invoke immediate parent class method.
• super() can be parent class constructor.

1) super is used to refer immediate parent class instance variable.


• We can use super keyword to access the data member or field of parent class. It is used if parent
class and child class have same fields.
2) super can be used to invoke parent class method
• The super keyword can also be used to invoke parent class method.
• It should be used if subclass contains the same method as parent class.
• In other words, it is used if method is overridden.
• In the example Animal and Dog both classes have eat() method if we call eat() method
from Dog class, it will call the eat() method of Dog class by default because priority is
given to local.

3) super is used to invoke parent class constructor.


• super() is added in each class constructor automatically by compiler if there is no
super() or this().
• As we know well that default constructor is provided by compiler automatically if there
is no constructor. But it also adds super() as the first statement.
Final Keyword in java
• The final keyword in java is used to restrict the user.
• The java final keyword can be used in many context.
• Final can be:
1. variable
2. method
3. Class

Java final variable


• If you make any variable as final, you cannot change the value of final variable(It
will be constant).
• A static final variable that is not initialized at the time of declaration is known as
static blank final variable. It can be initialized only in static block.
Java final method
• If you make any method as final, you cannot override it.

Java final class


• If you make any class as final, you cannot extend it.

Q) What is blank or uninitialized final variable?


• A final variable that is not initialized at the time of declaration is known as blank
final variable.
• If you want to create a variable that is initialized at the time of creating object and
once initialized may not be changed, it is useful.
• It can be initialized only in constructor.
Q) Can we initialize blank final variable?
• Yes, but only in constructor.

Q) What is final parameter?


• If you declare any parameter as final, you cannot change the value of it.

Q) Can we declare a constructor final?


No, because constructor is never inherited.

Q) Is final method inherited?


Ans) Yes, but you cannot override it.
Call by Value & Reference in java

Call by Value
• If we call a method passing a value, it is known as call by value.
• The changes being done in the called method, is not affected in the calling method.
• Call by Value means calling a method with a parameter as value. Through this, the
argument value is passed to the parameter.
• In call by value, the modification done to the parameter passed does not reflect in the
caller's scope.
Call by Reference in Java

• While Call by Reference means calling a method with a parameter as a reference.


• Through this, the argument reference is passed to the parameter.
• If we pass object in place of any primitive value, original value will be changed.
• while in the call by reference, the modification done to the parameter passed are
persistent and changes are reflected in the caller's scope.

You might also like