Describe OOP in java?

Answer Posted / javamasque

OOP means object oriented programming, where objects play active role to fulfill request of the user. As per the OOP concept, objects have the below 4 features

Encapsulation: Objects have the ability to hide its state (properties) and behavior (methods) with the use of access modifier.

Inheritance: It is a technique to define child class / interface by extending parent class / interface. As a class implements interface is also comes under inheritance. It helps to override legacy method or abstract method with new functionality. It also facilitates the child class to access protected members of its parent.

Polymorphism: It means to overload or override the behaviors (method) of a class. There are two types of polymorphism
• Static / Compile time: To overload a method with in same class in called static or compile time polymorphism. As we call overloaded method, compiler knows which overloaded method will be called.
Example
1.Class A has two method suppose sum(int n1, int n2), sum(long n1, long n2)
2.You created instance of class A inside main method and statically called either as below
A a = new A();
a.sum1(2, 3) or a.sum(2l, 3l)
3.Hence compiler knew which overloaded method called.
• Dynamic / Run time: To override a method using inheritance is called dynamic or runtime polymorphism. Override method execution is decided at run time hence JVM can decide which override method should be called.
Example
1.Class A has a method sum() and do addition of two integers.
2.Class B is child class of A and override sum() method and do addition of two float numbers.
3.There is an another class C which has main method and inside main method we have below code snippet
A a = new B();
a.sum();
4.Now compiler knows it is method of class A will be called but at runtime the override method of class B will be called, as instance of class A will be bound with the reference of class B at runtime dynamically.
5.Hence Overriding is called dynamic or runtime polymorphism.

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are three types of loops in java?

589


What are the advantages and disadvantages of reference counting in garbage collection?

576


What is final variable?

497


What are different types of inner classes ?

562


What is getclass () getname () in java?

681






Does substring create a new object?

530


How does indexof work?

507


What is the final class?

550


Can we define private and protected modifiers for variables in interfaces?

583


Does constructor creates the object ?

586


What are the differences between string and stringbuffer?

591


Describe the process as to how substring() methodology mechanisms in java.

562


How to retrieve data from database in java using arraylist?

530


what is thread? What are the high-level thread states? Or what are the states associated in the thread? : Java thread

500


Can we serialize arraylist in java?

543