Professional Documents
Culture Documents
OOPs Concept
OOPs Concept
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.
1) Inheritance
2) Polymorphism
3) Encapsulation
4) Abstraction
Inheritance
Acquiring or inheriting the property of one class to another class is called inheritance.
Class A Class B
Class A ds Class B
e n
ext Int y
Int x
syso( x, y)
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.
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();
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.
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 Method area
• Purpose is code readability
• Method overloading can be performed with static method and non-static method.
• 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.
Heap area
}
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
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 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.
object x
{
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.
The required encapsulated data can be accessed by using public methods (Getters and Setters).
}
Abstraction
It is the process of hiding the implementation and showing only the functionality to the user.
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
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.
}
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.
Example
}
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();
}
Interface1 Interface2
Implementation classA
implements implements
• 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();