0% found this document useful (0 votes)
20 views

Java Abstract Class What Is It Good For

This document discusses Java abstract classes and compares them to interfaces. It provides an example of an abstract Battery class with implemented and abstract methods, and shows how a concrete ComputerBattery class extends it and implements the abstract methods. The document also shows how an abstract class can implement an interface, and discusses that an abstract class with a main method cannot be run directly.

Uploaded by

NikolaMilosevic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java Abstract Class What Is It Good For

This document discusses Java abstract classes and compares them to interfaces. It provides an example of an abstract Battery class with implemented and abstract methods, and shows how a concrete ComputerBattery class extends it and implements the abstract methods. The document also shows how an abstract class can implement an interface, and discusses that an abstract class with a main method cannot be run directly.

Uploaded by

NikolaMilosevic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Sign in Get started

— T O QUIT OR NOT T O QUIT ? — T OP 50 S T ORIES WRIT ERS HERE ARE YOU CURIOUS ?

You have 2 free stories left this month. Sign up and get an extra one for free.

Java Abstract Class What Is It Good


For?
Tom Henricksen Follow
Oct 15, 2019 · 3 min read

The Java abstract class eludes many Java developers. Let’s learn what it
does for us and how to use it.

Abstract art: a product of the untalented sold by the unprincipled to the


utterly bewildered. Al Capp

I am guessing you have heard of the malady called ADD or Attention


Deficit Disorder. On a recent trip to Paris, my son and I discovered we are
suffering from another malady with similar initials. Art Deficit Disorder.

We can look at paintings and sculptures and find it uninspiring. Where


my daughter enjoyed the d’Orsay we looked for the food court. Where
we enjoyed some espresso and fresh-squeezed orange juice.

Java Abstract Class


Java has abstract classes that are incomplete. They cannot be
implemented like a regular class. Abstract classes must be subclassed to
be used. In these classes, we can declare abstract methods. Abstract
classes are similar to interfaces in Java. Let’s dive deeper into this
comparison.

Compare
Like interfaces, abstract classes cannot be instantiated. Where the
interface just will contain method signatures an abstract class can
contain a method body. Abstract classes can declare fields that are not
static and final.

Where interfaces have all fields automatically become public, static, and
final. We can implement as many interfaces as we want. Abstract classes
are like regular classes and we can only extend one.

The Java tutorial has some good guidance when to use abstract classes.
When we “want to share code among several closely related classes” or Top highlight

“expect that classes that extend your abstract class have many common
methods or fields”. Interfaces should be used when “expect that
unrelated classes would implement your interface” or “want to specify
the behavior of a particular data type”.

Java Abstract Class Example


Like all good coders let’s get our hands dirty with some code. First, we
can look at an example abstract class to get us started.

1 package com.myitcareercoach;
2
3 public abstract class Battery {
4 int volt;
5 int amps;
6 void charge(int chargingTime) {
7 // shared code
8 }
9
10 abstract boolean fullyCharged();
11
12 abstract boolean isTooHot();
13
14 }

Battery.java hosted with ❤ by GitHub view raw

This Battery abstract class has one implemented method and two
abstract methods. There are also two fields defined as well.

1 package com.myitcareercoach;
2
3 public class ComputerBattery extends Battery {
4
5 @Override
6 boolean fullyCharged() {
7 // TODO Add some code here!
8 return false;
9 }
10
11 @Override
12 boolean isTooHot() {
13 // TODO Add some code here!
14 return false;
15 }
16
17 }

ComputerBattery.java hosted with ❤ by GitHub view raw

ComputerBattery is a concrete Java class. Therefore, it needs to


implement both of the abstract methods that Battery defined.

Abstract and Interface?


An abstract class can even implement an interface. This seems like
mixing spaghetti and mashed potatoes but, hey it is legal!

1 public interface Student {


2
3 public void setSchedule();
4
5 public void registerForClass(String className);
6
7 }

Student.java hosted with ❤ by GitHub view raw

Let’s take our Student interface and mix it in an abstract class.

1 public abstract class ProbationaryStudent implements Student {


2
3 @Override
4 public void setSchedule() {
5 // implemented method
6 }
7
8 // not going to override registerForClass() method
9
10 }

ProbationaryStudent.java hosted with ❤ by GitHub view raw

In our ProbationaryStudent class, we don’t implement all the methods


defined in the Student interface. This is possible since the class is marked
as abstract.

Main?
Would you think if you had the main method in an abstract class that it
would run?

1 public abstract class DoesItRun {


2
3 public static void main(String[] args) {
4 System.out.println("Does it run?");
5 }
6
7 }

DoesItRun.java hosted with ❤ by GitHub view raw

I didn’t think it would either but in fact, it does run. I suggest you try it out
for yourself. As you can see abstract classes have their place in Java.
Similar to interfaces but used in a different way.

Where have you used Java interfaces?

Check out more great content and subscribe at MyITCareerCoach.com

Sign up for Top Stories


By T he Startup

A newsletter that delivers The Startup's most popular stories to your inbox once a
month. Take a look

Your email Get this newsletter

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more
information about our privacy practices.

Programming Java Learning To Code Software Development

195 claps 2 responses

WRIT T EN BY

Tom Henricksen Follow

I am a developer, agilist, and DevOps practitioner who enjoys


learning and writing about these topics. Find me at
myitcareercoach.com

The Startup Follow

Medium's largest active publication, followed by +687K


people. Follow to join our community.

More From Medium

How Convenient A Simple Introduction How to not get caught Side projects: API
Graeco-Latin Squares to Git. while web scraping? Authentication
Are Rajat M in T he Startup Efe Can Kursun in Data Driven Overview
Juan Manuel Dato Investor ACGo in T he Startup

How to debug layout What Makes a Great A Beginner’s Guide to Components and
issues with the Flutter Developer? GraphQL Composition with
Inspector Dhananjay Trivedi in Better Leonardo Maldonado in MVVM + Data Binding
Katie Lee in Flutter Programming freeCodeCamp.org Chris Michael in Kin + Carta
Created

Discover Medium Make Medium yours Become a member


Welcome to a place where words matter. On Medium, Follow all the topics you care about, and we’ll deliver the Get unlimited access to the best stories on Medium — and
smart voices and original ideas take center stage - with no best stories for you to your homepage and inbox. Explore support writers while you’re at it. Just $5/month. Upgrade
ads in sight. Watch

About Help Legal

You might also like