A class can be inherited.
The original class is called the parent/base/superclass.
The newly created class is called the child/derived/subclass.
Final classes can not be inherited.
The absolute superclass is the Object.
The inheritance keyword is extends.
EXAMPLE FOR INHERITANCE:
The object of a subclass will be an instance of the object of its superclass.
EXAMPLE FOR INHERITANCE (RELATION TO SUBCLASS):
A class has fields and methods with access modifiers attached to them.
The subclass will inherit all fields and methods with their access modifiers from the superclass except the constructors which should be declared independently, and final fields and methods.
The superclass can be accessed by the "super" keyword.
Using the "this" keyword from the superclass accesses the superclass, from the subclass accesses the subclass.
FIGURE OF INHERITANCE TYPES:
Java does not support the Multiple inheritance type but through interfaces it can be emulated.
Hybrid: combination of other types.
In the subclass all inherited fields can be accessed.
As extension, in the subclass new fields can be declared as well.
A field of the superclass can be overridden if in the subclass a new variable is declared with the same name.
The overridden field can have different value than the original one has.
Final and static fields can not be inherited, therefore be overridden.
Superclass can not be accessed within a static environment like the main() method.
EXAMPLE FOR OVERRIDDEN FIELDS:
In the subclass all inherited methods can be accessed.
As extension, in the subclass new methods can be declared as well.
A method of the superclass can be overridden if in the subclass a new method is declared with the same name and method signature.
The overridden method can have different code block and return type than the original one has.
In case the new method has different method signature than the original one has, instead of overriding, overloading will take place, both methods will be accessible in the subclass.
Because method overload is possible as well, the compiler will not know whether the aim was to override.
In order to make it sure that overriding is the aim, in the subclass, right before the overridden method head, the @Override annotation should be placed. In this case, if the method override was not successful in the code, the compiler will give a warning.
Final methods can be inherited. Final and static methods can not be overridden.
EXAMPLE FOR OVERRIDDEN AND OVERLOADED METHODS:
In case all the variants of possible parameters are not known or the number of possibilities are high, use vargargs instead.
The subclass will inherit the access modifiers as well.
In case of override, new modifier can be declared, with the restriction that the new modifier should allow wider access.
As an access modifier, the abstract keyword can be used before the classname. That is, such class has only superclass position in the structure.
Can not create object from it.
Can consist fields, normal methods and abstract methods.
abstract class A {...}Can have constructors. Its parameterized constructors can be called from subclasses but only as the 1st line of their constructor.
super(...);In an abstract class, next to normal methods, abstract methods can be declared.
In such method the code block is empty.
Therefore, all of these methods must be overridden as normal, but can be implemented differently in all its subclasses (polymorphism).
Abstract method can be declared only in abstract classes.
EXAMPLE FOR ABSTRACT METHOD:
In case the subclass is an abstract class as well, the subclass will inherit all methods (normal and abstract), and there is no obligation to implement all abstract methods of the super.
A constructor method can not be inherited, in all classes it must be implemented.
In case a class does not have a constructor, will use a default one.
In case the subclass want to use a constructor of the superclass, can use the "super(...)" statement.
The "super()" for the no-argument form of constructor in the superclass can not be accessed from the subclass since it invokes automatically when an instance of the subclass is created.
In case the "super(...)" statement is used in the subclass, this statement must be the first.
EXAMPLE FOR CONSTRUCTORS IN INHERITANCE:
An abstract class with abstract methods only is called interface.
Because its is a type of abstract class, no object can be created from it.
Other classes must implement it.
Keyword for implementation is implements.
More than one interfaces can be implemented by a class.
Interface can be inherited as well, it is called extending. It is allowed to inherit more than one interfaces.
All interface methods are public by default.
All interface fields are public static final by default.
Interfaces can have methods with code blocks. These are called default methods.
Default methods can be overridden on implementation or extending, or left as they are.
Default interface methods can be static. These can be addressed by using interfaceName.methodName(args) expression.
Default interface methods can be private explicitly. These are helper methods for interface methods.
EXAMPLE FOR INTERFACE INHERITANCE:
Use default method in case more classes will use the method with the same code block.
Use normal method in case in classes it must be implemented differently.
Use private method in case that is a helper of the interface.
Use static method in case that method is a helper of the implementer class.
Keep the code of interfaces and classes under approx. 200 lines plus Javadocs.
Interface with only one method.
This method specifies the purpose of the interface.
This kind of interface can be used as parameter of a custom method.
It is called as Single Abstract Method or SAM type in Java7.
Such parameter can be a lambda expression from Java8.
EXAMPLE FOR DECLARATION OF FUNCTIONAL INTERFACE:
EXAMPLE FOR USING FUNCTIONAL INTERFACE AS PASSED ANONYMOUS CLASS:
EXAMPLE FOR USING FUNCTIONAL INTERFACE AS PASSED LAMBDA EXPRESSION:
EXAMPLE FOR USAGE OF FUNCTIONAL INTERFACE AS PASSED LAMBDA EXPRESSION:
The java.util.function package contains 43 functional interfaces.
From among them, the most important 6 are below.
Functional interface which can be used with any kind of data. For this, a virtual type called T or "type parameter" is used as its type.
No primitive types can be used with generics functional interface.
The type structure can be designed once, in its variable references it will be virtually inherited via a wrapper or class type.
EXAMPLE FOR DECLARING GENERICS FUNCTIONAL INTERFACE:
EXAMPLE FOR USING INTERFACE (DECLARED IN PREVIOUS EXAMPLE) AS PASSED LAMBDA EXPRESSION:
By using <T>, any types except primitive types can be accepted. Narrowing is possible by using <T extends typeClass> where typeClass can be e.g. Number for numbers or any custom types so T can be only a subtype of typeClass.
Such T is called bounded type.
EXAMPLE FOR DECLARING GENERICS FUNCTIONAL INTERFACE OF NUMBERS:
Advantages of using generics:
code re-usability
compile-time type checking
OOP basic theory.
An entity can handle different tasks in different situations. E.g. under an identical name which shows the basic task, with different implementations the method processes data in different ways.
Polymorphism can be reached by using
method override (inheritance: extending and implementing classes and interfaces; run-time polymorphism)
method overload (due to different signatures, different code blocks, compile-time polymorphism)
operator overload (same operator provides different actions for different types, e.g. + can mean adding and concatenation; Java does not support user-defined operator overloading)
polymorphic variable (type (class) of variable can change during running; Java does not support it)
OOP basic theory.
Fields and methods which are connected by their common task, are placed into a common class/method. Thanks for that, a class or a method can be independently developed, corrected and tested, parallelly with other encapsulated entities, without having effect on other classes and/or methods.
OOP basic theory:
Encapsulation in method: fields can not be accessed from outside the method.
Encapsulation in class: fields and methods can not be accessed from outside the class.
Encapsulation in package: classes and their fields and methods can not be accessed from outside the package.
OOP basic theory.
Algorithms which can be used on various types e.g. sort() on Collection types.
Due to operating on different types and structures, implementations of such algorithms are different.
Through generics classes, a special inheritance can be achieved as a generics class carrying T as its type can be virtually inherited by its instances carrying specific type.
Advantages of using generics:
code re-usability
compile-time type checking
used with Collections (e.g. ArrayList<String> creates a String-based instance of ArrayList<T>)