These notes are from a good explanation taken from Jason Wertz videos on youtube
Abstract class
A class that represents a generalisation but is ONLY intended to be extended NOT instantiated.
A note on UML above: italics means ‘abstract’, so Mammal is an ‘abstract class’ and contains the abstract method ‘eat()’
As can be seen above, the abstract method eat() in the abstract class has no definition, this definition is deferred and must be contained within the subclass that inherits (extends) it’s properties (class Human).
We cannot instantiate (cannot make objects from) the Mammal class, but we can instantiate the Human class, as it’s not an abstract class. In the Human class we will HAVE to implement/use the eat() method from the abstract class.
Code example for abstract classes
package example5; public class Example5 { public static void main(String[] args) { // The following line WON'T WORK as we cannot // instatiate an abstract class, we can only access it // via it's subclass that extends it //Mammal bigMammel = new Mammal(); Human james = new Human(); james.getWeight(); james.eat(); } } // ABSTRACT CLASS abstract class Mammal{ private double weight=5; public void getWeight(){ System.out.println(weight); } // abstract method has NO implementation // we defer the implentation for the child class to sort out public abstract void eat(); } // NONE ABSTRACT CLASS that extends the abstract class class Human extends Mammal{ // this EAT method has to be here as it's in the abstract class // this is the superclass - we DEFINE it's definition here public void eat(){ System.out.println("Eating!!"); } }
Output:
5.0
Eating!!
Polymorphism example from the above
public class Example5 { public static void main(String[] args) { // The following line WON'T WORK as we cannot // instatiate an abstract class, we can only access it // via it's subclass that extends it //Mammal bigMammel = new Mammal(); Human james = new Human(); james.getWeight(); james.eat(); ////////// !!!!!!!!!!!!!!!! ////////////////////////////// // note: how a Human can be stored in a mammal variable // // because Human is polymorphic, Human IS-A Mammal // Mammal mammal = new Human(); mammal.getWeight(); mammal.eat(); } } // ABSTRACT CLASS abstract class Mammal{ private double weight=5; public void getWeight(){ System.out.println(weight); } // abstract method has NO implementation // we defer the implentation for the child class to sort out public abstract void eat(); } // NONE ABSTRACT CLASS that extends the abstract class class Human extends Mammal{ // this EAT method has to be here as it's in the abstract class // this is the superclass - we DEFINE it's definition here public void eat(){ System.out.println("Eating!!"); } }
Outputs:
5.0
Eating!!
5.0
Eating!!
How is this different to an INTERFACE?
An interface is a COMPLETELY abstract class that defines a protocol for object interactions. It can only contain abstract methods.
In the above example;
Silver implements Valuable
Silver HAS TO then contain the isValuable() method.
Summary
Interface code example:
public class Example5 { public static void main(String[] args) { Silver myBlockOfSilver = new Silver(); System.out.println(myBlockOfSilver.isValuable()); } } // INTERFACE // can ONLY contain abstract methods interface Valuable{ public boolean isValuable(); } class Silver implements Valuable{ public boolean isValuable(){ return true; } }
Outputs:
true