Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

Abstract Classes, Interface, Annotations Lecture 5

Abstract classes allow for generalized forms that will be shared by subclasses, leaving implementation details to subclasses. Abstract classes can contain abstract and non-abstract methods, while interfaces can only contain abstract methods. Both can be extended or implemented, but interfaces extend other interfaces while abstract classes extend other classes.

Uploaded by

riazhumaima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Abstract Classes, Interface, Annotations Lecture 5

Abstract classes allow for generalized forms that will be shared by subclasses, leaving implementation details to subclasses. Abstract classes can contain abstract and non-abstract methods, while interfaces can only contain abstract methods. Both can be extended or implemented, but interfaces extend other interfaces while abstract classes extend other classes.

Uploaded by

riazhumaima
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lecture # 05

Abstract classes, Interface, Annotations


Abstract Classes
Sometimes you will want to create a superclass that only defines a generalized form that will
be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such a class
determines the nature of the methods that the subclasses must implement.
Abstract Methods:
You may have methods that must be overridden by the subclass in order for the subclass to
have any meaning.
In this case, you want some way to ensure that a subclass does, indeed, override all necessary
methods. Java’s solution to this problem is the abstract method.
You can require that certain methods be overridden by subclasses by specifying the abstract
type modifier.

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

 Do we create abstract static method?


No, because static methods can be not overridden but abstract method should be overridden.

 Do we create static method in abstract classes?


Yes, we can create static, normal methods in abstract class and also variables but does not
create an object of abstract class. But you can use like this

 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.

By default functions/methods are public and abstract in Interfaces.

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.

Variables in abstract class may be both final and non-final.

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.

Abstract class vs. Interface:

Type of methods:

 Interface can have only abstract methods.


 Abstract class can have abstract and non-abstract methods. From Java 8, it can have default and
static methods also.

Final Variables:

 Variables declared in a Java interface are by default final.


 An abstract class may contain non-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:

 An interface can extend another Java interface only,


 An abstract class can extend another Java class and implement multiple Java interfaces.

Accessibility of Data Members:

 Members of a Java interface are public by default.


 A Java abstract class can have class members like private, protected, etc.

Interface Example:
 Two classes that are unrelated to each other can also implements as in a same interface.

Separate classes in same interface:


Use separate classes for the 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().

If we extends interface A to B using


extends keyword then b has all
function that a has.

So in main you have to override both


interface methods/functions.

Annotations:
Annotations itself an interface.

Important point regarding static method:


We know that static methods does not inherited.
Why?
“Inheritance means you have to override the method. Overriding means it depends on
objects because it is runtime polymorphism and static method cannot depends on the object
that is why they cannot be inherited.”
If you create static method in an interface then it should have a body in that interface. It can
only use for that interface.
Static method in interface called by an interface name.
The access modifier for the @overriden methods should be same or better. it should not be
private but may be public or protected.
Nested Interface:

You might also like