You are on page 1of 31

OOP’s Concepts (Object oriented Programming)

Like real world objects we create objects in java and use them efficiently with the help of OOP’s concepts.

Basically, Java OOP concepts let us create working methods and variables, then re-use all or part of them
without compromising security.

Four OOP’s principles

1) Inheritance
2) Polymorphism
3) Encapsulation
4) Abstraction
Inheritance

Acquiring or inheriting the property of one class to another class is called inheritance.

Father / Super Class Child / Sub Class

Class A Class B

To achieve this we need to use “extends” keyword

Example public class ClassB extends ClassA


Four types of Inheritance
1) Single level Inheritance
2) Multi level inheritance
3) Multiple Inheritance
4) Hierarchical inheritance

1) Single level inheritance


In this inheritance only two classes are involved, one is super class and another is sub class.

Super Class Sub Class

Class A ds Class B
e n
ext Int y
Int x
syso( x, y)

public class ClassB extends ClassA


2) Multi level inheritance
In this inheritance more than two classes are involved.

One class acquires the property of super class and this super class also aquires the property of another super class.

Super Class

Class A
Int x
Sub Class of Class A
ext
e nd
s Class B
Super class for Class B Int y
Sub Class of Class B
syso( x, y) ext
e nd
s Class C
public class ClassB extends ClassA
Int z
Super class for Class C
syso( x, y, z)
public class ClassC extends ClassB
3) Multiple inheritance
In this inheritance more than two classes are involved.
In this one class is acquiring the property of two super classes.

Super Class Super Class

Class A Class B This type of inheritance is not


Int x Int y possible in JAVA.
syso( x, y) Reason

Because of Diamond ambiguity


ex
te
nd Sub Class nds
e
s ext
Class C
Int z

syso( x, y, z)
Diamond Ambiguity This type of inheritance is not
possible in JAVA.
Supermost Class
Reason
Class Object
Because of Diamond ambiguity

finalize();

• Object class is the supermost class of


Super Class Super Class each and every class in java.

Class A Class B 2
1
Int x Int x
OBJECT
OBJECT

ex SUPER
te
nd Sub Class
s SUPER
extends
Class C ex
ten SUB
Int y d s SUB
syso( x, x, y)
4) Hierarchical inheritance
In this inheritance more than two classes are involved.

More than one sub classes has same super class.

Super Class

Class A
Int x
nd s
exte ext
e nd
s
Sub Class of Class A
Sub Class of Class A
Class C
Class B Int z
Int y
syso( x, y, z)
syso( x, y)

public class ClassB extends ClassA public class ClassC extends ClassA
Important concepts of Methods Method declaration
Public void demoMethod()
1) Method overloading
2) Method overriding
{
3) Method hiding

JVM Memory }

Method definition
Stack Static pool area
Public static void main (String[] args) Only static method declaration
{
Example
Static void demo()
}

Method area Heap area


Only non-static method declaration
Static method body
Example
Public void demo()
non-static method body
1) Method Overloading
Declaring multiple method with same name and different argument in same class
this is called as method overloading
Static non-static

Static pool area Heap area


Public class Test{ Public class Test{
Test.demo() x.demo()
Public static void demo() Public void demo()
{ Zero argument { Zero argument
syso(“HI”); syso(“HI”);
} Int argument } Int argument

Public static void demo(int a) { Public void demo(int a) {


syso(“Hi”); syso(“Hi”);
{ }
{ }
syso(“Hello”); syso(“Hello”);
} { } {
syso(“Hello”); syso(“Hello”);
} }

} }
Method area Method area
• Purpose is code readability

• Method overloading can be performed with static method and non-static method.

• In overloading all methods must be static or all methods must be non-static.

• Method overloading not possible between static and non-static method, because static methods stored in
static pool area and non-static methods stored in heap area.

• It is independent on return type of method.


2) Method Overriding (advantage of inheritance)
While acquiring the methods of super class in to sub class in inheritance and changing the implementation (body) of it
is called method overriding.

Heap area

Public class subclass extends Test{ .demo()

Public class Test{ Public void demo() object y


{
object x
Public void demo() syso(“Hello”);
{ }
syso(“HI”); {
syso(“Hi”);
} Public static void main(String[] args) }
{
} subclass x = new subclass(); {
syso(“Hello”);
}
Test y = new Test();
}
Method area
}
• If we override static method then it is called as method hiding.

3) Method Hiding (disadvantage of inheritance)


While inheriting static method having same name and same parameters, When we call that static
method in subclass it always call subclass method.
Static pool area
Super class method will get hide behind sub class method.
Test.demo()
Best example is main method. subclass.demo()

Public class subclass extends Test{

Public static void demo()


Public class Test{ {
syso(“Hello”); {
Public static void demo() } syso(“Hi”);
{ }
syso(“HI”); Public static void main(String[] args)
} { {
demo(); syso(“Hello”);
} } }

}
Method area
• Can we override main method?

No, We cant override main method, because it is static method. If we try to override it will get hide
behind subclass main method
Polymorphism
One element or object performs the different behavior at different stages of life cycle
is called as polymorphism.

Boy

Hospital
College Patient
Student

Shop

Customer
Two types of polymorphism

1) Compile time polymorphism


2) Run time polymorphism

These types are depend on binding of method declaration with method definition(Body)

Method declaration
Public void demoMethod()

{
Syso(“Hii”);
}

Method definition
Compile time polymorphism (Static polymorphism / static binding)

Method declaration and method definition are binded together at the time of program compilation
is called as compile time polymorphism

In method overloading method declaration and method definition is binded together


with reference of arguments (Parameters) at the time of compilation

Method overloading is best example of Compile time polymorphism

For static methods For non-static methods

Binding and Initialization at the time Binding at the time of compilation.


of compilation. Initialization at the time of object creation (Run time).

In method overloading declaration blinded with definition with the help of arguments.
Run time polymorphism

Method declaration and method definition are binded together at the time of program execution
Based on object creation is called runtime polymorphism.

In method overriding method declaration and method definition is binded together


with reference of reference variable of object at run time
Heap area
Method overriding is best example of Run time polymorphism
.demo()

object x

For non-static methods object y

Binding and Initialization happens at the time {


of object creation (Run time) syso(“Hi”);
}

{
syso(“Hello”);
}

Method area
Encapsulation

Hiding the data members from other classes directly and accessing them with the help of public
methods is call encapsulation.

How to achieve encapsulation

In encapsulation technique we declare fields as private in the class to prevent them


to access directly from other classes.

The required encapsulated data can be accessed by using public methods (Getters and Setters).

This concept is also called as data hiding.


Example Public class User{
The android application having user details
1) username Private String userName;
2) userEmail Private String userEmail;
3) userPassword Private String userPassword;
4) userPhone Private String userPhone;
5) userCity Private String userCity;

Public String getUserName()


{
Advantages return userName;
}
1) It provides security to our data no one can
access them directly. Public void setUserName(String name)
2) Encapsulation allows modifying implementation. {
3) If we don’t define setters then only field can be this.userName = name;
made read-only. }

}
Abstraction
It is the process of hiding the implementation and showing only the functionality to the user.

• Hiding implementation means hiding the code inside method.


• Showing only functionality to user means, showing only function or method name.

Examples

1) Fan - When we press the fan button we can see fan is running but we don’t know
how it is running (Internal functionality)

2) Break - When we press break of car, car will stop but we don’t know how it is stopping the car
(Internal functionality).

3) Sending an Email – While sending email it asks only sender email id, attachment and body and sends mail
We don’t know how it sends mail.
In overriding

ClassA ClassA extends ClassB

Public void demo() Public void demo()


{ {
syso(“demo printed”)
} }

Abstraction is achieved by

1) Abstract Class
2) Interface
1) Abstract Class
• Abstract class created with abstract keyword.
• Abstract class contains
1) abstract methods
2) concrete methods (non-abstract methods)
3) constructors
4) It also contains final and static methods.

To complete abstract methods from abstract class we create concrete class.

We have to give implementation to all the methods from abstract class.


Example

Abstract Class Concrete Class

Public Abstract class abst{ Public class extends abst{

abstract public void m1();


public void m1()
{
public void m2() syso(“Abstract method completed”);
{ }
syso(“ printing concrete method”);
} public void m2()
{
syso(“Implementation Changed”);
final public void m3() }
{
syso(“Implementation can not be changed”); }
}

}
Abstract class

extends
extends

extends
Class A Class B Class C
2) Interface
Interface is blueprint of class in which all variables are constants and all methods are abstract methods.

Interface is 100% abstract in nature.

• All abstract methods of interface is completed in implementation class.


• To inherit interface in class we use implements keyword.

• Constant variables means - public static and final


• Abstract methods means - public abstract method

Example

Variables  String abc;  public static final String abc;

Methods  void abc();  public abstract void abc();

Interface is mainly used to achieve abstraction and multiple inheritance .


• Interface do not have any constructor so we can not instantiate interface.

interface Implementation Class

Public interface intf1 { Public class implements intf1{

String a = “velocity”; public void m1()


Int b = 100; {
syso(“m1 completed in implementation class”);
void m1(); }

void m2(); public void m2()


{
syso(“m2 completed in implementation class”);
} }

}
A class can implement more than one interface. A interface does not have any super most class
like Object (no diamond ambiguity problem). interface
One interface inherit another interface.
Public interface intf1 {

String a = “velocity”;
In jdk 8 we can add default methods in interface. Int b = 100;

void m1();

void m2();

default void m3()


{
syso(“M3 from interface”)
}

}
Interface1 Interface2

Implementation classA
implements implements

public class ClassA implements interface1, interface2


If two variables of same name from different interface
Variables are always static in interface so we have to call them with interface name.

If two methods with same name from different interface


We will take one method of same name in implementation class and give them body,
so method called from implementation class.

If two default methods with same name from different interface


We will make one public method of same name, and inside that method we have to mention
that method is calling from which interface with the help of super keyword.
Generalization in java
Extracting all the common and important properties from all the classes and declare them in
one super file this concept is called generalization.

• It is common and generic concept in any object oriented language.

• Generalization file can be super class, abstract class or interface but always interface
is preferred because of multiple inheritance.
RBI
Void interestRate();

Void creditScore();
Void WithdrawalLimit();

SBI HDFC KOTAK


Public void interestRate() Public void interestRate() Public void interestRate()
{ syso(5.6%) }; { syso(6.5%) }; { syso(7%) };

Public void ceditScore() Public void ceditScore() Public void ceditScore()


{ syso(740) }; { syso(730) }; { syso(730) };

Public void withdrawalLimit() Public void withdrawalLimit() Public void withdrawalLimit()


{ syso(60000) }; { syso(75000) }; { syso(80000) };

You might also like