Object Oriented Programming Concepts
- Object-Oriented Programming system is a programming paradigm in which software design involve around data, or objects, rather than functions and logic.
- In OOP objects are data fields that have unique attributes and properties.
- OOP focuses on the objects that developers want to manipulate rather than the logic required to manipulate them.
- Object-Oriented Programming system is well suited with large and complex software programs.
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
- An interface contains only abstract methods i.e. methods without definition or implementation.
- We can't instantiate an interface.
- Variables in Interface are public, static and final. Interface also don't contain constructor.
- Members of the interface can never be private.
- An interface can't be extended by class, it can only be implemented by class.
- Abstract methods of the interface must be overridden in the class that implements the interface.
- Multiple inheritance can be used with Interfaces. In simple words a class can implement multiple interfaces.
In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below.
1. Public: The classes, methods, or variables which are defined as public, can be accessed by any class or method.
2. Protected: can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.
3. Default: is accessible within the package only. By default, all the classes, methods, and variables are of default scope.
4. Private: The class, methods, or variables defined as private can be accessed within the class only.
Inheritance is a one of the four pillars of OOP by which one object acquires all the properties and behavior of another object of another class. It is used for Code Reusability and Method Overriding. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
There are five types of inheritance in Java.
- Single-level inheritance
- Multi-level inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Multiple inheritance is not supported in Java through class.
There are following benefits of Inheritance.
• Code reusability
• Less development and maintenance costs
• Reduces code redundancy and supports code extensibility
• Save time and effort to write codes
Why Java doesn't support multiple inheritance? OR Explain Diamond Problem with an Example.
In multiple inheritance one class inherits the properties of multiple classes. In other words, in multiple inheritance we can have one child class and n number of parent classes. Java does not support multiple inheritance (with classes) because of diamond problem.
Lets consider following example.
Suppose we have class Parent1 which contains test() method.
Also we have another class Parent2 which also contains test method().
Now, We have Child class which extends both Parent1 and Parent2.
[Note: above statement is incorrect and not possible in JAVA, In order to explain Diamond problem I have created this example.]
Now, as per the basic rule of inheritance, a copy of both test() methods should be created in the Child class which leaves the Child class with two methods with same signature, Then, if we call the test() method using the object of the Child class compiler will complain as it will face an ambiguous situation not knowing which method to call. This ambiguous situation is known as diamond problem in Java.
Explain Polymorphism in JAVA?
Polymorphism in JAVA allows us to perform the same action in many different ways. It refers to the ability of a class to provide different implementations of a method.
There are two types of polymorphism -
• Compile time polymorphism
• Run time polymorphism
For Example.
Method Overloading and Method Overriding
What is compile time polymorphism and Run time Polymorphism? OR
What is static polymorphism and Dynamic polymorphism? OR
What is Early binding and Late binding?
The polymorphism that takes place during compile time is called compile time polymorphism. It is also called static polymorphism or early binding.
In this type of polymorphism, a class contain multiple methods having same name but different signatures.
Example : Method Overloading
Method Overloading Vs Method Overriding.
Method Overloading:
1. Method overloading is the example of compile time polymorphism.
2. Method overloading is performed within class.
3. In method overloading, methods must have the same name and different signatures. Method can't be overloaded just by changing return type of the method.
4. Argument list should be different while doing method overloading.
5. Return type can be same or different in Method Overloading.
6. Private and final methods can be overloaded.
Method Overriding:
1. Method overriding is the example of run time polymorphism.
2. Method overriding occurs in two classes that have IS-A (inheritance) relationship.
3. In method overriding, methods must have the same name and same signature.
4. Argument list should be same in method overriding.
5. Return type must be same or covariant in method overriding.
6. Private and final methods can’t be overridden.
=======================================================================
Comments
Post a Comment