So let's get started by learning about abstract classes and abstract methods and abstract class as the 2
name suggests is a class that is too abstract which means that it is too generic to be instantiated 3
. 4
In other words it is not concrete enough to be instantiated on includes one or more methods for which 5
it is not possible to provide any implementation even though it is not intangible. 6
The class still plays an important role but defining a common protocol for all its subprocess so it 7
defines a contract and hence can be used polymorphically on an abstract that is defined by simply inserting 8
the key word abstract in the class declaration that the compiler ensures that the class cannot be instantiated 9
like an abstract last and abstract method also involves inserting the key word abstract in the method 10
declaration an abstract method will not have any body that is normally braces and it simply ends with 11
that semicolon and its implementation will be provided by subprocess. 12
That is a MUST be overridden. 13
It also means that an abstract muttered cannot be declared as nothing. 14
And that's because we know that static methods cannot be overridden. 15
So an upstart class has no use unless it is extended. 16
It may exist though defined only static members but that cannot be the only purpose of its existence 17
and it would also be very confusing. 18
So with an abstract class it is the instances of its subclasses which are doing the real work at runtime 19
. 20
Now let's look at some specifics. 21
We cannot have an abstract method in a non abstract us. 22
So if there is even one abstract method then the class must be abstract to us. 23
In addition to abstract an abstract class may also contain one or more concrete methods that is methods 24
with implementations. 25
Not that an abstract class need not have any abstract methods but that is not typically done. 26
It would be very confusing to have an abstract class without any abstract methods. 27
So we know that implementation for abstract methods then be provided in subclasses. 28
However a subclass itself can also be declared as abstract. 29
That is it would be an abstract supperless and in this case that subclass doesn't have to override an 30
inherited abstract method. 31
It doesn't even have to really clear that inherited abstract matter. 32
It means that some other subclass don't. 33
The inheritance but would be expected to provide an implementation for that method. 34
It can do it all right. 35
Any inherited method. 36
Whether or not the inherited method is abstract like any abstract class it can also define new abstract 37
and concrete methods. 38
No if a subclass is non-abstract which means that it is a concrete subclass then it must always right 39
. 40
All of the unimplemented abstract methods. 41
All of the unimplemented abstract methods so some of the abstract methods might have been implemented 42
in one or more super classes. 43
And the inheritance tree only them it does that or not implement it must be implemented in this class 44
. 45
Otherwise we do get compilation errors and that's not really a short demo of abstract process or an 46
abstract methods for this item or a new folder. 47
Abstract has been added in the directory structure. 48
Com semantics clear up three new classes have been added here. 49
One is abstract superclass abstract subclass an concrete subclass. 50
So let's just go ahead and look at these classes. 51
So this is abstract superclass. 52
This is the most class and it is an abstract class so it is declared with the keyword abstract here 53
so it is an abstract class. 54
I need to close to abstract methods so both the methods have the keyword abstract. 55
So next let's look at the subclass of this which is an abstract subclass So it extends abstract superclass 56
and it is also abstract and it is overwriting only one method which is a best one matter. 57
So if you're going to abstract superclass you have test too so it's overloading only one method with 58
just one and it is simply printing out this text just one and you can ignore this notation. 59
It just implies that it is already one of the superclass methods but Farnam you can ignore it because 60
it's covered in a separate chapter and it is also declaring its own abstract method and your method 61
called Destiny Dometic pesto which is declared here is not is not overridden in this in this particular 62
class. 63
Now let's look at concrete of class which is a subclass of abstract subclass. 64
OK so this is a concrete subclass so you don't see the keyword abstract here and there are two methods 65
to be implemented from the inheritance hierarchy which is a test to test 3. 66
So test to was declared as an abstract method and the topmost class on pastry was declared as an abstract 67
method an abstract subclass. 68
So this particular class has to provide implementation implementations for all the unimplemented abstract 69
methods so it provides the implementations here as do industry and here is the main method. 70
Here we are just instantiating this class concrete subclass and we are invoking all the three methods 71
. 72
So let's just go ahead and compile and run this. 73
So I'm in the abstract folder. 74
Just compile this and let me just run it. 75
Did you go at Brent's test run test do and best three. 76
Now let's just also uncommon this class. 77
It's just creating an instance of an abstract class which should give a compiler error because abstract 78
classes cannot be instantiated. 79
So let me just try to recompile this and as you can see it's is abstract superclasses abstract cannot 80
be instantiated So that's a compilation of that and that we are getting. 81
So that's the demo for abstract classes on.