Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a method of an abstract class

is it possible to use a method of an abstract class? how can i use a method of a class without having an instance?

like image 377
cemregoksu Avatar asked Mar 27 '10 19:03

cemregoksu


People also ask

How are methods used in abstract class?

To declare an abstract method, use this general form: abstract type method-name(parameter-list); As you can see, no method body is present. Any concrete class(i.e. class without abstract keyword) that extends an abstract class must override all the abstract methods of the class.

Can abstract classes have methods?

An abstract class can have an abstract method without body and it can have methods with implementation also. abstract keyword is used to create a abstract class and method.

How do you use an abstract method in Java?

An abstract method in Java is declared through the keyword “abstract”. While the declaration of the abstract method, the abstract keyword has to be placed before the name of the method. There is no body in an abstract method, only the signature of the method is present.

Can we write method definition in abstract class?

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.


3 Answers

If you declare a method as static, you can call it directly without needing a class instance. Otherwise you will need to have an instance of a derived class.

Since an abstract class cannot be instantiated directly, you cannot call a method of an abstract class directly unless it is a static method. But you can call a static method of an abstract class directly, here is a quick example:

#include <iostream>
#include <ostream>
#include <fstream>
using namespace std;

class stest{
  public:
   static void test();
   virtual void a() = 0;
  };

void stest::test(){ cout << "test\n"; }

int main(){
  stest::test();
  return 0;
}

Alternatively, if you have an instance of a class that is derived from an abstract class, you can treat it as an instance of the abstract class, and can call any methods on it.

like image 74
Justin Ethier Avatar answered Oct 05 '22 22:10

Justin Ethier


Abstract class doesn't imply you don't have an instance, it implies that the runtime type of the instance is actually some derived class that provides implementations for the pure virtual functions in the abstract base class. But not all member functions of an abstract class have to be pure virtual, you can have a mix of concrete and abstract functions.

When you call member functions "on the abstract class", all virtual functions, including the pure virtual ones, are called polymorphically. So the override defined in the derived class gets executed. Non-virtual functions call the definition in the base class, you can't have pure concrete functions so even an abstract class has to provide implementation for non-virtual functions.

It's even possible for a pure virtual function to have an implementation provided by the abstract base class. An override still has to be provided, but then the override can call the base class implementation.

like image 40
Ben Voigt Avatar answered Oct 05 '22 23:10

Ben Voigt


I think you can do it using a pointer.

#include <iostream>

class A
{
    public:
    virtual void showName()=0;
    void show(){std::cout<<"class A"<<std::endl;}
};

int main()
{
    A *a;
    a->show();
}
like image 30
user3052134 Avatar answered Oct 05 '22 22:10

user3052134