Object Oriented Programming (OOPs) concept in Java - ayushmathur94/DirectQuesAns_Prep GitHub Wiki
Q1.) What are concepts of Object Oriented Programming (OOPs) ?
1. Absraction
2. Encapsulation
3. Polymorphism
4. Inheritance
5. Association
6. Composition
7. Aggregation
Abstraction is the concept of hiding of internal implementation while showing only basic interface/functionality
to the user.
Real Time Example :
1.) ATM machine : we use atm machine for cash withdrawal, mini statement, money transfer etc. But we don't know what things are happening inside ATM machine when we insert ATM card. 2.) SMS from mobile : when we need to send SMS from our mobile , we simply type the text and send the message. But we don't know the internal processing of the message delivery.
Ways to Achieve Abstraction : 1.) Using Abstract Class 2.) Using Interface
Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited
from another class). Abstract class can have abstract as well as non-abstract methods (concrete methods) .
There can be no object of an abstract class. That is, an abstract class can not be directly instantiated with
the new operator.
Abstract method: abstract method does not have a body (An abstract method is a method that is declared
without implementation). The body is provided by the subclass (inherited from). A method defined abstract
must always be redefined in the subclass, thus making overriding compulsory.
Example of abstraction via abstract class
Another way to achieve abstraction in Java, is with interfaces.
Using interfaces, you can achieve (complete) abstraction. Since all the methods of the interface are
abstract and the user doesn’t know how a method is written except the method signature/prototype.
Q3.) What is an interface in Java ?
> The methods declared in interface are by default abstract (only method signature, no body). To access the
interface methods, the interface must be "implemented" (kinda like inherited) by another class with the
implements keyword (instead of extends). The body of the interface method is provided by the "implement" class.
> The class that implements interface must implement/have all the methods of that interface.
Example of abstraction via Inferface
> The variables declared in an interface are public, static & final by default.
> Java programming language does not allow you to extend more than one class, However you can implement more
than one interfaces in your class.
Example of Abstraction via More Than One Interfaces
Q4.) What is difference between abstract class and interface ?
> Encapsulation in Java is a process of wrapping data and code (function/method) together under a single unit.
> It’s the mechanism to bind together the data and the function that work on it.
> Encapsulation is the process of hiding Objects’ properties from the outer world and provides methods
to access them.
> Encapsulation hides the objects’ data, so it’s also called data-hiding.
> Encapsulation is one of the ways to achieve abstraction in Object-oriented programming.
Q6.) What are the ways to achieve encapsulation in JAVA ?
We can achieve encapsulation in Java by declaring all class fields as private and then provide public getter and
setter methods for them.
public class Data {
private int id;
private String value;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Q7.) What are advantages of Encapsulation ?
> code is loosely coupled (We can change the class variable name (not instance variable) without affecting
the client programs, by depreciating old methods and providing new getter and setters with new class variable
names)
> code is more secure
> code reusability
> helps provide access control for Object properties
> Inheritance in java is one of the core concepts of Object Oriented Programming.
> Its a mechanism by which one class is allowed to inherit all the properties/features and behavior (ie. fields
and methods) of another class (parent class).
> Java Inheritance is used when we have is-a relationship between objects.
> Inheritance in Java is implemented using extends keyword.
Terminologies related to inheritance :
Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).
Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class,
or child class). The subclass can add its own fields and methods in addition to superclass fields and methods.
class derived-class extends base-class{
// methods and fields
}
> by using the object of the subclass we can also access the members of a superclass.
Example of Inheritance in Java
1.) Single Inheritance 2.) MultiLevel Inheritance 3.) Hierarchical Inheritance 4.) Multiple Inheritance
5.) Hybrid Inheritance
1.) Single Inheritance :
In Single Inheritance , subclasses inherit features of (only) one superclass.
2.) MultiLevel Inheritance :
In MultiLevel Inheritance , a derived class will be inheriting a parent class and as well as the derived class
also act as the parent class to other class.
3.) Hierarchical Inheritance:
In Hierarchical Inheritance, one parent class will be inherited by many sub classes.
ClassA will be inherited by ClassB, ClassC and ClassD
4.) Multiple Inheritance :
> In multiple Inheritance , one class can have more than one parent class and inherit features from
all parent classes.
> Multiple Inheritance is nothing but one class extending more than one class.
> Multiple Inheritance is not directly supported by Java. But you can achieve Multiple Inheritance in Java
using Interfaces.
5.) Hybrid Inheritance :
> Hybrid Inheritance is the combination of Single and Multiple Inheritance.
> ClassA will be acting as Parent Class for ClassB and ClassC , & ClassB and Class C will be acting as Parent
for ClassD.
> Hybrid Inheritance is not directly supported by Java.But you can achieve Hybrid inheritance in Java
using Interfaces.
Polymorphism means many forms/behaviors. Defining a method in multiple classes with the same name with different implementations for exhibiting different behaviors of the object is called polymorphism. We can perform polymorphism in Java by using > Method Overriding > Method Overloading
Q.) What are the types of Polymorphism ?
There are two types of polymorphism in Java: ***compile-time polymorphism*** and ***runtime polymorphism***
Compile Time Polymorphism: Whenever an object is bound with their functionality at the compile-time, this is known as the compile-time
polymorphism. At compile-time, java knows which method to call by checking the method signatures. So this is called compile-time polymorphism
or static or early binding. Compile-time polymorphism is achieved through method overloading. Method Overloading says you can have more than
one function with the same name in one class having a different prototype. Function overloading is one of the ways to achieve polymorphism but
it depends on technology that which type of polymorphism we adopt. In java, we achieve function overloading at compile-Time.
Run-Time Polymorphism: Whenever an object is bound with the functionality at run time, this is known as runtime polymorphism. The runtime
polymorphism can be achieved by method overriding. Java virtual machine determines the proper method to call at the runtime, not at the compile
time. It is also called dynamic or late binding. Method overriding says child class has the same method as declared in the parent class. It
means if child class provides the specific implementation of the method that has been provided by one of its parent class then it is known as
method overriding.
difference bw runtime and compiletime polymorphism
Q.) How Inheritance and polymorphism are connected ?
Polymorphism occurs when we have many classes that are related to each other by inheritance.
Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows
us to perform a single action in different ways.
For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs,
Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.)
Best Described AssociationAggComp
Method Overriding is redefining a super class method in a sub class.
Rules for Method Overriding
- The method signature i.e. method name, parameter list and return type have to match exactly.
- The overridden method can widen the accessibility but not narrow it, i.e. if it is private in the base class, the child class can make it public but not vice versa.
class Doctor{ public void treatPatient(){ // treatPatient method } class Surgeon extends Doctor{ public void treatPatient(){ // treatPatient method } } Class run{ public static void main (String args[]){ Doctor doctorObj = new Doctor() // treatPatient method in class Doctor will be executed doctorObj.treatPatient(); Surgeon surgeonObj = new Surgeon(); // treatPatient method in class Surgeon will be executed surgeonObj.treatPatient(); } }
- Method overloading is in the same class, where more than one method have the same name but different signatures. While Method overriding is when one of the methods in the super class is redefined in the sub-class. In this case, the signature of the method remains the same.
- Overriding is what has dynamic binding in Java. Overloading has static binding, and which function is called is determined at compile time, not at runtime.
Dynamic Polymorphism in OOPs is the mechanism by which multiple methods can be defined with same name and signature in the superclass and subclass. The call to an overridden method are resolved at run time.
Dynamic Polymorphism Example: A reference variable of the super class can refer to a sub class object
Doctor obj = new Surgeon(); Consider the statement obj.treatPatient();
Here the reference variable “obj” is of the parent class, but the object it is pointing to is of the child class
obj.treatPatient() will execute treatPatient() method of the sub-class – Surgeon
If a base class reference is used to call a method, the method to be invoked is decided by the JVM, depending on the object the reference is pointing to
For example, even though obj is a reference to Doctor, it calls the method of Surgeon, as it points to a Surgeon object
This is decided during run-time and hence termed dynamic or run-time polymorphism
Difference between Static & Dynamic Polymorphism Static Polymorphism in Java is a type of polymorphism that collects the information for calling a method at compilation time, whereas Dynamic Polymorphism is a type of polymorphism that collects the information for calling a method at runtime.
Static Polymorphism |
Dynamic Polymorphism |
---|---|
It relates to method overloading. | It relates to method overriding. |
Errors, if any, are resolved at compile time. Since the code is not executed during compilation, hence the name static. | In case a reference variable is calling an overridden method, the method to be invoked is determined by the object, your reference variable is pointing to. This is can be only determined at runtime when code in under execution, hence the name dynamic. |
Ex: void sum (int a , int b); void sum (float a, double b); int sum (int a, int b); //compiler gives error. |
Ex: //reference of parent pointing to child object Doctor obj = new Surgeon(); // method of child called obj.treatPatient(); |