Sitemap

Java OOPs Concepts

4 min readFeb 18, 2023

--

Introduction

Java is one of the most popular programming languages in the world. It’s an object-oriented language that is used to build a wide variety of applications, from mobile apps to enterprise software. In this blog post, we’ll explore some important Java concepts, including SRP, DRY, Good Encapsulation, Data Hiding, toString, Setter, Getter, and Constructor Chaining.

SRP — Single Responsibility Principle

The Single Responsibility Principle (SRP) is a design principle in object-oriented programming. It states that a class should have only one reason to change. In other words, a class should have a single responsibility or a single job to do. This makes your code more modular and easier to maintain. By following the SRP, you can avoid having large, monolithic classes that are difficult to work with.

DRY — Don’t Repeat Yourself

The Don’t Repeat Yourself (DRY) principle is another important programming principle. It states that you should avoid duplicating code in your application. Instead, you should write reusable code that can be used throughout your application. This makes your code more efficient, easier to maintain, and less error-prone. By following the DRY principle, you can avoid having redundant code that is difficult to maintain.

Good Encapsulation

Encapsulation is a concept in object-oriented programming that refers to the practice of hiding the internal details of an object and providing a public interface for accessing it. Good encapsulation means that the internal state of an object is well-protected, and the public interface is well-designed and easy to use. By following good encapsulation practices, you can make your code more secure, maintainable, and scalable.

Data Hiding

Data hiding is an important part of encapsulation. It refers to the practice of hiding the internal state of an object so that it can’t be accessed directly from outside the object. This makes your code more secure, as it prevents external code from manipulating the internal state of an object. By following good data hiding practices, you can ensure that your objects are well-protected and your code is more maintainable.

toString

toString is a method in Java that is used to convert an object to a string representation. It’s often used to display the contents of an object in a user-friendly way. By providing a good toString method, you can make it easier to debug your code and work with objects in your application.

Get Kshitij Kumar’s stories in your inbox

Join Medium for free to get updates from this writer.

Setter and Getter

Setters and getters are methods in Java that are used to set and retrieve the values of private fields in an object. They are an important part of encapsulation, as they allow you to control how the internal state of an object is accessed from outside the object. By following good setter and getter practices, you can make your code more maintainable and secure.

Constructor Chaining

Constructor chaining is a technique in Java that allows you to call one constructor from another constructor in the same class. This can be useful for reducing code duplication and simplifying the process of creating new objects. By following good constructor chaining practices, you can make your code more efficient and easier to maintain.

Problem related to this topic

Create a Class Student , Student contains RollNo, Name, 3 Subject Marks

Total Marks , Percentage , Grade

>=90 A Grade
<90 to 70 B Grade
<70 to 60 C Grade
<60 to 50 D Grade
<50 F Grade

Rule : SRP , DRY , Gud Encapsulation , Data Hiding , toString, Setter, Getter, Constructor Chaining — Fit in this problem

import java.util.Arrays;
public class Student {
private int rollNo;
private String name;
private int[] subjectMarks;
private int totalMarks;
private double percentage;
private char grade;
public Student(int rollNo, String name, int[] subjectMarks) {
setRollNo(rollNo);
setName(name);
setSubjectMarks(subjectMarks);
calculateTotalMarks();
calculatePercentage();
calculateGrade();
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int[] getSubjectMarks() {
return subjectMarks;
}
public void setSubjectMarks(int[] subjectMarks) {
for (int i = 0; i < subjectMarks.length; i++) {
if (subjectMarks[i] > 100) {
subjectMarks[i] = 100;
}
}
this.subjectMarks = subjectMarks;
}
public int getTotalMarks() {
return totalMarks;
}
public double getPercentage() {
return percentage;
}
public char getGrade() {
return grade;
}
private void calculateTotalMarks() {
int total = 0;
for (int mark : subjectMarks) {
total += mark;
}
this.totalMarks = total;
}
private void calculatePercentage() {
this.percentage = (double) totalMarks / (subjectMarks.length * 100) * 100;
}
private void calculateGrade() {
if (percentage >= 90) {
grade = 'A';
} else if (percentage >= 70) {
grade = 'B';
} else if (percentage >= 60) {
grade = 'C';
} else if (percentage >= 50) {
grade = 'D';
} else {
grade = 'F';
}
}
public String toString() {
return "Roll No: " + rollNo + "\n"
+ "Name: " + name + "\n"
+ "Subject Marks: " + Arrays.toString(subjectMarks) + "\n"
+ "Total Marks: " + totalMarks + "\n"
+ "Percentage: " + percentage + "\n"
+ "Grade: " + grade;
}
}
import java.util.Scanner;
public class Student1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Roll No: ");
int rollNo = scanner.nextInt();
System.out.print("Enter Name: ");
String name = scanner.next();
int[] subjectMarks = new int[3];
for (int i = 0; i < subjectMarks.length; i++) {
System.out.print("Enter Marks for Subject " + (i+1) + ": ");
subjectMarks[i] = scanner.nextInt();
}
Student student = new Student(rollNo, name, subjectMarks);
System.out.println(student);
}
}

Conclusion

In this blog post, we’ve explored some important Java concepts, including SRP, DRY, Good Encapsulation, Data Hiding, toString, Setter, Getter, and Constructor Chaining. By following these concepts, you can write better code that is more maintainable, efficient, and secure. Whether you’re building a mobile app or enterprise software, these concepts are essential for creating high-quality code.

--

--