






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Object-oriented programming refers to languages that uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism and son on, in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function. Object-oriented programming has several advantages such as: ➢ OOP is faster and easier to execute ➢ Provides a clear structure for the programs ➢ OOP helps to keep the PHP code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug ➢ OOP makes it possible to create full reusable applications with less code and shorter development time PS: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it. There are four major principles that make a language Object Oriented. These are Encapsulation, Data Abstraction, Polymorphism and Inheritance. These are also called as four pillars of Object-Oriented Programming.
Encapsulation is the mechanism of hiding of data implementation by restricting access to public methods. Instance variables are kept private and accessor methods are made public to achieve this. For example, we are hiding the name and dob attributes of person class in the below code snippet. Encapsulation: private instance variable and public accessor methods.
Example: public class Employee { private String name; private Date dob; public String getName() { return name; } public void setName( String name) { this.name = name; } public Date getDob() { return dob; } public void setDob( Date dob) { this.dob = dob; } }
Abstract means a concept or an Idea which is not associated with any particular instance. Using abstract class/Interface we express the intent of the class rather than the actual implementation. In a way, one class should not know the inner details of another in order to use it, just knowing the interfaces should be good enough.
Inheritance is a mechanism in which one class acquires the property of another class. For example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields and methods of the existing class. Hence, inheritance facilitates Reusability and is an important concept of OOP. The existing types of inheritance are:
1. Single Inheritance In Single Inheritance one class extends another class (one class only).
As per shown in diagram Class C is subclass of B and B is a of subclass Class A.
4. Hierarchical Inheritance In Hierarchical Inheritance, one class is inherited by many sub classes. As per above example, Class B, C, and D inherit the same class A. 5. Hybrid Inheritance Hybrid inheritance is a combination of Single and Multiple inheritance.
As per above example, all the public and protected members of Class A are inherited into Class D, first via Class B and secondly via Class C.
Polymorphism is a OOPs concept where one name can have many forms. For example, a smartphone for communication. The communication mode you choose could be anything. It can be a call, a text message, a picture message, mail, etc. So, the goal is common that is communication, but their approach is different. This is called Polymorphism. It is further of two types, static and dynamic. Static polymorphism is achieved using method overloading and dynamic polymorphism using method overriding. Difference between Static & Dynamic Polymorphism Static Polymorphism Dynamic Polymorphism It relates to method overloading. It relates to method overriding. Errors, if any, are resolved at compile time. Since the code is not executed during compilation, hence the name static. In case a reference variable is calling an overridden method, the method to be invoked is determined by the object, your reference variable is pointing to. This is can be only determined at runtime when code in under execution, hence the name dynamic.
class Test{ public static void main(String[] args){ DemoOverload demo=new DemoOverload(); System.out.println(demo.add(2,3)); //method 1 called System.out.println(demo.add(2,3,4)); //method 2 called System.out.println(demo.add(2,3.4)); //method 4 called System.out.println(demo.add(2.5,3)); //method 3 called } } In the above example, there are four versions of add methods. The first method takes two parameters while the second one takes three. For the third and fourth methods, there is a change of order of parameters. The compiler looks at the method signature and decides which method to invoke for a particular method call at compile time. Dynamic Polymorphism Suppose a subclass overrides a particular method of the superclass. Let’s say we create an object of the subclass and assign it to the superclass reference. Now, if we call the overridden method on the superclass reference then the subclass version of the method will be called. Example: class Vehicle{ public void move(){
System.out.println(“Vehicles can move!!”); } } class MotorBike extends Vehicle{ public void move(){ System.out.println(“MotorBike can move and accelerate too!!”); } } class Test{ public static void main(String[] args){ Vehicle vh=new MotorBike(); vh.move(); // prints MotorBike can move and accelerate too!! vh=new Vehicle(); vh.move(); // prints Vehicles can move!! } }
➢ https://stackoverflow.com/questions/20783266/what-is-the-difference- between-dynamic-and-static-polymorphism-in-java ➢ https://www.geeksforgeeks.org/polymorphism-in-c/ ➢ https://www.d.umn.edu/~gshute/softeng/presentations/oo- principles.xhtml ➢ https://howtodoinjava.com/oops/object-oriented-principles/ ➢ https://raygun.com/blog/oop-concepts-java/