These days ,I am learning inheritance and dynamic binding.

I could figure out what the code means.

However, I can't see much point in setting such base classes

So,I want to know why should we set up base classes and derived classes

Recommended Answers

All 3 Replies

If you can figure out what the code means, then I suggest you take an example that someone else has written that uses inheritance and try to rewrite it into a program that behaves the same way but does not use inheritance.

A base class, as the name suggests, acts as a 'base' for derived classes. Example: class Car has 3 derived classes, Toyota,Honda and Chevrolet. Functions and variables you want to use in Toyota but not in Honda can be in Car. Also, methods and variables that you want in all derived classes can be in Car, so u dnt need to define them all over again (re-inventing the wheel).

@tkud: What you are describing is what I call "derived class intersection", which is a very classic misunderstanding of what base classes are useful for. I suggest you revisit your understanding of the topic. Learn from the saying "Inherit not to reuse, but to be reused".

@tiredoy: The main purpose of base classes is to allow substitution of derived class objects for base class objects. If you develop an algorithm or data structure of some kind, you generally require that the input has some data and methods, generally called "requirements" which can be regrouped into "abstractions", which in OOP, are realized as base classes. The idea is that you develop the algorithm or methods to work using only what the abstraction provides (data and polymorphic functions). Then, any derived class (or implementation of the abstraction) can be reused by any of the methods that work on the base class. In other words, you can substitute, in the algorithms, objects of derived classes to act in the place of where the algorithms only require an object of the base class, and by this substitution, you can also inject code that modifies (or extends) the behavior of the algorithms. This is polymorphism.

"Compose to reuse"
"Inherit to be reused"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.