Abstract Classes, Interface, Annotations Lecture 5
Abstract Classes, Interface, Annotations Lecture 5
Syntax:
Abstract type name(parameter-list); //written in parent class
These methods are sometimes referred to as subclass's responsibility because they have no
implementation specified in the superclass.
Thus, a subclass must override them—it cannot simply use the version defined in the
superclass.
Any class that contains one or more abstract methods must also be declared abstract.
# there can be no objects of an abstract class.
# You cannot declare abstract constructors, or abstract static methods.
# You can declare static methods in abstract class.
Because there can be no objects for abstract class. If they had allowed to call abstract static
methods, it would that mean we are calling an empty method (abstract) through classname
because it is static.
Any subclass of an abstract class must either implement all of the abstract methods in the
superclass, or be declared abstract itself.
Abstract classes can include as much implementation as they see fit i.e. there can be concrete
methods (methods with body) in abstract class.
Although abstract classes cannot be used to instantiate objects, they can be used to create
object references, because Java’s approach to run-time polymorphism is implemented through
the use of superclass references.
A public constructor on an abstract class doesn't make any sense because you can't instantiate
an abstract class directly (can only instantiate through a derived type that itself is not marked as
abstract)
Check: https://stackoverflow.com/questions/260666/can-an-abstract-class-have-a-constructor
What if we put final before the abstract class what will happen? Can we allowed to
create final abstract class?
No, it prevent the class from being inherited. But you can create final variables.
Do abstract classes support multiple inheritance? (you can’t extends the two parent class to
one child class)
No, because the problem is if two normal method have same name in abstract class or
any other class then, the child class is confused which one to call, to overcome this we have
interface.
Java Interfaces
It is created with .java extension.
Interfaces contain abstract classes and abstract classes contain abstract function/method or
normal method. //abstract functions are no body of a function is allowed.
The variables are static and final by default in interfaces. WHY? Because you can’t create object of
interfaces so that you have to have static before variables cuz you cant make an object to call that
variable/property.
Why the variables are final in interface? Cuz you don’t have constructors in an interface then how to
initialize the variable use final cux final variable needs to be initialized.
Implementation:
Abstract class provide the implementation of interface.
Interface can’t provide the implementation of abstract class.
Type of methods:
Final Variables:
Type of variables:
Abstract class can have final, non-final, static and non-static variables.
Interface has only static and final variables.
Implementation:
Abstract class can provide the implementation of interface. Using the keyword “Interface”
you can fully abstract a class interface from its implementation. Means you can specify
that what the class must do but, not how it does it (means no body of a method is
declared).
How it does it means where to define method?
It will be specify by the child classes by overriding the abstract method in the interface.
Interface can’t provide the implementation of abstract class.
Inheritance vs Abstraction:
A Java interface can be implemented using keyword “implements”. // you can implement multiple
interfaces together. Means an interface in java can extends multiple interfaces in java.
and abstract class can be extended using keyword “extends”. Here you can only extend single class.
Multiple implementation:
Interface Example:
Two classes that are unrelated to each other can also implements as in a same interface.
Extending interface:
Keyword “implements” use for classes to interfaces.
Keyword “extends” use for class to class and interface to interface.
Here interface A contain fun () and
interface B contain greet().
Annotations:
Annotations itself an interface.