Java as Object Oriented Programming Language
The most commonly asked questions on Java in interviews are the concepts of Object Oriented Programming.
The Object Oriented Programming Languages follow the principle of programming using the objects which represents real life objects of life such as Car, House, and Accounts etc.
The features of OOPL which forms the basic building block or pillar of OOPS are Polymorphism, Inheritance and Encapsulation. By short called as P-I-E of OOPS.
Polymorphism: The one line definition – “Same name different functionality”
Polymorphism is the ability of a single variable of a given type to reference objects of different types and automatically calls the method that is specific to the type of the object variable references to.
For example
If you have a class “Account”, “SavingsAccount” and “FixedDepositAccount”
class Account
{
public int calcInterest()
{
// calculates generic interest amount
}}
class SavingsAccount extends Account
{
public int calcInterest()
{
// calculates savings account interest
}
}
class FixedDepositAccount extends Account
{
// No implementation of calcInterest method
// same as generic interest
}
Now ,
Account savAcc = new SavingsAccount();
savAcc.calcInterest(); // This will call the method on SavingsAccount class
Account fixAcc = new FixedDepositAccount();
fixAcc.calcInterest() ; // This method will call the method on Account class as there is
// implementation for
The above example illustrated the power of polymorphism where, we can easily modify method implementations without actually breaking the code elsewhere.
So, whenever we call a method on an object, even though we don’t know that type it is, right thing happens at the runtime. This is called Polymorphism.
Inheritance : One line definition “inheriting the features of superclass to the subclass”
Inheritance is the inclusion of the behaviour of the bas class in the derived class so that the methods/members can be accessible in the derived classes. It helps in code reuse, reduced development time and increased maintainability of code.
There are two types of inheritance,
Implementation inheritance – where the subclass extends the functionality of the superclass. Here the derived class has an option to implement all or some of the features of the superclass based on its requirement.
Here you can only extend only one class.
Interface inheritance - This is also known as sub typing. Interfaces provide a mechanism for specifying a relationship between otherwise unrelated classes, typically by specifying a set of common methods each implementing class must contain. Interface inheritance promotes the design concept of program to interfaces not to implementations. This also reduces the coupling or implementation dependencies between systems. In Java, you can implement any number of interfaces. This is more flexible than implementation inheritance because it won’t lock you into specific implementations which make subclasses difficult to maintain. So care should be taken not to break the implementing classes by modifying the interfaces.
Encapsulation – Data Hiding or Hiding the implementation details
Encapsulation refers to keeping all related members (methods and variables) together in an object. Encapsulation results in modular code making them safe and preventing the misuse of the members.
Marking the instance variables of an Object as private and provide public methods for accessing the variables make a class (Object) tightly encapsulated. By this, we can restrict the access of instance variables and we can change the business logic for the variables without actually breaking the code.
For ex:
public class Bowling
{
private int noOfDeliveries;
public void setNoOfDeliveries(int balls)
{
// here we can write a logic to restrict the user to only set the
// value of instance variable between 0 and 6
// preventing the instance variable being corrupted with invalid values
}
}
No comments:
Post a Comment