answersLogoWhite

0


Best Answer

You can't have a subclass without inheritance. A subclass is just another name for a derived class, and derived classes inherit all the public and protected members of their base classes. You cannot have a base class with neither public nor protected members -- there has to be at least one exposed interface, even if it is only a static public method.

I suspect you are actually asking how to call a specific method of a derived class when all you have is a pointer to its base class. The correct solution is to use inheritance and call a virtual method of the base class. But what do you do when the specific method you want to call is non-generic?

This is a classic case of poor class design. The problem is not that you are trying to extort non-generic behaviour from a base class, the problem is in how you are going about it. If you only have a pointer or reference to the base class then you are expected to make use of its generic interface. If you need a more specific interface, then you need a pointer or reference to the more specific type, not its generic type. Unfortunately, this may not always be possible, especially when dealing with legacy code for which you have no control over. In these cases, the only solution is to resort to expensive runtime type information and dynamically cast your base class pointer to the appropriate type. If the return value is NULL then the base class does not point at your expected derivative, so you must check the return value is non-NULL before calling the specific method. This is the only safe way of doing it, in the absence of a suitable generic interface.

This, in turn, means your calling code requires knowledge of that specific type. While that's generally not a problem when the calling code is external to the base class, it is a problem when the calling code is the base class itself. This means your base class must be blessed with knowledge about some or all of its derivatives. To be blunt, that's a maintenance nightmare because every time you derive a new object for which you wish to extort specific behaviour you must also update the base class in order to accommodate that new derivative. Quite simply this is a sloppy way of going about things.

The best solution is, of course, to rethink your design. Separate your interfaces from your implementations and gather all the common interfaces together in the base class itself. Look into ways of generalising the more specific methods such that it is possible to call those methods from the generic interface, without the need for runtime type information. Use arguments as flags to allow greater flexibility, and let the more specific implementations work out for themselves exactly which calls to make based upon those arguments. You cannot avoid using inheritance, of course, but ultimately it is the correct way of doing things. Your code will be cleaner and easier to maintain in the long run. And it all starts with good design.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Use some methods of a class in subclass without inheritance in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is every class in java is subclass of itself?

A class can be a subclass of another class, not of itself.A class can be a subclass of another class, not of itself.A class can be a subclass of another class, not of itself.A class can be a subclass of another class, not of itself.


When a subclass can call a parent's class constructor?

A subclass invokes its base class constructor at the point of instantiation. That is; you cannot instantiate a subclass object without first constructing its base class, which is done automatically.


In java why method defined in super class need not to defined in subclass?

In Java, or in any object oriented language such as C++, a method defined in super (parent) class does not need to be defined in a subclass, because that is the primary purpose of inheritance. Object oriented programming allows you to define and declare a class that implements the behavior for an object. Inheritance allows you to refine, or subclass, that class by "reusing" all of the functionality of the parent class into the sub class, adding additional definition and declaration for the sub class. If the subclass needs to change a parent class method, it can overload that method. This is called abstraction.


Can you have inheritance without polymorphism?

Yes. Inheritance and polymorphism are two different things. Inheritance is when the attributes and methods of a class are inherited by a deriving class that creates a more specialized type. Polymorphism is when two methods exist with the same name, differing only in argument types, or in class type. The former type, argument types, is an example of ad-hoc polymorphism that does not even require a class.


What portions of a super class can be used by subclass?

Any members of a superclass which are declared as public or protected can be used by all subclasses.

Related questions

Which object-oriented concept defines the relationship between a subclass and a super class?

Inheritance


What is heritance in java?

With inheritance, you can use methods and fields from the superclass in a subclass. So for example when I have a class Person with fields age and gender, I can make a subclass Student. a Student object has always the fields from its superclass Person (age and gender), but you can make extra fields for a Student object. The same is true for methods: a method defined in the Person class can also be used on a Student object because Student is a subclass from Person. Got it? ;)


Is every class in java is subclass of itself?

A class can be a subclass of another class, not of itself.A class can be a subclass of another class, not of itself.A class can be a subclass of another class, not of itself.A class can be a subclass of another class, not of itself.


When a subclass can call a parent's class constructor?

A subclass invokes its base class constructor at the point of instantiation. That is; you cannot instantiate a subclass object without first constructing its base class, which is done automatically.


In java why method defined in super class need not to defined in subclass?

In Java, or in any object oriented language such as C++, a method defined in super (parent) class does not need to be defined in a subclass, because that is the primary purpose of inheritance. Object oriented programming allows you to define and declare a class that implements the behavior for an object. Inheritance allows you to refine, or subclass, that class by "reusing" all of the functionality of the parent class into the sub class, adding additional definition and declaration for the sub class. If the subclass needs to change a parent class method, it can overload that method. This is called abstraction.


What is a sub class?

In object oriented programming approach subclass is derived from parent class. This term is generally used in concept called "Inheritance" Example [PHP] class A { //class A definition } class B extends A { //class B definition } In above example class A is parent class and class B is subclass/child class .


What is sub classes?

In object oriented programming approach subclass is derived from parent class. This term is generally used in concept called "Inheritance" Example [PHP] class A { //class A definition } class B extends A { //class B definition } In above example class A is parent class and class B is subclass/child class .


Can you have inheritance without polymorphism?

Yes. Inheritance and polymorphism are two different things. Inheritance is when the attributes and methods of a class are inherited by a deriving class that creates a more specialized type. Polymorphism is when two methods exist with the same name, differing only in argument types, or in class type. The former type, argument types, is an example of ad-hoc polymorphism that does not even require a class.


What is a subclass and when is a subclass of use in data modeling?

A subclass is a specialized version of a class that inherits attributes and methods from its parent class. In data modeling, a subclass is useful for representing entities that have additional attributes or behaviors that are specific to a subset of instances of the parent class. Subclasses help organize data more efficiently and allow for more specific modeling of a system.


Short note on inheritance?

Inheritance is used object oriented program. When you create a class, you can create a child class that inherits methods and data from the parent class.


What portions of a super class can be used by subclass?

Any members of a superclass which are declared as public or protected can be used by all subclasses.


What is the similarity between abstract class and class?

They provide the same level of abstraction and encapsulation, and similar inheritance. The differences in inheritance: an abstract class forces you to have at least 1 subclass (otherwise this abstraction branch is useless, because you won't be able to create any instance), while a non-abstract class may have optional subclasses.