OOPS Concepts in Java - OOPS Concepts Example - JournalDev
OOPS Concepts in Java - OOPS Concepts Example - JournalDev
OOPS Concepts or Object Oriented Programming Concepts are very important. Without having an
idea about OOPS concepts, you will not be able to design systems in the object-oriented programming
model.
1 OOPS Concepts
1.1 Abstraction
1.2 Encapsulation
1.3 Polymorphism
1.4 Inheritance
1.5 Association
1.6 Aggregation
1.7 Composition
OOPS Concepts
7. Composition
Let’s look into these object-oriented programming concepts one by one. We will use Java
programming language for code examples so that you know how to implement OOPS concepts in
java.
Abstraction
Abstraction is the concept of hiding the internal details and describing things in simple terms. For
example, a method that adds two integers. The internal processing of the method is hidden from the
outer world. There are many ways to achieve abstraction in object-oriented programmings, such as
encapsulation and inheritance.
A Java program is also a great example of abstraction. Here java takes care of converting simple
statements to machine language and hides the inner implementation details from the outer world.
Encapsulation
Access modifier keywords are used for encapsulation in object oriented programming. For example,
encapsulation in java is achieved using private , protected and public keywords.
Polymorphism
Polymorphism is the concept where an object behaves differently in different situations. There are two
types of polymorphism – compile time polymorphism and runtime polymorphism.
Compile time polymorphism is achieved by method overloading. For example, we can have a class as
below.
https://www.journaldev.com/12496/oops-concepts-java-example 2/5
9/3/2019 OOPS Concepts in Java - OOPS Concepts Example - JournalDev
}
}
Here we have multiple draw methods but they have different behavior. This is a case of method
overloading because all the methods name is same and arguments are different. Here compiler will be
able to identify the method to invoke at compile time, hence it’s called compile time polymorphism.
Runtime polymorphism is implemented when we have “IS-A” relationship between objects. This is also
called as method overriding because subclass has to override the superclass method for runtime
polymorphism.
If we are working in terms of superclass, the actual implementation class is decided at runtime.
Compiler is not able to decide which class method will be invoked. This decision is done at runtime,
hence the name as runtime polymorphism or dynamic method dispatch.
package com.journaldev.test;
package com.journaldev.test;
@Override
public void draw(){
System.out.println("Drwaing circle");
}
package com.journaldev.test;
@Override
public void draw() {
System.out.println("Drawing Square");
}
https://www.journaldev.com/12496/oops-concepts-java-example 3/5
9/3/2019 OOPS Concepts in Java - OOPS Concepts Example - JournalDev
Shape is the superclass and there are two subclasses Circle and Square . Below is an
example of runtime polymorphism.
In above examples, java compiler don’t know the actual implementation class of Shape that will be
used at runtime, hence runtime polymorphism.
Inheritance
Inheritance is the object oriented programming concept where an object is based on another object.
Inheritance is the mechanism of code reuse. The object that is getting inherited is called superclass
and the object that inherits the superclass is called subclass.
package com.journaldev.java.examples1;
class SuperClassA {
a.foo();
a.bar();
}
}
Association
Association is the OOPS concept to define the relationship between objects. Association defines the
multiplicity between objects. For example Teacher and Student objects. There is one to many
relationship between a teacher and students. Similarly a student can have one to many relationship
with teacher objects. However both student and teacher objects are independent of each other.
Aggregation
Aggregation is a special type of association. In aggregation, objects have their own life cycle but there
is an ownership. Whenever we have “HAS-A” relationship between objects and ownership then it’s a
case of aggregation.
Composition
https://www.journaldev.com/12496/oops-concepts-java-example 5/5