OOP and Polymorphism

One modern concept which many powerful languages utilize is Object Oriented Programming. It allows you to reuse previous code (saved as classes). This saves time because theoretically this previous class has already been debugged and should just work meaning you can take the class and use it without worrying about any issues. You can also do things like extend classes (inheritance) if you want to add functionality.

As discussed from the previous post, polymorphism allows you to have many shapes and is a key OOP feature. With regards to Java, it allows the JVM to determine which classes in the hierarchy is referenced by a superclass variable. A quick superclass example: mammal would be the superclass while human would be a subclass (more specific). So in polymorphism if you wanted to reference humans you could do so by declaring the refence as the more broad class since they both are technically under that same class.

An example of this is declaring a computer which can reference either a computer OR and laptop because the laptop is also a computer. This simplifies coding when dealing with methods which have class parameters. There are many more OOP concepts in Java such as method overriding/ overloading. For the sake of brevity I will not be talking about these concepts, however please let me know if you are interested in them and I’ll try to include them in a future post.


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 )

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