Sie sind auf Seite 1von 2

The 5 Pillars of Java Java is a pure object oriented language.

Pure object oriented means having all the features of an object oriented language plus an additional feature of dealing only with objects and classes. In other words, primitive data types should not be allowed. However, there is an exception to this fact that primitive data types cannot be ignored as without them the whole world will be in trouble. In future, it might be possible to completely disallow the usage of primitive data types. Until then support has to be provided. However, java does allow usage of classes as data types using wrapper classes. Wrapper classes allow primitive data types to be converted into non-primitive data types. 1st Pillar - Encapsulation Encapsulation means to capsulate data and the function into a single entity. Encapsulation is the mechanism that binds the code and data together and restricts access by the outside world. A very good example of encapsulation is a car. All the internal working of the car, its transmission, parts, etc. is hidden from the outside world. Only the important details are mentioned like the size of the tire, number of gears, etc. You can think of encapsulation as a way to wrap the data inside a container to restrict access from outside world. However, access to the data is provided through a well-defined interface. In java the basis of encapsulation is class. You will specify the code and variables in the form of class variables and methods. The behavior and interface of a class are fully defined by the methods of the class. Access to these methods and variables is specified using the keywords 'public', 'protected' or 'private'. Public access means anyone can access the data. Private access means only the methods defined within the class can access the data. Protected access means methods within the class and methods within the child classes can access the data. Child classes are defined using inheritance. Thus, encapsulation is hiding of essential details from programmer's point of view. 2nd Pillar - Polymorphism The word polymorphism has been derived from a Greek word 'poly' which means many and 'morph' which means forms. So, polymorphism refers to many forms. In java, polymorphism means one method with many different forms. As an example, consider a program that adds 2 numbers, which can be a whole number or a real number but the logic for their addition is same. Using polymorphism one can introduce such a function that can sum both real and whole numbers depending on what the user passes. In java language, polymorphism is also expressed by the phrase "one interface, multiple methods". This allows designing of a single interface for a group of related activities. This interface can then be implemented by other general classes. It is the job of the compiler to select the appropriate method depending on the values being passed at compile time or runtime. Polymorphism comes in 2 forms: overloading and overriding. Overloading refers to one method with same name but a different return type or number of arguments. A method sum that adds 2 numbers and another method sum that adds 3 numbers is an example of overloading. Overriding refers to a method with same name, return type and number of arguments but

with different logic. Overriding is concerned with an inheritance aspect of object oriented programming. 3rd Pillar - Abstraction Abstraction refers to hide all non-essential details from the user and display only the essential part of it. Abstraction is the process of formulating general concepts by abstracting common properties of instances. Abstraction also comes in 2 forms: control abstraction and data abstraction. Control abstraction is abstracting the method, whereas, data abstraction is abstracting the data. Car is a very good example to define abstraction. A person driving the car only needs to know where the gear, clutch, brakes, etc. are present, but he does not need to know how the transmission works, how much fuel is released upon pressing the race, etc. Abstraction is achieved by forming hierarchical classification or layers. These layers are formed as per the requirement. Let us talk about a car where you have a sound system as the top most layer followed by radio, cd/dvd player that works as a second layer and the last layer is the system that powers the sound system. In java, abstraction is carried out in a way where the object works as the top most layer and all the variables and methods are accessed by it. With abstraction a user is given a handle to access the data but doesn't need to know anything about the internal working of the system. Thus, abstraction is hiding of essential details from the user's point of view. 4th Pillar - Inheritance Inheritance is the feature that makes java programming language more powerful and promotes code reusability. In the present world, code reusability has become very important and without it no programming language is complete. Inheritance allows data and behavior of one class to be shared across classes that are present in the same inheritance hierarchy. For example, a dog belongs to animal class and golden retriever is a type of dog. So, all the features of animal class are inherited by the dog class, which in turn are inherited by golden retriever. Thus, inheritance promotes reusability of code by allowing classes within the same inheritance hierarchy to share common data and behavior. 5th Pillar - Interface Inheritance provided the benefit of code reusability but had a major drawback where classes belonging to different hierarchy could also share the same code. Consider a situation where the fly method of bird class is being shared by the airplane class because both fly and make use of inheritance. This use of inheritance is prohibited. Instead, make use of interfaces to share common behavior among classes present in different hierarchies. If you want to share the fly method of bird class with an airplane class, create an interface with a fly method and keep common variables in it. Now, the class that wants to use the fly method must implement that interface and define its own body. So, both bird and airplane class will have different fly method. Thus, interface is another strong pillar of java without which java could not have survived the market.

Das könnte Ihnen auch gefallen