Apps development for Android step by step

Lesson 0.11: Java basics – more about classes (inheritance and constructors)

You should be already aware what is an object and why we use classes, methods and variables (and how to add a new class to your project). Otherwise read previous lesson. To more comfortable jump into Adroid Java developing we need a bit more information about them. Let’s start with class inheritance and class constructors).

Inheritance – superclasses and subclasses

We have already created a few classes in previous lesson. Every class could have its children – other classes based on it. This is called inheritance. For instance we have a template for a book object (class Book) and then we could create template for a textbook based on it (class TextBook). This subclass takes everything from the superclass, but could modify some methods (we say in Java override them) and add some extra variables and methods.

So we could add a variable textbookSubject and method copiesLeftAtStore(). By the way we could still call “original” method using super.metod_name. There was such a situation in onCreate() method discussed in previous lesson. To say that one class is based on another class we use a keyword extends.

Class inheritance enables creating new classes on basis of others (Photo credit: danzel08/SXC)

Class inheritance enables creating new classes on basis of others (Photo credit: danzel08/SXC)

Here is a sample code:

From line 1 to 10 we have a description of Book class. In line 12 we declare a new class that extends Book class, so it takes all its methods and variables. Later we’re adding some extra variable and method not available in superclass. Finally we override a method from superclass, so it would act differently. Mind that one of methods has boolean type so it has to return boolean value (true or false). The other one is void so it doesn’t return any value.

One class could have various different subclasses and any of subclasses could has its own subclasses. Inheritance is very useful – we don’t need to rewrite a full code if we need a class similar to one we already created.

By the way we could create object instances based on classes of every level. In our example we could use object Book as well as object TextBook.  If we would like to disable creation of instances of superclass we could define such a class as abstract one. In this case line 1 should look like this:

Constructor – what is needed to create an instance of class

Now we focus on a class constructor.  A constructor looks similar to a method, but it has one specific goal – it helps to create an instance of a class. Usually it defines what data are needed to create an instance and then sets values of some class variables. Constructor has always a name of the class (this is how a class knows what is its constructor).

We will add a constructor to the class Book. Let’s decide that every Book instance has to have a title.

So our constructor would look like this:

The whole class code would look like this:

So now if you would like to establish a new book object you have to specify its title, otherwise you will see an error. We often use constructor to set received data to specific class variables. So the title given at creating new instance of Book should become a title of that class instance. To refer to variables in this class we use keyword… this.

The constructor could look like this:

We admit it could be quite confusing. This is because one title variable belongs to class and another with the same name to constructor. To make it more clear we could change name of variable in constructor.

So when we want to create an instance of object Book we have to send a title to it. This title is saved into newTitle variable. Then a class variable title receives the value of newTitle. So finally we set the title send via constructor as an instance title.

In the second example we don’t need to specify this.title = newTitle. It would be enough to write title = newTitle. The problem is only when we have the same variables names – we will explain it in the next lesson.

Many constructors – flexible instance creation

Every class has a constructor. If we don’t specify it, it’s just empty. What is more important a class could have a few constructors.

The second constructor in our example could look like this:

When we create an instance of class we could use one of constructors. This is a flexible solution – for example we could create an instance with full set of data or just main ones.

Creating a new object instance – how to use classes

Now very important thing: how to create/invoke an instance of class/object? We have to specify class name, instance name and call one of constructors.

For example:


We could read it as: newBook is an instance of Book class and depending on constructor it would have a specific title or a title and number of pages.

Summary: One class could be used for creating subclasses (subclass extends superclass). To invoke an instance of an object we call a constructor and it determines what parameters (arguments) would be used to create an instance. There are many additional options for both inheritance and constructors, but as a beginner it would be enough to understand how inheritance works, what for are constructors and how to create an object instance. In the next lesson we will look more deeply into methods and variables.