Chapter 6 |
Abstract classes
An abstract class is a class that is declared abstract
. Abstract classes cannot be instantiated (we cannot create objects from an abstract class), but they can be subclassed. The purpose of abstract classes is to be a parent to several related classes.
An example of abstract classes importance can be seen in the cases where we have a broad concept (e.g. Automobile) but actual objects must be of specific types (e.g. electric car, diesel car and petrol car).
Abstract classes may or may not include abstract
methods but one is for sure: if a class includes abstract
methods, then the class itself must be declared abstract
).
An abstract
class with abstract methods is defined as follows:
public abstract class ClassName { modifier dataType instanceFieldName; //abstract instance field cannot be initialized modifier abstract returnType abstractMethodName(listOfParameters); //abstract return-type methods modifier abstract void abstractMethodName(listOfParameters); //abstract void methods //non-abstract methods }
Consider an example.
public abstract class Dog { private String name; public Dog(String name) { this.name = name; } public abstract void barking(); }
Different cultures "hear" dog's barks differently (more about barks can be found in Wiki: https://en.wikipedia.org/wiki/Bark_(sound)):
public class EstonianDog extends Dog { public EstonianDog(String name) { super(name); } @Override public void barking() { System.out.println("Auh-auh!"); } }
public class RussianDog extends Dog { public RussianDog(String name) { super(name); } @Override public void barking() { System.out.println("Gav-gav!"); } }
public class KoreanDog extends Dog { public KoreanDog(String name) { super(name); } @Override public void barking() { System.out.println("Meong-meong!"); } }
Using polymorphism, it is possible to create an array of the abstract class data type whose elements are of the subclass data type:
public class TestDogs { public static void main(String[] args) { Dog d1 = new RussianDog("Sharik"); Dog d2 = new KoreanDog(""); Dog d3 = new EstonianDog("Muri"); Dog[] dogs = {d1, d2, d3}; for (Dog d : dogs){ d.barking(); } } }
import java.io.File; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.nio.file.Files; class Person { private String firstName; private String lastName; Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } } abstract class GetPersonFromFile { protected abstract Person lineIntoPerson(String line); public List<Person> readPerson(File input) throws Exception { // Files.readAllLines reads from the file all lines into a list List<String> text = Files.readAllLines(input.toPath(), StandardCharsets.UTF_8); List<Person> people = new ArrayList<>(); for (String line : text) { people.add(lineIntoPerson(line)); } return people; } } class FirstAndLastName extends GetPersonFromFile { protected Person lineIntoPerson(String line) { String[] partOfName = line.split(" "); // Malle Mallikas return new Person(partOfName[0], partOfName[1]); } } class LastAndFirstName extends GetPersonFromFile { protected Person lineIntoPerson(String line) { String[] partOfName = line.split(", "); // Mallikas, Malle return new Person(partOfName[1], partOfName[0]); } } class TestEmployee { public static void main(String[] args) throws Exception { FirstAndLastName flname = new FirstAndLastName(); List<Person> nimed = flname.readPerson(new File("names.txt")); GetPersonFromFile secondReader = new LastAndFirstName(); List<Person> officialNames = secondReader.readPerson(new File("officialnames.txt")); // GetPersonFromFile reader = new GetPersonFromFile(); // error! } }
Even if we cannot create an object of GetPersonFromFile
class, we can use its methods.
Why do we need abstract classes?
Abstract classes are used to organize programs. Consider the exaple program about employees. The program could have been written without an abstract class. But a real-world application might have hundreds of classes. Grouping classes together is important in keeping a program organized and understandable.
Remember:
- When we define abstract classes or abstract methods, we use the keyword
abstract
. - If a class is declared
abstract
, no objects of that type can be created. - Access modifiers such as public can be placed before abstract (not compulsory).
- An abstract class can contain both normal and abstract methods.
- If a class has at least one abstract method, then the class must be declared abstract.
- To use an abstract class, extend it from another class (
extends
keyword), provide implementations to all the abstract methods in it.
Java abstract class java.net.URLConnection
An example of abstract classes is java.net.URLConnection
which allows to send or receive data from a website. The only abstract method in this class is connect
which determines how to open a connection. The method is implemented in several subclasses like HttpURLConnection
, HttpsURLConnection
, FtpURLConnection
etc.
Useful link: Check API and have a look at methods of URLConnection abstract class.
Chapter 6 |