Tutorial by Examples

An abstract class is a class marked with the abstract keyword. It, contrary to non-abstract class, may contain abstract - implementation-less - methods. It is, however, valid to create an abstract class without abstract methods. An abstract class cannot be instantiated. It can be sub-classed (exten...
Static method can be inherited similar to normal methods, however unlike normal methods it is impossible to create "abstract" methods in order to force static method overriding. Writing a method with the same signature as a static method in a super class appears to be a form of overriding,...
Final classes When used in a class declaration, the final modifier prevents other classes from being declared that extend the class. A final class is a "leaf" class in the inheritance class hierarchy. // This declares a final class final class MyFinalClass { /* some code */ } ...
Substitutability is a principle in object-oriented programming introduced by Barbara Liskov in a 1987 conference keynote stating that, if class B is a subclass of class A, then wherever A is expected, B can be used instead: class A {...} class B extends A {...} public void method(A obj) {...} ...
With the use of the extends keyword among classes, all the properties of the superclass (also known as the Parent Class or Base Class) are present in the subclass (also known as the Child Class or Derived Class) public class BaseClass { public void baseMethod(){ System.out.println(&...
In Java, parent and child class both can have static methods with the same name. But in such cases implementation of static method in child is hiding parent class' implementation, it's not method overriding. For example: class StaticMethodTest { // static method and inheritance public stati...
Variables are SHADOWED and methods are OVERRIDDEN. Which variable will be used depends on the class that the variable is declared of. Which method will be used depends on the actual class of the object that is referenced by the variable. class Car { public int gearRatio = 8; public St...
Casting an instance of a base class to a subclass as in : b = (B) a; is called narrowing (as you are trying to narrow the base class object to a more specific class object) and needs an explicit type-cast. Casting an instance of a subclass to a base class as in: A a = b; is called widening and does...
The idea behind programming to an interface is to base the code primarily on interfaces and only use concrete classes at the time of instantiation. In this context, good code dealing with e.g. Java collections will look something like this (not that the method itself is of any use at all, just illus...
When to use abstract classes: To implement the same or different behaviour among multiple related objects When to use interfaces: to implement a contract by multiple unrelated objects Abstract classes create "is a" relations while interfaces provide "has a" capability. This c...
Overriding in Inheritance is used when you use a already defined method from a super class in a sub class, but in a different way than how the method was originally designed in the super class. Overriding allows the user to reuse code by using existing material and modifying it to suit the user's ne...

Page 1 of 1