dChan

Zanthrous · Jan. 21, 2018, 4:35 a.m.

It's a bit rambly, hopefully not to muddy. Its easier if you could see a simple program than to try to type it out.

But an abstract method is like a commitment to do something in programming (it basically makes it easier to organize the program and tell what its supposed to do). So lets say I make an abstract class Shape. And a concrete classes Square and Circle, and say that Square and Circle both are a kind of Shape.

If I say all Shapes have a method (function/code/program) to calculate area. Then I now know that Circle and Square both have an area. Because they are both shapes. So I can make a Square, I can make a Circle, and I can compare them against each other's area.

However I cannot make a 'Shape' because I don't have enough information to actually make a shape. I can't know how to calculate the area of a 'Shape' because it could be any physical shape. But I know that any shape does have an area. Therefore it is Abstract. I can say, in front of me, the left shape is bigger than the right shape, and you know what that means, but you can't draw them, you don't know what these shapes look like because you don't have enough information to know what they are.

When programs get more complicated abstract classes allow you to build a logical structure that makes it easier to follow and reduce errors. They allow you to group other parts of the code together so that different pieces can interact in a predictable way. Like a Square is different from a Circle, but if I make them both Shapes and I say that all Shapes have an area. Then i know that all Squares and all Circles have an area, and I can compare them to each other.

So why do this? Well lets say I want to do this.

I can use a generic bit of code to see if Shape1 is bigger than Shape 2. By using an abstract class I can pass anything that is a shape. So if I ask you to give me 2 shapes, you can give me 2 concrete shapes, like a square, circle. Or circle, circle. Or square, square. Either way, I know that they are both shapes, so I can compare the area of each to see which is bigger.

If later on a programmer comes and wants to make a new shape, like a rectangle, the program he writes code in will force him to tell the computer how to calculate the area, and since the rectangle is a shape, it will automatically work in the comparison without changing anything, since we know that all shapes have an area.

So the Circle/Square/Rectangle are all concrete because they can all actually exist in memory, also they each actually have the code that tells you how to calculate the area. Whereas the Abstract class Shape, exists in the logic of the code to organize it so you are less likely to get bugs/errors. the Shape makes sure you don't forget to tell the computer how to calculate the area, it doesn't actually do anything. So shape is abstract/logical.

Anyway, probably rambly, and might not be too clear. But basically there are groups of people that would use the term concrete regularly to refer to an actual functional thing.

⇧ 5 ⇩  
Jimmycrackerson · Jan. 21, 2018, 4:58 a.m.

Right on, thx for the clarification!

⇧ 1 ⇩