Polymorphism and abstraction (OOP 3)

In the previous blog, I stated 2 of 4 properties of OOP in Java. In this article, I would like to go through 2 remaining properties including Polymorphism and abstraction.

Polymorphism

Polymorphism means we can perform the one task in many different ways depending on the situation and context that it get involved. For example, when a person is studying at school, he or she might have to listen to the lecture, doing an assignment, discussing, etc. With the same person, but if he or she is staying at home, he or she might have to cook, clean up, gardening, etc.

With Java, in the case we want to use polymorphism technique, the code would follow 2 conditions:

  • The subclass has to inherit from another super class
  • The polymorphism method or function has to override in the subclass (Override the method that defined in the superclass).

The following classes will show how to do it.

class Shape{
    public void printOut(){
        System.out.println("showing the shape!!!");
    }
}
class Triangle extends Shape{
    @Override
    public void printOut(){
        System.out.println("this is printOut() of Triangle!!!");
    }
}

Abstraction and abstract class

According to the definition, abstraction is a concept which is not pointed out any specified object. In other words, abstraction in coding is a process that hides what is happening, and only shows the main features of the Object.

For example, once the user types and sends a message, they do not know what is happening with what they sent. The things that user could be known is whether their message is sent and received or not.

In the above example, the user only knows three main features of the sending message application:

  • how to type the message
  • press send button to send the message
  • if the message was successfully sent or fail.

All of the things of sending process are being hide. That is abstraction.

Abstract class in Java

One class is abstract if it is declared as abstract, all of its abstract methods need to be defined. Abstract class has no constructor.

abstract class Shape{
   abstract void drawingShape();
}

class Triangle extends Shape{
    private int edge;
    public Triangle(){
        edge = 3;
    }
    @Override
    public void drawingShape(){
        System.out.println("drawing Triangle");
    }
}

class Rectangular extends Shape{
    private int edge;
    public Rectangular(){
        edge = 4;
    }
    @Override
    public void drawingShape(){
        System.out.println("drawing Rectangular");
    }

Conclusion

So far, we went through four characteristics of OOP in programming. In the real world, we should create the classes in the separate files which will be helpful for reviewing and maintaining process later.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: