Head First Java Chapter 08 summary

Udesh Kavinda
3 min readNov 17, 2021

--

What Is Abstraction?

Abstraction means filtering or taking the essential or relevant and ignoring the irrelevance. In Object Oriented analysis, design and programming, abstraction means model (and subsequently code) only minimum essential things.

In Java, we can achieve Data Abstraction using Abstract classes and interfaces.
Interfaces allow 100% abstraction (complete abstraction). Interfaces allow you to abstract the implementation completely.

Abstract classes allow 0 to 100% abstraction (partial to complete abstraction) because abstract classes can contain concrete methods that have the implementation which results in a partial abstraction.

Why do we need Abstract Classes in Java?

If we can’t create an object from abstract classes and neither can’t use them, then what is the need for abstract classes?

To answer this question, let’s take a situation where we want to create a class that just declares the general form or structure or guidelines of a particular idea, without giving a complete implementation of every method.

And, we want that this generalized form or structure of the class can be used by all of its child classes, and the child classes will impose these guidelines by fulfilling all the implementation details according to the need.

There is no need for implementing the method in the parent class thus, we can declare these methods as abstract in the parent class. That is, we will not provide any method body or implementation of these abstract methods.

Making these methods as abstract will enforce all the derived classes to implement these abstract methods otherwise, there will be a compilation error in your code.

Why can’t we create an object of an Abstract Class?

You might be wondering why can’t we instantiate an abstract class or create an object from it? Suppose, if Java allows you to create an object of the abstract class and use this object.

Now, if someone calls the abstract method through the object of the abstract class then what would happen? There would not be any actual implementation of the called method.

Also, an abstract class is like a guideline or a template that has to be extended by its child classes for the implementation.

BULLET POINTS

  • When you don’t want a class to be instantiated mark the class with the abstract keywords.
  • An abstract class can have both abstract and non-abstract methods.
  • If a class has even one abstract method, the class must be marked abstract.
  • An Abstract method has no body, and the declaration ends with a semi-colon.
  • All abstract method must be implemented in the first concrete subclass in the inheritance tree.
  • Every class in java is either a direct or indirect subclass of class Object.
  • Methods can be declared with Object arguments and/or return types.
  • You can call methods on an object only if the methods are in the class used as a reference variable type, regardless
    of the actual object type.
  • A reference variable of type Object can’t be assigned to any other reference type without a cast, but at runtime
    the cast will fail if the object on the heap is NOT a type compatible with the cast.
  • All objects come out of the ArrayList<object> as type object.
  • Multiple inheritance is not allowed in java because of the problems associated with the ‘Deadly Diamond of Death’.
  • An interface is like a 100% pure abstract class. It defines only abstract methods.
  • Create an interface using the interface keyword instead of the word class.
  • Implement an interface using the key word implements.
  • Your class can implement multiple interfaces.
  • A class that implements an interface must implement all the methods of the interface, since all interface methods
    are implicitly public and abstract
    .
  • To invoke the superclass version of a method from a subclass that’s overridden the method use the super keywords.

--

--