Java is a class-based object-oriented programming (OOP) language built around the concept of objects. Run Time Polymorphism is an example for method overriding.Ĭheck Polymorphism in Java article for more details on Polymorphism.Using OOP concepts to write high-performance Java code (2023) By Anna Monus | Posted | 14 min. In this case, which method to call will be decided at the run time time based on what object is actually pointed by the reference variable. Static/Compile Time polymorphism is an example for method overloading. In this case, which method to call will be decided at the compile time itself based on number or type of the parameters There are 2 types of Polymorphism available in Java In java, we can achieve polymorphism using method overloading and method overriding Since the object takes multiple forms, it is called Polymorphism. Polymorphism is the concept where an object behaves differently in different situations There are several types of Inheritance supported in JavaĬheck Inheritance in Java article for more details on Inheritance We can create more specific classes by inheriting fields and methods from the Generic class.Īnd we can add additional fields and methods in the Subclass. The whole idea behind inheritance is “ Re-usability ”. We use “ extends ” keyword to achieve the same. The class which inherit the properties is called Subclass or Child class. The class from which the properties are inherited is called Super class or Parent class One class inherits or acquires the properties of another class. When we create a class in Java, It means we are doing encapsulation.Įncapsulation helps us to achieve the re-usability of code without compromising the security.Ĭheck Encapsulation in Java article for more details We can restrict the access to the members of a class using access modifiers such as private,protected and public keywords Since encapsulation wraps both fields and methods in a class, it will be secured from the outside access. Since variable “c” is pointing to Audi class object, we can see below outputĬheck Interfaces in java article for more detailsĮncapsulation is the process of binding object state(fields) and behaviors(methods) together in a single entity called “Class”. So using abstract class, we can achieve 0 to 100 % abstraction. Inside Abstract class, we can declare abstract methods as well as concrete methods If we make any class as abstract then it can't be instantiated which means we are not able to create the object of abstract class. We can achieve “ abstraction ” in Java using 2 waysĪbstract class in Java can be created using “ abstract ” keyword. So, this way, we hide the internal implementation and abstract it inside a method. So, anyone who calls this method will not be knowing the internal complexity of the method but will be knowing the functionality of the method and hence he calls it and uses it. Similarly, in java we can write a method to perform some functionality inside a class and we can expose it to outside world just by providing an option to call this method. Similary, we apply brakes whenever we want to stop the car but we are not sure how the braking system works internally. So, gearbox working is abstracted and provided with simple mechanism of changing gears to the outside world. Similarly, we drive a car and change the gears as and when its required to go high speed and come back to low speed but We don’t know how the gear box works internally. These functionalities have been kept inside and we are just accessing them using the options provided in the mobile. We use mobile phone everyday but we don’t know how the functionalities are designed inside it so that we receive the calls and send messages etc. So complex functionality can be made available to the outside world in a simple way. Similarly we can provide different values for each field for “s2” and “s3” and we call this as a “ state ” of an object.Ĭheck class-and-objects-in-java article to know more about Class and ObjectsĪbstraction is the concept of hiding the internal details of a functionality and providing a simple representation for the same.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |