OOPs Concept in Java
OOPS stands for Object-Oriented Programming. It is a fundamental concept in Java.
There are 4 main concepts Abstraction, Encapsulation, Inheritance and Polymorphism.
It provides a clear and logical structure, making the code easier to understand, maintain, debug and easy to manage.
It is a programming approach that organizes code into classes and objects.
A class is a blueprint that defines properties and behaviors, while an object is a real-world entity.
Java Classes:
A class is a blueprint for objects to create real-world entities that shares common characteristics and properties.
Class is not a real-world entity. It is just a template or blueprint, or a prototype from which objects are created.
It contain Data members, methods, a Constructor, Nested classes, and interfaces
Components of Java Classes:
Modifiers: A class can be public or has default access.
Class keyword: Class keyword is used to create a class.
Class name: The name should begin with an initial letter.
Interfaces: A class can implement more than one interface.
Body: The class body is surrounded by braces{ }.
Fields: Fields are variables defined within a class that hold the data or state of an object.
Constructors: It is a special method in a class that is automatically called when an object is created. And it's name is same as class name.
Java Objects:
Objects are the instances of a class that are created to use the attributes and methods of a class. A typical Java program creates many objects.
For example the animal type Dog is a class, while a particular dog named XYZ is an object of the Dog class.
Properties of an object:
State: It is represented by attributes of an object. It also reflects the properties of an object.
Behavior: It is represented by the methods of an object. It also reflects the response of an object with other objects.
Identity: It gives a unique name to an object and enables one object to interact with other objects.
Abstraction in Java? Abstraction is a process of hiding the implementation details and showing only functionality to the user. For example filling the contact form. It will show only the particular details of the contact form not internal process details. It’s called Abstraction in Java.
Points To Remember :
It can have abstract and non-abstract methods.
It cannot be instantiated.
It is used for abstraction.
It provides 100% abstraction because it can have concrete methods.
An abstract class must be declared with an abstract keyword.
An Abstract methods can either be hidden or overridden, but non abstract methods can only be hidden.
What is an Abstract Class?
An abstract class is a class that is declared with abstract keyword.
Abstract classes cannot be instantiated, but they can be subclassed.
It can contain abstract methods (methods without a body) as well as concrete methods (methods with a body).
When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.
However, if it does not, then the subclass must also be declared with abstract keyword.
Abstract Class Example:
abstract class Shape {
// Abstract method does not have a body.
public abstract double area();
// Concrete method has a body.
public void display() {
System.out.println("This is a shape...");
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Implementing abstract method
public double area() {
return length * width;
}
}
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
// Implementing abstract method
public double area() {
return Math.PI * radius * radius;
}
}
// Derived Class
public class Main {
public static void main(String[] args) {
Shape rect = new Rectangle(5, 4);
Shape circle = new Circle(3);
rect.display();
System.out.println("Area of rectangle: " + rect.area());
circle.display();
System.out.println("Area of circle: " + circle.area());
}
}
Output:
This is a shape.
Area of rectangle: 20.0
This is a shape.
Area of circle: 28.274...
What is Abstract method?
A method which is declared as abstract and does not have implementation is known as abstract method. Methods are declared without body within an abstract class is also called abstract method.
Abstract method Example:
abstract class Animal {
// Abstract method does not have a body.
public abstract void makeSound();
// Concrete method
public void sleep() {
System.out.println("Zzz");
}
}
class Dog extends Animal {
// Implementing abstract method
public void makeSound() {
System.out.println("Woof");
}
}
class Cat extends Animal {
// Implementing abstract method
public void makeSound() {
System.out.println("Meow");
}
}
// Derived Class
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
dog.makeSound();
cat.makeSound();
dog.sleep();
cat.sleep();
}
}
Output:
Woof
Meow
Zzz
Zzz
Encapsulation in Java?
Encapsulation in Java is a mechanism of wrapping the data (variables) and code (methods) together into a single unit.
It is a mechanism that binds together data and the code and manipulates it.
Advantages of Encapsulation:
Data Hiding
Code Reusability
Easier Testing
Enhanced Security
Flexibility
Modularity
Maintainability
Reduced Coupling etc..
Implementation of Encapsulation:
public class Person {
private String name;
private int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age >= 0) {
this.age = age;
} else {
System.out.println("Age cannot be negative.");
}
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
//Driver Class
public class Main {
public static void main(String[] args) {
Person person = new Person("Suresh", 30);
person.display();
person.setName("Babu");
person.setAge(25);
person.display();
}
}
OutPut:
Name: Suresh
Age: 30
Name: Babu
Age: 25
Polymorphism in Java?
Polymorphism means "Many forms" The Polymorphism is one of the core concepts in OOPs languages and describes the concept where you can use different classes with the same interface.
The Polymorphism has two types Runtime polymorphism (dynamic binding) and Compile time polymorphism (static binding).
We can perform polymorphism in java by method overloading and method overriding. Method overriding is an example of dynamic polymorphism, while method overloading is an example of static polymorphism.
Types of Polymorphism:
1.Method Overriding
2.Method Overloading
Method Overloading:
public class OverloadingDemo {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
//Driver Class
public static void main(String[] args) {
OverloadingDeno obj = new OverloadingDemo();
System.out.println("Sum of integers: " + obj.add(5, 10));
System.out.println("Sum of doubles: " + obj.add(5.5, 10.5));
}
}
Method Overriding:
class Animal {
public void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Dog barks");
}
}
public class OverridingDemo {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.sound();
}
}
Output: "Dog barks"
*Disclaimer: We have published ( Government Jobs ) the above images and information for reference purpose only, for any changes on the content we refer to visit the Official Website to get the latest info.
NOTE: We are not a Recruiters, we just share details of the job vacancies available presently.
We didn't collect or charge any money for recruitment from both Government Jobs seekers as well as not from Job Providers. Please be aware of fraudulent Calls, SMS or Emails.