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."

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.

}

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 (18)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Hoist
Level 35 , San Diego, United States
27 October 2022, 23:56
> all this discussion here is great -- and impossible to find this concise on the Web
moseka
Level 23 , Nairobi, Kenya
10 June 2020, 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 A
Level 18 , Rubery, United Kingdom
28 June 2020, 14:10
Very nice explanation!
Alaskian7134
Level 22 , Iasi, Romania
23 May 2021, 12:41
well said. i want to add this: the abstract method in the abstract parent class should probably be seen like some kind of a contract. something like "We made the Car class, and is your job to design SportCar, we don't care how you design 'move()', but is mandatory to do it, you can't run away from it"
Lucas Hoage
Level 14 , Savannah, United States
9 June 2020, 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 deng
Level 22 , Paris, France
4 July 2020, 04:30
the difference between override and overload , we should make clear
Chang You
Level 47 , Santa Rosa, United States
24 December 2020, 17:23
Yes, should be override
Syed Tayyab ul Mazhar
Level 12 , Karachi, China
15 May 2020, 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.
Ashish RajAnand
Level 13 , Bhilai , India
12 May 2020, 09:51
I don't understand
Jonaskinny Java Developer at Sandmedia
25 February 2022, 00:43
Company is an abstract concept. You dont want a job at Company. You want a job at XYZ company. You can't show me the address for Company, but you can define common attributes and behaviors of a typical Company, like address. Address could be an abstract method. Indian companies will display address differently than French companies, but they each need to display their address, so make address abstract in the Company class, subclass to FrenchCompany and IndianCompany and implement the address method specific to each, then put one object of each type in a list, loop it and for each item call .address()
Serghei
Level 26 , Moldova, Republic of
1 April 2020, 15:01
"Everybody has to go through this."
Robert Constantinescu
Level 25 , Bucharest, Romania
2 November 2019, 09:16
you've done it until here! Great Job & keep going like Musk did :) !
Anonymous #10782038 Java Developer at Microsoft
29 March, 23:29
Ah back in 2019 when people still thought Musk was smart.
Renat Mukhametshin
Level 16 , Pervouralsk, Russain Federation
30 August 2019, 11:02
yeah! finally i find out something about this topic and this is clarifyed in some degree
Shashank Mishra
Level 18 , Panaji, India
29 August 2019, 22:19
here enters one above all ;)
Ewerton Backend Developer
29 June 2019, 18:14
"I still don't understand why we would want to complicate our lives this way."
Fadi Alsaidi
Level 34 , Carrollton, TX, USA
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.