Showing posts with label oops interview. Show all posts
Showing posts with label oops interview. Show all posts

Understanding Object Oriented Programming

Understanding Object Oriented Programming

Object oriented programming is a helpful way of organizing code. By mapping physical objects to code objects, programmers are able to think more easily about a problem.

OO Terminology

class - an abstract representation of something with certain properties and abilities.

object - a specific instance of a class whose state is unrelated to any other object of the same class. Objects are instantiated, or created, from a class definition.

methods - the functions of a class which gives its objects certain capabilities.

members - the variables of a class which gives its objects certain properties.

constructor - a special function of a class that instantiates an object of that class and initializes its members to some default values specified by the programmer.

accessor (getter) - a method which retrieves a private value in an object.

mutator (setter) - a method which updates a private value in an object.

static - denotes a method or member that belongs to the entire class, not a specific object. For methods, this denotes that the method is called by the class, not a specific object. For variables, this denotes that the variable value is shared across all instantiated objects and should be accessed through the class.

scope - the visibility / context of a variable

inheritance - when a more general class provides an outline for a more specific class

Example: A Rectangle class can inherit from a Shape class.

The general class is called the parent or base class, and the specific class is called a subclass of the base class.

Inheritance removes code duplication and allows for greater code reuse. This is good because having multiple copies of the same code can be a problem if you have to change something later and need to find all of the copies to change. Code reuse allows changes to be made in one central location, propagating the results down where they are needed.

polymorphism - allowing multiple implementations for a method depending on the context; generally refers to method overloading and method overriding.

overload - multiple methods with the same name but different parameters.

override - the replacement of a parent class's method with a more specific implementation of the method by the subclass.

encapsulation - the hiding of implementation details to achieve a simpler interface.

Example:
// Class definition for animals in a pet store.
class Animal {
// Member variables.
// These variables are encapsulated; direct access to them is not
// allowed, but instead is granted by the accessor functions below.
private String name;
private int id;
private float price;

// Constructor
public Animal(String name, int id, float price) {
this.name = name;
this.id = id;
this.price = price;
}

// Methods
// setPrice() is a polymorphic method because
// it is an example of method overloading.
public void setPrice(float price) {
this.price = price;
}

// Mutator (setter) method.
public void setPrice(int price) {
this.price = (float) price;
}

// Accessor (getter) methods.
public String getName() {
return this.name;
}

public int getId() {
return this.id;
}

public float getPrice() {
return this.price;
}
}

// Object instantiation.
Animal fido = new Animal();

Practice Question: What is the difference between an interface and abstract class?

An interface specifies a set of methods, usually grouped under a common theme. It cannot have variables except for static final variables, used as constants. A class implements an interface by implementing all of the functions defined in the interface. A class can implement multiple interfaces to achieve different functionalities.

An abstract class is an incomplete class definition which declares methods for its subclasses to provide implementations for. It can contain variables. Because Java does not support multiple inheritance, a class may only extend one abstract class.

Neither can be instantiated.

Practice Question: Does Java allow multiple inheritance? Why / why not?

Java does not allow multiple inheritance because of the semantic ambiguity involved in permitting classes to have multiple super classes. The classic issue is the diamond problem:

Suppose two classes B and C inherit from A, and class D multiple inherits from both B and C. If a method in D calls a method defined differently in both B and C, then which function should D run?

However, Java allows multiple interfaces to be implemented. In the case of two interfaces with the same method, a compile time error results if the two methods do not have the exact same return type.

object oriented features Interview Questions oops

object oriented features Interview Questions oops
1. Explain what is an object?
An object is a combination of messages and data. Objects can receive and send messages and use messages to interact with each other. The messages contain information that is to be passed to the recipient object.
2. Explain about the Design Phase?
In the design phase, the developers of the system document their understanding of the system. Design generates the blue print of the system that is to be implemented. The first step in creating an object oriented design is the identification of classes and their relationships.
3. Explain about parametric polymorphism?
Parametric polymorphism is supported by many object oriented languages and they are very important for object oriented techniques. In parametric polymorphism code is written without any specification for the type of data present. Hence it can be used any number of times.
4. Explain about multiple inheritance?
Inheritance involves inheriting characteristics from its parents also they can have their own characteristics. In multiple inheritances a class can have characteristics from multiple parents or classes. A sub class can have characteristics from multiple parents and still can have its own characteristics.
5. Explain the mechanism of composition?
Composition helps to simplify a complex problem into an easier problem. It makes different classes and objects to interact with each other thus making the problem to be solved automatically. It interacts with the problem by making different classes and objects to send a message to each other.
6. Explain about overriding polymorphism?
Overriding polymorphism is known to occur when a data type can perform different functions. For example an addition operator can perform different functions such as addition, float addition etc. Overriding polymorphism is generally used in complex projects where the use of a parameter is more.
7. Explain about inheritance?
Inheritance revolves around the concept of inheriting knowledge and class attributes from the parent class. In general sense a sub class tries to acquire characteristics from a parent class and they can also have their own characteristics. Inheritance forms an important concept in object oriented programming.
8. Explain the rationale behind Object Oriented concepts?
Object oriented concepts form the base of all modern programming languages. Understanding the basic concepts of object-orientation helps a developer to use various modern day programming languages, more effectively.
9.Explain about instance in object oriented programming?
Every class and an object have an instance. Instance of a particular object is created at runtime. Values defined for a particular object define its State. Instance of an object explains the relation ship between different elements.
10) Explain about encapsulation?
Encapsulation passes the message without revealing the exact functional details of the class. It allows only the relevant information to the user without revealing the functional mechanism through which a particular class had functioned.
11) Explain about polymorphism?
Polymorphism helps a sub class to behave like a parent class. When an object belonging to different data types respond to methods which have a same name, the only condition being that those methods should perform different function.
12) Explain about object oriented databases?
Object oriented databases are very popular such as relational database management systems. Object oriented databases systems use specific structure through which they extract data and they combine the data for a specific output. These DBMS use object oriented languages to make the process easier.
13) What are all the languages which support OOP?
There are several programming languages which are implementing OOP because of its close proximity to solve real life problems. Languages such as Python, Ruby, Ruby on rails, Perl, PHP, Cold fusion, etc use OOP. Still many languages prefer to use DOM based languages due to the ease in coding.
14) Explain the implementation phase with respect to OOP?
The design phase is followed by OOP, which is the implementation phase. OOP provides specifications for writing programs in a programming language. During the implementation phase, programming is done as per the requirements gathered during the analysis and design phases.
15) Explain about Object oriented programming?
Object oriented programming is one of the most popular methodologies in software development. It offers a powerful model for creating computer programs. It speeds the program development process, improves maintenance and enhances reusability of programs.
16) Explain about a class?
Class describes the nature of a particular thing. Structure and modularity is provided by a Class in object oriented programming environment. Characteristics of the class should be understandable by an ordinary non programmer and it should also convey the meaning of the problem statement to him. Class acts like a blue print.

OOPs Java Basic Interview Questions and answers

OOPs Java Basic Interview Questions and answers
1. What is a virtual function in C++?
Simply put, the virtual keyword enables a function to be ‘virtual’ which then gives possibility for that function to be overridden (redefined) in one or more descendant classes. It is a good feature since the specific function to call is determined at run-time. In other words, a virtual function allows derived classes to replace the implementation provided by the base class.
2. What is the difference between private, protected, and public?
These keywords are for allowing privilages to components such as functions and variables.
Public: accessible to all classes
Private: accessible only to the class to which they belong
Protected: accessible to the class to which they belong and any subclasses.
3. What is a cartesian product in PL/SQL?
When a Join condition is not specified by the programmer or is invalid(fails), PL/SQL forms a Cartesian product.
In a Cartesian product, all combinations of rows will be displayed.
For example, All rows in the first table are joined to all rows in the second table. It joins a bunch of rows and it’s result is rarely useful unless you have a need to combine all rows from all tables.
4. What is mutual exclusion? How can you take care of mutual exclusion using Java threads?
Mutual exclusion is where no two processes can access critical regions of memory at the same time.
Java provides many utilities to deal with mutual exclusion with the use of threaded programming.
For mutual exclusion, you can simply use the synchronized keyword and explicitly or implicitly provide an Object, any Object, to synchronize on.
The runtime system/Java compiler takes care of the gruesome details for you. The synchronized keyword can be applied to a class, to a method, or to a block of code. There are several methods in Java used for communicating mutually exclusive threads such as wait( ), notify( ), or notifyAll( ). For example, the notifyAll( ) method wakes up all threads that are in the wait list of an object.
5. What are some advantages and disadvantages of Java Sockets?
Some advantages of Java Sockets:
* Sockets are flexible and sufficient. Efficient socket based programming can be easily implemented for general communications.
* Sockets cause low network traffic. Unlike HTML forms and CGI scripts that generate and transfer whole web pages for each new request, Java applets can send only necessary updated information.
Some disadvantages of Java Sockets:
* Security restrictions are sometimes overbearing because a Java applet running in a Web browser is only able to establish connections to the machine where it came from, and to nowhere else on the network
* Despite all of the useful and helpful Java features, Socket based communications allows only to send packets of raw data between applications. Both the client-side and server-side have to provide mechanisms to make the data useful in any way.
* Since the data formats and protocols remain application specific, the re-use of socket based implementations is limited.