How to create an instance of concrete class from static method of abstract class

8Observer8

I try to create an instance of concrete class from static method of abstract class. But I have this error:

Uncaught TypeError: Object prototype may only be an Object or null: undefined

On this line of code in ConcreteClass.js: return extendStatics(d, b);

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };

My project files:

Program.ts

import { AbstractClass } from "./AbstractClass";

class Program
{
    public static Main()
    {
        let instance = AbstractClass.CreateObject();
        instance.Method();
    }
}
Program.Main();

AbstractClass.ts

import { ConcreteClass } from "./ConcreteClass";

export abstract class AbstractClass
{
    public static CreateObject()
    {
        return new ConcreteClass();
    }

    public abstract Method(): void;
}

ConcreteClass.ts

import { AbstractClass } from "./AbstractClass";

export class ConcreteClass extends AbstractClass
{
    public Method() : void
    {
        console.log("Method of ConcreteClass");
    }
}
Shaun Luttin

The problem has to do with a circular import. AbstractClass and ConcreteClass are importing each other and the definition of each is making use of the other. The specific problem is that when ConcreteClass is extending AbstractClass, the AbstractClass is still undefined, because it is waiting for ConcreteClass to finish loading. As a result, the runtime is seeing something like vaguely this:

import { AbstractClass } from "./AbstractClass";

// at runtime AbstractClass has not finished loading yet so it is undefined
export class ConcreteClass extends undefined
{
    public Method() : void
    {
        console.log("Method of ConcreteClass");
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create an abstract class with static factory method?

How to resolve concrete non abstract class instance

How is abstract class different from concrete class?

Typescript static method in an abstract class to create an instance of self

Java: How to Create an instance of a subclass from with a Static method of the super class

static method from abstract class

How to return instance of concrete class from method with generic return type

How to instantiate class from static method of abstract class

Instance child class in abstract static method

How to access the attributes of an object instantiated via a constructor in an abstract class, from an overridden method in a concrete child class?

Kotlin concrete class extending from abstract class and interface, with the interface using a method implemented in the abstract class

Coming from Ruby: How to move a static method into abstract class in TypeScript?

Method that accepts an instance of the concrete class

Get Concrete Class name from Abstract Class

Generics: Create instance from abstract class

Return a class, not an instance, of a concrete class that extends an abstract base class?

How is it possible to create an instance of an abstract class?

Defining all abstract method in one concrete class

Abstract and concrete method with same signature in generic class

Create new class instance from class method

Create new class instance from class method

Returning a class reference from the static method of an abstract class

Refactoring a concrete method in abstract class which contains an abstract method

calling an abstract method in a concrete method inside an abstract class in C#

Fluent NHibernate multiple abstract class inheritance. How create table per concrete class without create table in db for abstract class

How to create the instance of a concrete implementation of a class in C++ as in Java?

How to call abstract method from abstract class called by inherit class

How to create a class with an abstract `__init__` method?

Static factory method in abstract class