Docsity

Understanding Abstract Classes: Templates for Unknown Methods, Slides of Java Programming

Partial preview of the text

Download Understanding Abstract Classes: Templates for Unknown Methods and more Slides Java Programming in PDF only on Docsity!

Abstract classes

Announcements

Kristen's office hours are canceled tomorrow

Abstract classes

Remember classes are templates that we use to create objects We can also use classes as templates for other classes (this is inheritance) Sometimes when making these templates, we don't know what to fill in, but we want every class to have that method. (See: BadCards, BadBirthday, BadHoliday.java)

Abstract classes

It is possible to create templates even when we don't know how some methods are going to work yet To do this we use an abstract class (shown below): To create the abstract class, you simply put the word abstract in your class declaration

Non-private abstract methods

Abstract methods must be inside an abstract class Since children have to override these methods, they cannot be private (non-inherited) Error!

Using abstract classes

It is illegal to try and create an instance of an abstract class, since it is not fully defined You can however, use it as a type to store its children In fact, abstract classes are only really useful as a parent to other classes

A larger example

A good example of where abstract classes make sense is biological classification All animals are broken down into: Kingdoms, Phylum, Class, Order, Family, Genus and Species Higher ranks are more general, lower ranks are more specific

A larger example

A good example of where abstract classes make sense is biological classification All animals are broken down into: Kingdoms, Phylum, Class, Order, Family, Genus and Species Abstract classes make sense, because no real animal exists until the “Species” point (until then it is categorization)