Object Oriented Procedure
Object Oriented Procedure
~Ganesh Jadhav
OOP’S Introduction
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.
• 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.
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
• 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.
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[]");}
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
• 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.
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