Abstract Classes

Available

"Hello, Amigo! We have a fascinating new topic."

"Today is just a day of fascinating topics!"

"Why, thank you!"

"You're welcome."

"Remember when we introduced the ChessItem base class to simplify all the classes for chess pieces?"

"Yes."

"Now imagine that each piece has a method that handles rendering the piece on the screen. You call the method and the piece draws itself at its current coordinates. Would it be helpful to move this method into the base class?"

"Yes." After I've learned about polymorphism, I would be able to call the render method for all of the pieces, regardless of their type. Something like this:"

For example:
class ChessBoard
{
  public void drawAllChessItems()
  {
  //draw them regardless of their type.
  ArrayList <ChessItem> items = new ArrayList<ChessItem>();
  items.add(new King());
  items.add(new Queen());
  items.add(new Bishop());

  //draw them regardless of their type.
  for (ChessItem item: items)
  {
   item.draw();
  }
 }
}

"Well done. Exactly. And what would be done by the draw method of the ChessItem class itself?"

"I don't know. Chess doesn't have such a piece. And that means it has no visual representation."

"Precisely. And it doesn't make sense to create a ChessItem object. There is no such chess piece. It's only an abstraction—a class that we made for convenience. That's how abstraction works in OOP: we moved all the important (shared by all pieces) data and methods into a base class, but we kept their differences in the classes corresponding to specific chess pieces."

Abstract Classes - 1

Java has a special class type for this: the abstract class. Here are three things to remember about abstract classes.

1) An abstract class can declare methods without implementing them. Such a method is called an abstract method.

For example:
public abstract class ChessItem
{
 public int x, y; //coordinates
 private int value; //the piece's "value"

 public int getValue() //an ordinary method, returns value
 {
   return value;
 }

 public abstract void draw(); //abstract method. There is no implementation.

}
3
Task
Java Core,  level 2lesson 5
Locked
Code entry
Sometimes you don't need to think, you just need to hammer it out! As paradoxical as it may seem, sometimes your fingers will "remember" better than your conscious mind. That's why while training at the secret CodeGym center you will sometimes encounter tasks that require you to enter code. By entering code, you get used to the syntax and assimilate some material. What's more, you combat laziness.

2) An abstract method is marked with the keyword abstract.

If a class has even one abstract method, then the class is also marked with abstract.

3) You cannot create objects of an abstract class. Code attempting to do so simply won't compile.

Java code Description
ChessItem item = new ChessItem();
item.draw();
This code won't compile.
ChessItem item = new Queen();
item.draw();
But you can do this.

4) If your class inherits an abstract class, you need to override all of the inherited abstract methods, i.e. you have to implement them. Otherwise, your class will also have to be declared abstract. If the class has even one unimplemented method declared directly in the class or inherited from the parent class, then the class is considered abstract.

"But why is all this necessary? Why do we need abstract classes? Isn't it possible to use ordinary classes instead? And instead of abstract methods, can't we just create empty implementations consisting of opening and closing curly brackets?"

"You could. But these restrictions are like the private modifier. We use the private modifier to deliberately block direct access to data, so that other programmers and their classes use our public methods."

The same applies to an abstract class. Whoever wrote the class doesn't want anyone to create instances of the class. On the contrary, the author expects the abstract methods of his or her abstract class to be inherited and overridden.

"I still don't understand why we would want to complicate our lives this way."

"The advantage of this feature is evident in large projects. The more classes you have, the more clearly you need to delineate their roles. You'll see the advantage of doing this, and soon. Everybody has to go through this."

Comments(13)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
mosekaLevel 19 , Nairobi
10 June, 08:29
For instance , in a class called Car it has an abstract method move(). move() is a common method that will be used by its child classes (i.e Sports car, bus, etc). But all these child classes, though they have a common move property, the way/ detail of implementation is different. It makes sense to mark the move method abstract in the Car class. Since an abstract method is not implemented in an abstract class, the child class must inherit that property(move()) and it will be defined/implemented in their respective classes.
Devonte ALevel 15 , Rubery
28 June, 14:10
Very nice explanation!
Lucas HoageLevel 14 , Savannah
9 June, 21:48
I'm not sure I understand. Is this similar in the way we override the default constructor by requiring new objects to have N amount of parameters passed into it when created? So in abstract, whenever a class extends the abstracted class, it MUST override the methods that are abstracted or else it must be declared abstract itself. Both situations just seem like a way too fool proof class extension and object creation.
fifi dengLevel 16 , Paris
4 July, 04:30
the difference between override and overload , we should make clear
Syed Tayyab ul MazharLevel 12 , Karachi
15 May, 17:48
Suppose you've 100 classes, each class has a purpose. Now, suppose one of those classes is meant to be parent class and is not to be instantiated, you could make it like this:
//this class is not meant to be instantiated
public class SomeClass{}
Or you could just make it an abstract class, like this:
public abstract class SomeClass{}
And now without having to write a comment, at first look you or anyone would understand that you're not meant to create objects of this class. That's what I understood from this lesson.
rajLevel 13 , Bhilai
12 May, 09:51
I don't understand
SergheiLevel 26
1 April, 15:01
"Everybody has to go through this."
Robert ConstantinescuLevel 25 , Bucharest
2 November 2019, 09:16
you've done it until here! Great Job & keep going like Musk did :) !
Renat MukhametshinLevel 13 , Pervouralsk
30 August 2019, 11:02
yeah! finally i find out something about this topic and this is clarifyed in some degree
Shashank MishraLevel 18 , Panaji
29 August 2019, 22:19
here enters one above all ;)
EwertonLevel 30 , Belo Horizonte
29 June 2019, 18:14
"I still don't understand why we would want to complicate our lives this way."
Fadi AlsaidiLevel 22 , Carrollton, TX
26 December 2019, 23:09
I am sure you probably have a good idea by now as to why, given the fact that you are at lvl 30. Wow good job sir and wish me the same :) I suppose if you want to make sure all inheriting classes implemented a minimum amount of methods, abstract class would guarantee that. I also suppose that if you wanted certain method to not only be implemented in the inheriting classes but also to be different on each inheriting class, abstract class would guarantee that as well.
PatrykLevel 18 , Krakow
19 April 2019, 09:58
"(...)made for convenience. hat's how abstraction works in OOP: we mov(...)" Should be That's.