7 Replies - 375 Views - Last Post: 31 October 2017 - 11:16 AM Rate Topic: -----

#1 GoldenCompass  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 29-October 17

Create an Object of an Abstract Class

Posted 29 October 2017 - 04:15 AM

Is it possible to create an object of an abstract class on a forms based application. I have two list boxes on my application. One list box displays the name of each shape in the list all shapes. The second list box displays information about the selected shape. Does anyone know how I can achieve this? The classes in my program are;

abstract class Shape
Circle : Shape
Trapezium : Shape

I would be most grateful if someone could help and advise.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Create an Object of an Abstract Class

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,112
  • Joined: 05-May 12

Re: Create an Object of an Abstract Class

Posted 29 October 2017 - 06:22 AM

The point of an abstract class is that you can't instantiate it. You can instantiate concrete classes that derive from the abstract class.
Was This Post Helpful? 0
  • +
  • -

#3 GoldenCompass  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 29-October 17

Re: Create an Object of an Abstract Class

Posted 29 October 2017 - 07:55 AM

View PostSkydiver, on 29 October 2017 - 06:22 AM, said:

The point of an abstract class is that you can't instantiate it. You can instantiate concrete classes that derive from the abstract class.


Is there anyway around this?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,112
  • Joined: 05-May 12

Re: Create an Object of an Abstract Class

Posted 29 October 2017 - 08:22 AM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

Why would you need away around this. By the use of polymorphism, this works out nicely. Your listboxes should be databound to lists of the abstract class. The listbox item renderer simply calls the virtual method or property on the abstract class that gives the thing to be displayed for that listbox. The lists of abstract classes are populated by inserting concrete classes.

I question the application design that requires two listboxes. I would consider one listbox that lets the user choose which one they want to inspect, and a textbox or property grid that displays the details of the selected item. With that design, you would only need one list of abstract classes.
Was This Post Helpful? 0
  • +
  • -

#5 GoldenCompass  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 3
  • Joined: 29-October 17

Re: Create an Object of an Abstract Class

Posted 29 October 2017 - 08:30 AM

Would you be ever so kind to implement that program in c# on a forms application? I am struggling to understand your point?
Was This Post Helpful? -1
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5894
  • View blog
  • Posts: 20,112
  • Joined: 05-May 12

Re: Create an Object of an Abstract Class

Posted 29 October 2017 - 08:56 AM

Sorry, no. You are right on the line with regards to this site's "gimme the codez" policy.
Was This Post Helpful? 1
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,983
  • Joined: 21-June 11

Re: Create an Object of an Abstract Class

Posted 29 October 2017 - 09:30 AM

View PostGoldenCompass, on 29 October 2017 - 04:30 PM, said:

I am struggling to understand your point?


The point is: you can't instantiate Shape in your example, nor is there any reason that you should need to. All you need are instances of Circle and Trapezium, of which you can create as many as you want.
Was This Post Helpful? 0
  • +
  • -

#8 aidenkael  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 59
  • View blog
  • Posts: 275
  • Joined: 22-October 13

Re: Create an Object of an Abstract Class

Posted 31 October 2017 - 11:16 AM

View PostGoldenCompass, on 29 October 2017 - 10:30 AM, said:

I am struggling to understand your point?


To give you an example:
public abstract class Shape
{
.....STUFF......
}

public class Triangle : Shape
{
.....STUFF.......
}



To instantiate, you need to use the concrete implementation of Shape (in this case, it is Triangle)

Triangle triangle = new Triangle();



For a reference on polymorphism check out this from Tutorials Point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1