Basic Concepts in Oops - 3

Hi everyone, this is the continuation of the previous post. Here i am discussing some more concepts in oops.

11) Encapsulation

The combining of data and functions into a single unit is known as encapsulation. The data is not reachable from outside functions. Encapsulation helps in data hiding.

12) Multiple Inheritance

Multiple inheritance is the type of inheritance in which one subclass inherits from 2 or more base class. In C++, we can do multiple inheritance directly. But in java, we can't do multiple inheritance directly. For that we are using interfaces. An example of multiple inheritance in C++ is shown below.


class Vehicle
{
private:
int vehicleId;
int milage;

public:
Vehicle(int a, int b): vehicleId(a),milage(b)
{
}
};

Class Production
{
private:
int yearOfProduction;

public:
Production(int c): yearOfProduction(c)
{
}
};

Class Car: public Vehicle, public Production
{
private:
int cost;

public:
Car(int a, int b, int c, int d)
: Vehicle(a,b),Production(c),cost(d)
{
}
};


Here Vehicle and Production are the base class and Car is derived class. Multiple inheritance in C++ can cause problems when both the base classes have a same variable name or same function name. In java, multiple inheritance is done with the help of interfaces.

13) Interfaces

An interface is a type of class which declares a set of related methods, outside of any class. In java, an interface is defined using the interface keyword. Methods are only declared in interfaces. They are not defined. In other words, the methods are abstract. Methods are properly defined only by the sub classes, which implement the interface.

Interface does not include any constructors. Interface cannot contain any instance field. The fields should be static and final. Following example shows an example of interface in java.

class Vehicle
{
int vehicleId;
int milage;
Vehicle(int a, int b)
{
vehicleId=a;
milage=b;
}

interface Cost
{
void calculateCost();
}

class Car extends Vehicle implements Cost
{
int yearOfManufacture;
int currentYear=2012;
int cost=0;
Car(int a, int b, int c)
{
super(a,b);
yearOfManufacture = c;
}
public void calculateCost()
{
cost= 20,000 - ((currentYear-yearOfManufacture)*100);
}
}



Here, the subclass Car extends the base class Vehicle and implements Cost. An interface can extend multiple interfaces as well.

14) Abstract Class

An abstract class is similar to an interface. It can have  method implementation. But an interface can have only method definition. Abstract class is a proper class. It can have data members and can be a subclass of other classes.Abstract classes cannot be instantiated, but they can be subclassed. When a derived class extends abstract class, derived class should provide the implementation for abstract method in abstract/base class.

15) Abstract Method

An abstract method is a method, that is declared without an implementation. Following gives an example to abstract class and interface.


abstract class Vehicle
{
abstract void Cost();
}

class Car extends Vehicle
{
int tcost=0;
void cost()
{
int tcost = 20,000;
}
}



Here, Cost is the abstract method and Vehicle is the abstract class.

Check my next post for more about Oops. Comments/ Suggestions are always welcome.

Similar Posts

1) Oops - 1
2) Oops - 2
3) Oops - 4

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++