Showing posts with label oops concepts. Show all posts
Showing posts with label oops concepts. 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.

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.