Object oriented principles in Java Explained in Short
As you probably know, Java is an Object-oriented language. There are some fundamental object-oriented principles that are supported by Java. In this post, I will give you an overview of the object-oriented principles supported by Java. I will also explain how Java supports them. An understanding of these OOPs principles will give you a strong foundation while writing code.
Abstraction
Abstraction simply means hiding the internal details from the outside world. In Java, classes & methods are used to achieve abstraction. So a class hides its internal working from the outside world & only provides public methods for the functionality which the outside world needs to access.
Encapsulation
Wrapping together data & code that operates on the data together as a single unit is referred to as encapsulation. Again, a well-implemented java class can be an example of encapsulation. By well implemented, I mean that the correct access specifiers are used i.e. fields should have private access & the methods that operate on the data must have public access.
Polymorphism
Polymorphism is when one task is performed in different ways. Java supports two types of polymorphism, compile-time, and runtime. Compile-time polymorphism is achieved via method overloading. Runtime polymorphism is achieved via method overriding. Method overloading is known as Compile-time polymorphism since the version of an overloaded method to be invoked is determined at compile time. Method overriding is known as runtime polymorphism since the version of an overridden method to be invoked is determined at runtime.
Inheritance
Inheritance allows programmers to create new classes that share some of the attributes of existing classes. This lets us build on previous work without reinventing the wheel. Inheritance allows us to reuse code. The inherited object is called a superclass and the object that inherits the superclass is called a subclass.
Further Reading
Conclusion
So, in this article, we understood about the object-oriented principles in Java.
Comments
Post a Comment