Skip to main content

Posts

Showing posts with the label OOP

Encapsulation VS Abstraction in Object-Oriented Programming

Encapsulation  binds together the data and functions that manipulate the data, keeping it safe from interference and misuse. Real-World Example:   Every time you log into your email account( Gmail, Yahoo, Hotmail or official mail), you have a whole lot of processes taking place in the backend, that you have no control over. So your password, would probably be retrieved in an encrypted form, verified and only then you are given access. You do not have any control, over how the password is verified, and this keeps it safe from misuse. Abstraction  is a process of hiding the implementation from the user, only the functionality is exposed here. So you are aware only of what the application does, not how it does it. Real-World Example:   When you log into your email, compose and send a mail. Again there is a whole lot of background processing involved, verifying the recipient, sending a request to the email server, sending your email. Here you are only interested in compo...

Difference between abstract class and interface in OOP

Source: Amit Sethi In Interface: > All variables must be public static final. > No constructors. An interface can not be instantiated using the new operator.   > All methods must be public abstract .  

OOP concepts in Java

What is the Class?  The class is a group of similar entities. It is extensible program code for creating objects and to define data types as well as methods. What is Object in OOP? Object refers to a particular instance of a class whereas objects can be a combination of variables, functions and data structures. What are the 4 principles in OOP? Encapsulation, Data Abstraction, Polymorphism and Inheritance. What is Inheritance? Deriving a new class from an existing class and forming them into a hierarchy of classes. What is Encapsulation?   It is used to hiding the implementation details. What is Constructor overloadin?  It is a feature that allows a class to have more than one constructor having different argument lists. What is the difference between method overloading vs method overriding? Why do we need to use Method Overloading? Suppose that you have a class that can use calligraphy to draw various types of data (strings, in...

Static and Dynamic Polymorphism in OOP

  There are two types of Polymorphism. 1. Static Polymorphism. 2. Dynamic Polymorphism. Static polymorphism is achieved through method overloading. Method overloading means there are several methods present in a class having the same name but different types/order/number of parameters. At compile time, compiler knows which method to invoke by checking the method signatures.  So, this is called compile time polymorphism or static binding. The concept will be clear from the following example: class Adder { static int add ( int a , int b ){ return a + b ; } static int add ( int a , int b , int c ){ return a + b + c ; } } class Main { public static void main ( String [] args ){ System . out . println ( Adder . add ( 11 , 11 ); System . out . println ( Adder . add ( 11 , 11 , 11 ); } } Dynamic Polymorphism: Suppose a sub class overrides a particular method o...

Compare Static and Dynamic Binding

 Connecting a method call to the method body is known as binding. There are two types of binding: -static binding (also known as early binding).  -dynamic binding (also known as late binding). Static binding in occurs during compile time while dynamic binding occurs during runtime. private , final and static methods and variables use static binding and are bonded by compiler while virtual methods are bonded during runtime based upon runtime object. Static binding uses Type ( class in Java) information for binding while dynamic binding uses object to resolve binding. Overloaded methods are bonded using static binding while overridden methods are bonded using dynamic binding at runtime. 

Object Oriented Programming Concepts & Questions

1. What is Dynamic Binding?  Binding means the link between procedure call and code to be executed. Dynamic binding means link exists between procedure call and code to be executed at    run time when that procedure is called. It is also known late binding.  It is generally used with polymorphism and inheritance. For example, compiler comes to know at runtime that which function of 'sum' will be called either with two arguments or with three arguments.   2. What is the difference between Dynamic Binding in C++ and Java?  There’s no virtual keyword in Java because all non-static methods always use dynamic binding. In Java, the programmer doesn’t have to decide whether or not to use dynamic binding.  Virtual Function is a function in the base class, which is overridden in the derived class, and which tells the compiler to perform dynamic Binding on this function. The reason virtual exists in C++ is so you can leave it off for...