Tuesday, May 3, 2016

Difference Between Inheritance In Java And C++Tccicomputercoaching.Com

Before to differentiate inheritance in two different languages Java and C++, first we should know what is inheritance?
Definition:
Inheritance is mechanism to build new class from old class.
Why useful Inheritance in programming?
Inheritance is used in both languages for reusing code and/or creating is-a relationship.
We can create new ones, we can capitalize on efforts, which went into the designs, implementation, testing of existing software.
Inheritance is a feature in object-oriented programming which is very similar to the way we inherit characteristics from our parents.
The class from which we inherit is called base/parent/super class, the class in which we inherit is called derived/ child/ sub/ class.
Class A ------------------------------------------------------ Class B
Base class Derived class
Parent class Child class
Super class sub class
There may be one or more than parent class, same like one or more than sub class. Depends on these no. of base class and sub class we can differentiate the inheritance types.
Single inheritance
One base class and one derived class
Multiple inheritance
One base class and more than one derived class
Multilevel inheritance
One base class, one sub class, again one class inherited from that sub class. Then that sub class is called parent class for its inherited class.
Hierarchical inheritance:
Grandparent to parent1 and parent 2, then same features inherited from parent1 or parent 2 to child class again.
Hybrid inheritance:
Its combinations of two or more inheritance like single, multiple and hybrid inheritance.
Now let’s see Difference between Java and C++:
C++ support multiple inheritance, where java doesn’t support multiple inheritance.
Java supports interface in place of multiple inheritance.
In C++ the parent is referred to as a base class, and the child is called a derived class. In Java, the parent is the
Super class, and the child is the subclass.
Java uses extends keyword for inheritance. Unlike C++, Java doesn’t provide an inheritance specifies like public, protected or private.
In Java, methods are virtual by default. In C++, we explicitly use virtual keyword.
While inheriting a class in C++, user can specify the access specifier like,
classA
{
publicinta;
protectedint b;
};
class Derived1 : **private** A
{
// a will be private here.
// b will be private here.
};
But the same is not possible in Java, i.e. extends in java is always like "public" inheritance in C++.
All classes in Java ultimately inherit from the Object class. C++ can have inheritance trees that are completely unrelated to one another.
If you like this post then please share And Visit Our Website: http://tccicomputercoaching.com/

No comments:

Post a Comment