In my previous blog , I discussed about what Java basically is . Now , let's begin with the necessary concepts of java which made java a purely Object oriented language.
Java is known as Highly object oriented language and it covers almost all OOPS concepts by itself.
There are mainly 4 OOPS concepts which are to be used by us in future a lot. These are -
1. Inheritance
2. Polymorphism
3. Abstraction
4. Encapsulation
Lets first discuss about Inheritance. What Inheritance really is and how it can make our work easier in java.
What is Inheritance?
As the name suggests , inheritance is used to inherit the properties of something. What is something there? - a class . So , when one class inherits the properties of another class , it is said that inheritance is occurred. The class which inherits the properties is known as subclass and the class which is inherited known as super class.
As shows in this picture , we have 3 types of inheritance in java -
1.Simple Inheritance - Where only one class inherits the property of any other class like class B inheriting all the properties of class B.
2. MultiLevel Inheritance - Where classes are inherited in multiple levels like class C inherits class B and class B inherits class A . So , basically they are creating chains of inheritance.
3. Hierarchical Inheritance - where two classes let's say class B and class C has only one parent class A.
OOPS has one more type of inheritance called multiple inheritance , but it is not there in java.
What is Polymorphism?
In this class , we are calling a single class Animal but gets different output.
As the name suggests Polymorphism denotes to "many forms" . In java , this concept is very useful in terms of the fields where we have same categories but different outputs. Like in this example too , we have class Animal as general class but after it extended by class Cat and Dog , it manipulated the method makeNoise() according to their convenience or speciality.
So , inheritance gave birth to this concept which we known as polymorphism.
What is abstraction?
Abstraction is used when we want to hide certain details and only want to show some details. Basically , it is used to hide the functionality and only give results. For implementing abstraction in java , we have abstract classes .
In Abstract classes , we can't create objects . They are restricted classes for this and methods don't contain any body . They only have declaration . The body of these methods are provided in their sub classes which extends them ( concept of inheritance).
As shown in this picture , this is what abstraction do to our code . We code complex methods and functionalities but we only show their abstract classes which has only declaration of those methods.
What is Encapsulation?
In my previous blog , I already told about what encapsulation is . Encapsulation basically wraps up our objects and methods in a capsule . This capsule is what we call a class. In java , everything is inside a class and what is outside the class is of no importance. So , java use all the things in an encapsulation format.
This picture perfectly describes what encapsulation basically is. Here by data members , we want to say variables and objects that we use in methods.
Now , all you are wondering that Java is purely an object oriented language , hence proved. but not yet.
Yes , previously I also said Java a purely Object oriented language but this is not true . Java also has some concepts absent for being an object oriented one.
Let's see , who knows those points which are disproving the java as object oriented and I will answer this in my next blog. Till then rest.
Comments
Post a Comment