What Are OOP Concepts in Java
What Are OOP Concepts in Java
For instance, a class of variable might be an address. The class might specify that
each address object shall have a name, street, city and zip code. The objects, in this
case, might be employee addresses, customer addresses or supplier addresses. In
addition, abstraction provides a mechanism for hiding the implementation details of a
class or method from the outside world and providing a simplified interface for clients to
interact with. In Java, you can achieve abstraction through two main mechanisms:
abstract classes and interfaces.
1. Abstract Classes: An abstract class is a class that you can’t instantiate and can
only extend by subclasses. Abstract classes can have both abstract and non-
abstract methods. Abstract methods do not have a body and you must implement
them by any subclass that extends the abstract class. Non-abstract methods
have a body and you can directly call them by the subclass.
2. Interfaces: An interface is a collection of methods. You can use it to define a set
of behaviors that a class should implement. A class can implement multiple
interfaces, and all the methods defined in an interface must be implemented by
any class that implements it.
Access Modifiers
1. Public: Public variables and methods can be accessed from anywhere, including
outside the class.
2. Private: Private variables and methods can only be accessed within the class
they are defined in.
1. Protected: Protected variables and methods can be accessed within the
same class and its subclasses.
Encapsulation enables developers to write cleaner, more organized, and more secure
code. By controlling access to variables and methods, encapsulation promotes good
software design practices and helps to manage the complexity of large-scale projects.
The subclass inherits all the public and protected variables and methods of the
superclass, and it can also define its own variables and methods. This makes it possible
to create a hierarchy of classes, where each subclass inherits from its superclass and
adds its own unique features.
Benefits of Inheritance
1. Reusability: By inheriting from a superclass, a subclass can reuse the code and
functionality already defined in the superclass, making it easier to write and
maintain code.
2. Polymorphism: Inheritance allows for polymorphism, where objects of different
subclasses can be treated as objects of the same superclass, making it easier to
write generic code.
3. Flexibility: Inheritance provides a way to add new features to an existing class
hierarchy without modifying the existing code.
Two more examples of polymorphism in Java are method overriding and method
overloading.
In method overriding, the child class can use the OOP polymorphism concept
to override a method of its parent class. That allows a programmer to use one method
in different ways depending on whether it’s invoked by an object of the parent class or
an object of the child class.
Benefits of Polymorphism
Polymorphism allows for more flexible and adaptable code. By enabling objects of
different classes to be treated as if they are of the same class, polymorphism promotes
code reuse, simplification, and flexibility, making it an essential component of Object-
Oriented Programming.
//save as Student.java
package com.javatpoint;
public class Student {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name
}
}
//save as Test.java
package com.javatpoint;
class Test {
public static void main(String[] args) {
Student s = new Student();
s.setName(“vijay”);
System.out.println(s.getName());
}
}
Compile By: javac -d . Test.java
Run By: java com.javatpoint.Test
Output: vijay
Example of Inheritance in Java
It’s quite simple to achieve inheritance as an OOP concept in Java. Inheritance can be
as easy as using the extends keyword:
class Mammal {
}
class Aardvark extends Mammal {
}
For a full tutorial on the different ways to use inheritance in java, see this blog post.
class Person {
void walk() {
System.out.println(“Can Run….”);
}
}
class Employee extends Person {
void walk() {
System.out.println(“Running Fast…”);
}
public static void main(String arg[]) {
Person p = new Employee(); //upcasting
p.walk();
}
}
Differences between OOP and other programming styles
Object-Oriented Programming (OOP) has become widely popular due to its many
advantages over other programming styles such as Procedural Programming and
Functional Programming.
Procedural Programming
Functional Programming
Here are some key differences between OOP and other programming styles:
1. Data and behavior: OOP is based on the idea of encapsulating data and
behavior within objects, whereas procedural programming separates data and
behavior into different functions or procedures. Functional programming, on the
other hand, treats data and behavior as separate entities altogether.
2. Inheritance and code reuse: OOP uses inheritance to reuse code and build
complex systems. Procedural programming and functional programming do not
have inheritance concepts built into them.
3. Flexibility: OOP is more flexible than procedural programming because it allows
for changes to be made to the underlying data structures and objects without
changing the entire system. In contrast, procedural programming requires a
complete restructuring of the program if any changes are made.
• DRY (Don’t Repeat Yourself). A core concept in Java, DRY simply means you
should never have two blocks of identical code in two different places. Instead,
have one method you use for different applications.
• If you expect your Java code to change in the future, encapsulate it by making all
variables and methods private at the outset. As the code changes, increase
access to “protected” as needed, but not too public.
• Single Responsibility. This best practice principle for OOP concepts in Java
states that a class should always have only one functionality. That way, the class
can be called and/or extended on its own when new uses arise for it, without
causing coupling between different functionalities.
• Open Closed Design. Make all methods and classes Closed for modification but
Open for an extension. That way, tried and tested code can remain static but can
be modified to perform new tasks as needed.