Improve comments on abstract classes

This commit is contained in:
Andrew Gallasch 2017-10-28 16:09:07 +10:30 committed by GitHub
parent d5ea29ce9f
commit 32090679c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -703,15 +703,21 @@ public class ExampleClass extends ExampleClassParent implements InterfaceOne,
// // Method declarations // // Method declarations
// } // }
// Marking a class as abstract means that it contains at least one abstract // Abstract Classes cannot be instantiated.
// method that must be defined in a child class. Similar to interfaces, abstract // Abstract classes may define abstract methods.
// classes cannot be instantiated, but instead must be extended and the abstract // Abstract methods have no body and are marked abstract
// methods defined. Different from interfaces, abstract classes can contain a // Non-abstract child classes must @Override all abstract methods
// mixture of concrete and abstract methods. Methods in an interface cannot // from their super-classes.
// have a body, unless the method is static, and variables are final by default, // Abstract classes can be useful when combining repetitive logic
// unlike an abstract class. Also abstract classes CAN have the "main" method. // with customised behavior, but as Abstract classes require
// inheritance, they violate "Composition over inheritance"
// so consider other approaches using composition.
// https://en.wikipedia.org/wiki/Composition_over_inheritance
public abstract class Animal public abstract class Animal
{ {
private int age;
public abstract void makeSound(); public abstract void makeSound();
// Method can have a body // Method can have a body
@ -722,17 +728,12 @@ public abstract class Animal
age = 30; age = 30;
} }
// No need to initialize, however in an interface
// a variable is implicitly final and hence has
// to be initialized.
private int age;
public void printAge() public void printAge()
{ {
System.out.println(age); System.out.println(age);
} }
// Abstract classes can have main function. // Abstract classes can have main method.
public static void main(String[] args) public static void main(String[] args)
{ {
System.out.println("I am abstract"); System.out.println("I am abstract");