In this, we understand the Object-Oriented Programming (OOP) paradigm of Java, deeply with this table of content…
What is Object-Oriented Programming and Other Programming Paradigms
Object-oriented programming System(OOPs) is a programming concept based on the objects that interact with each other to perform the functions. Each object can be characterized with behavior and states. An object keeps the current state and the behavior in the fields and methods. It emphasizes the DRY(Don’t Repeat Yourself) Principle.
For instance, an object could represent an email with properties like subject, title, body, and behaviors such as attachments, sending, organizing.
OOPs categorizes entities as software objects that have some data associated with them to perform operations upon.
Another common programming concept is Procedural Oriented Programming(POP), which structures a program like a food recipe that provides a finite number of steps, in the form of functions or code blocks, that flow sequentially in order to get the final result.
Almost all the programming languages follow Object Oriented Programming System including Java, let us now jump to the concepts of OOPs in java.
Major OOPs Concepts In Java
Classes In Java
A class defines the attributes and behavior of an object. For example, if you have an Employee class, it can have attributes like name, salary, and age, whereas its behavior can include functions such as deduct or increment salary or calculate bonus, etc.
Hope you understood Classes in java, next major concept of object-oriented programming is the Objects in Java.
Objects In Java
Objects are the instances of classes or derived attributes and behaviors from class with actual data. Here is an example program for demonstrating classes and objects in java.
public class Main {
int val_1 = 22;
int val_2 = 32;
public static void main(String[] args) {
// creating multiple objects for the class main
Main obj_1 = new Main();
Main obj_2 = new Main();
System.out.println(obj_1.val_1);
System.out.println(obj_2.val_2);
}
}
22
32
Methods In Java
Functions underclasses are known as methods. They define the behavior of a class. Here is an example for implementing methods under classes…
import java.io.*;
public class Main {
static void method(String x) {
System.out.println("Hello From "+x+ "!");
public static void main(String[] args) {
method("Atufa");
}
Hello From Atufa!
Static and non Static methods in Java
In java, methods and classes are usually defined with static or public access modifiers.Defining methods or classes using static keywords makes them static, meaning they can be accessed outside the classes without creating an object for them.
Hope you understood Methods in java well, the next major concept of object-oriented programming is the Inheritance in Java.
Inheritance In Java
You might have some inheritance or resemblance of behaviours in you like your parents.Just like this, you can inherit properties of one class from another.The newly inherited class is a derived or child class and the former as base or Parent class.
Example Program for Inheritance implementation
import java.io.*;
class Main {
// Main attribute
protected String writer = "AL Sweigart";
// Main method
public void famousBook() {
System.out.println("Automate the boring stuff with python");
}
}
class Author extends Main {
private int books = 22; // Author attribute
public static void main(String[] args) {
// Create a Author object
Author Book = new Author();
// Call the famousBook() method (from the Main class) on the Book object
Book.famousBook();
// Display the value of the writer attribute (from the Main class) and the
// number of the books from the Author class
System.out.println(Book.writer + " " + Book.books);
}
}
The Main class is the super class, whereas the Author is the derived or child class of Main.The main method famous book can be accessed by any derived class as it is defined as public.
Here we just needed to add another attribute and method and the rest of the attributes and methods are added to the child class by adding the extends to class name Author and defining main() method.
The output for the above program will be
Automate the boring stuff with python
AL Sweigart 22
import java.io.*;
final class Main {
// Main attribute
protected String writer = "AL Sweigart";
// Main method
public void famousBook() {
System.out.println("Automate the boring stuff with python");
}
class Author extends Main { // this line will cause an error
private int books = 22;
public static void main(String[] args) {
Author Book = new Author();
Book.famousBook();
System.out.println(Book.writer + " " + Book.books);
}
prog.java:13: error: cannot inherit from final Main
Polymorphism In Java
Polymorphism is when you use a class method in different ways or in more than one way.There are two types of polymorphism Here is an example of a program to have a better understanding of Polymorphism..
import java.io.*;
class Shape {
public void area() {
System.out.println("Geometrical shapes");
}
class Circle extends Shape {
public void area() {
System.out.println("pi*radius*radius");
}
class Triangle extends Shape {
public void area() {
System.out.println("half*base*height");
}
class Main {
public static void main(String[] args) {
// create shape object
Shape Shape = new Shape();
Shape Circle = new Circle(); // Create a Circle object
Shape Triangle = new Triangle(); // Create a Triangle object
System.out.println("calling Shape method area");
Shape.area();
System.out.println();
System.out.println("calling Circle method area");
Circle.area();
System.out.println();
System.out.println("calling Triangle method area");
Triangle.area();
}
In the above code, the class Shape is the Base class whereas Circle and Triangle are the derived or sub classes. The same method area works differently in all the classes, it returns the area of different shapes.
The output for the above example
calling Shape method area
Geometrical shapes
calling Circle method area
pi*radius*radius
calling Triangle method area
half*base*height
Check out this Complete Online Java Course by FITA Academy. FITA provides a complete Java course including core java and advanced java J2EE, and SOA training, where you will be building real-time applications using Servlets, Hibernate Framework, and Spring with Aspect Oriented Programming (AOP) architecture, Struts through JDBC bundled with, placement support, and certification at an affordable price with an active placement cell,by expert software developers with 10+ years of experience in the field to make you an industry required certified java developer.
Getter and Setter method In Java
The attributes defined as private cannot be accessed outside the class, or the base class in case of inheritance.We will need public methods which can have access to the private attributes to retrieve or set the value of private attributes.
Hope you understood Polymorphism in java well, the next major concept of object-oriented programming is the Encapsulation in Java.
Encapsulation In Java
Encapsulation in simple terms is to make the methods and attributes private or protected and give a level of security to be accessed outside the class. The keywords public, private and protected are used to give a defined level of access outside the class. Example program for Encapsulation.
public class Encapsulation {
/* private variables can only be accessed by public methods of same class */
private String nickName;
private String habit;
private int age;
/* getter method for accessing private variable age */
public int get_age() {
return age;
/* getter method for accessing private variable nickname */
public String get_name() {
return nickName;
/* getter method for accessing private variable habit */
public String get_habit() {
return habit;
/* setter method for setting value for private variable age */
public void set_age(int newAge) {
age = newAge;
/* setter method for setting value for private variable age */
public void set_name(String newName) {
nickName = newName;
/* setter method for setting value for private variable age */
public void set_habit(int newHabit) {
habit = newHabit;
}
public class encaptest {
public static void main(String[] args) {
Encapsulation obj = new Encapsulation();
// setting values of the variables
obj.set_name("Atufa");
obj.set_habit("Writing content for geeks..");
obj.set_age(18);
// Displaying values of the variables
System.out.println("Writer's name: " + obj.get_name());
System.out.println("Writer's age: " + obj.get_age());
System.out.println("Writer's habit: " + obj.get_habit());
/*
* Direct access of habit is not possible due to encapsulation
* System.out.println("Writer's habit: " + obj.habit);
*/
}
Writer’s name: Atufa
Writer’s age: 18
Writer’s habit: Writing content for geeks..
Data Abstraction
Abstraction is where in the user is kept unaware of the code that is used for implementation, for instance, when you are buying a brand new phone, it is one thing for you, although there are many individual parts in this. Abstraction allows users to use the phone without knowing the complexity of the parts that form the phone.
In java you can make a class abstract by just prefixing the class name with the abstract keyword.
An abstract method can be declared in the super class with an abstract keyword, and defined under any subclass.
Here is an example program for abstraction
import java.io.*;
// An Abstract class
abstract class Shape {
// Abstract method (does not have a body)
public abstract void area();
// Regular method
public void property() {
System.out.println("A geometrical Shape");
}
}
// Subclass (inherit from Shape)
class Circle extends Shape {
public void area() {
// The body of area() is provided here
System.out.println("Area of circle: pi*radius*radius");
}
}
// Subclass (inherit from Shape)
class Triangle extends Shape {
public void area() {
// The body of area() is provided here
System.out.println("Area of triangle: half*base*height");
}
}
class Main {
public static void main(String[] args) {
Circle crc = new Circle(); // Create a Circle object
crc.property();
crc.area();
Triangle tri = new Triangle(); // Create a Triangle object
tri.property();
tri.area();
}
}
A geometrical Shape
Area of circle: pi*radius*radius
A geometrical Shape
Area of triangle: half*base*height
This was the end of object-oriented programming and its concepts in java with example programs. To get in-depth knowledge of core Java and advanced java, J2EE, SOA training along with its various applications and real-time projects using Servlets, Spring with Aspect Oriented Programming (AOP) architecture, Hibernate Framework,and Struts through JDBC you can enroll in Certified Java Course in Chennai or Certified Java Course in Bangalore by FITA or a virtual class for this course,at an affordable price, bundled with real-time projects, certification, support, and career guidance assistance and an active placement cell, to make you an industry required certified java developer.
FITA’s courses training is delivered by professional experts who have worked in the software development and testing industry for a minimum of 10+ years, and have experience of working with different software frameworks and software testing designs.