0 votes
377 views
in Java by (3.7k points)

Object Oriented Programming (OOPs) Concept in Java

1 Answer

0 votes
by (3.7k points)

Here, we will learn about the basics of OOPs (Object-Oriented Programming). Object-Oriented Programming is a paradigm that provides many concepts, such as abstraction, inheritance, data binding, polymorphism, etc. 

Java, C#, PHP, Python, C++, etc. are using object-oriented languages.

Object-oriented programming: Object-Oriented Programming or OOPs refers to languages that uses objects in programming. The main aim of object-oriented programming is to implement real-world entities, for example, object, classes, abstraction, inheritance, polymorphism, etc. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

OOPs Concepts:-

  1. Polymorphism
  2. Inheritance
  3. Encapsulation
  4. Abstraction
  5. Class
  6. Object
  7. Method

OOPs  Concept

Polymorphism:- Polymorphism means, we can perform a single action in different ways. Polymorphism is derived from two Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms.

There are two types of polymorphism: Compile-time polymorphism and Runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding. If you overload a static method in Java, it is the example of compile time polymorphism.

Compile-time polymorphism (Overloading):- Overloading allows different methods to have the same name, but different signatures where the signature can differ by the number of input parameters or type of input parameters or both. Overloading is related to compile-time (or static) polymorphism.

public class Sum {
 
    public int sum(int x, int y) {
        return (x + y);
    }
 
    public int sum(int x, int y, int z) {
        return (x + y + z);
    }
 
    public double sum(double x, double y) {
        return (x + y);
    }
 
    public static void main(String args[]) {
        Sum s = new Sum();
        System.out.println("Sum takes two int parameters:- "+s.sum(10, 20));
        System.out.println("Sum takes three int parameters:- "+s.sum(10, 20, 30));
        System.out.println("Sum takes two double parameters:- "+s.sum(12.5, 16.5));
    }
}

Output:-
Sum takes two int parameters:- 30
Sum takes three int parameters:- 60
Sum takes two double parameters:- 28.0

Runtime polymorphism(Overriding):- In java, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

class Parent {
    void show() {
        System.out.println("Parent's show()");
    }
}
 
// Inherited class
class Child extends Parent {
    @Override
    void show() {
        System.out.println("Child's show()");
    }
}
 
// Driver class
class Test {
    public static void main(String[] args) {
        Parent obj1 = new Parent();
        obj1.show();
 
        Parent obj2 = new Child();
        obj2.show();
    }
}

Output:-
Parent's show()
Child's show()

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...