Object-oriented programming allows for simplified programming. Its benefits include reusability, refactoring, extensibility, maintainability, and efficiency. These are some of the features that greatly enhance simplification.

Polymorphism

Polymorphism is the ability of an object to take many forms. The most common use of polymorphism in OOP is when a parent class reference is used to refer to a child class object.

Any Java object that can pass more than one IS-A test is considered polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for its own type and for the Object class.

It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared, the type of a reference variable cannot be changed.

The reference variable can be reassigned to other objects as long as final is not declared. The type of the reference variable would determine the methods you can call on the object.

A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.

Example:

Let’s see an example.

public interface Vegetarian{}
public class animal{}
public class Deer extends Animal implements Vegetarian{}

Now the Deer class is considered polymorphic as it has multiple inheritance. The following is true for the example above:

• A deer IS-AN animal
• A deer IS-A vegetarian
• A deer IS-A deer
• An ES-A Deer object

When we apply the facts of the reference variable to a Deer object reference, the following statements are legal:

Stag d = new Stag();
Animal a = d;
vegetarianov=d;
Object o = d;

Abstraction

Abstraction refers to the ability to make an abstract class in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are accessed in the same way. You just can’t instantiate the abstract class.

If a class is abstract and cannot be instantiated, the class doesn’t have much use unless it’s a subclass. This is usually how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.

Abstract class:

Use the abstract keyword to declare an abstract class. The keyword appears in the class declaration somewhere before the class keyword.

/* File name: Employee.java */
abstract public class employee
{
private string name;
private chain address;
private int number;
Public Employee (string name, string address, int number)
{
System.out.println(“Building an employee”);
this.name = name;
this address = address;
this.number = number;
}
public double calculation pay()
{
System.out.println(“Inside Employee computePay”);
return 0.0;
}
public void mailCheck()
{
System.out.println(“Mailing a check to ” + this.name
+ ” ” + this.address);
}
Public String to String()
{
return name + ” ” + address + ” ” + number;
}
public string getName()
{
return name;
}
public string getAddress()
{
Return address;
}
public void setAddress(String newAddress)
{
address = new address;
}
public int getNumber()
{
return number;
}
}

Note that nothing is different in this class of employee. The class is now abstract, but it still has three fields, seven methods, and a constructor.

Abstract methods:

If you want a class to contain a particular method, but you want the actual implementation of that method to be determined by the child classes, you can declare the method in the parent class as abstract.

The abstract keyword is also used to declare a method abstract. An abstract method consists of a method signature, but not a method body.

The abstract method would have no definition, and its signature is followed by a semicolon, not braces, as follows:

abstract public class employee
{
private string name;
private chain address;
private int number;

public abstract double countPay();

//Remainder of the class definition
}

Declaring a method as abstract has two results:

• The class must also be declared abstract. If a class contains an abstract method, the class must also be abstract.
• Any child class must override the abstract method or be declared abstract.

A child class that inherits an abstract method must override it. If they don’t, they must be abstract and any of their children must override it.
Eventually, a descendant class has to implement the abstract method; otherwise you would have a hierarchy of abstract classes that cannot be instantiated.

Leave a Reply

Your email address will not be published. Required fields are marked *