Understanding Inheritance in OOP
Understanding Inheritance in OOP
(SEng2062)
Chapter - 3
Inheritance
1
Inheritance
Inheritance is one of the cornerstones of object-oriented
programming because it allows the creation of hierarchical
classifications. Using inheritance, you can create a general class
that defines traits common to a set of related items. This class
can then be inherited by other, more specific classes, each
adding those things that are unique to it.
Inheritance is the mechanism that permits new class to be
created out of existing classes by extending and refining its
capabilities. The existing classes are called the base classes
/parent classes/ super-classes, and the new classes are called
the derived classes /child classes/ subclasses. The sub-class can
inherit or derive the attributes and methods of the super-
class(es) provided that the super-class allows so. Besides, the
subclass may add its own attributes and methods.
2
Cont…
Inheritance defines an “is-a” relationship. Example from a
class mammal, a number of classes can be derived such as
Human, Cat, Dog, Cow, etc. Humans, Cats, Dogs, and Cows all
have the distinct characteristics of mammals. In addition,
each has its own particular characteristics. It can be said
that a cat “is-a” mammal. With inheritance, you can save
time during program development by basing new classes on
existing proven and debugged high-quality software. This
also increases the likelihood that a system will be
implemented and maintained effectively.
Syntax
public class (subclass-name) extends (existing-class-
name) {
// Changes and additions.
}
3
Cont…
You need to know a few important things about
inheritance:
A derived class automatically takes on all the
behavior and attributes of its base class. Thus, if you
need to create several different classes to describe
types that aren’t identical but have many features in
common, you can create a base class that defines all
the common features. Then, you can create several
derived classes that inherit the common features.
A derived class can add features to the base class it
inherits by defining its own methods and fields. This
is one way a derived class distinguishes itself from its
base class.
4
Cont…
A derived class can also change the behavior
provided by the base class. For example, a base class
may provide that all classes derived from it have a
method named play(), but each class is free to
provide its own implementation of the play() method.
In this case, all classes that extend the base class
provide their own implementation of the play method.
Inheritance is best used to implement is-a-type-of
relationships. For example: Solitaire is a type of
game; a truck is a type of vehicle; a cat is a type of
animal; an invoice is a type of transaction. In each
case, a particular kind of object is a specific type of a
more general category of objects.
5
Benefits of Inheritance
The main reason we use inheritance in programming, particularly in
object-oriented programming (OOP), is to promote code reuse and
establish a natural hierarchical relationship among classes. Here are
the key benefits and reasons for using inheritance:
Code Reuse
Inheritance allows a new class (subclass or derived class) to use the
properties and methods of an existing class (superclass or base class). This
helps in avoiding redundancy and promoting code reuse, making the
codebase easier to maintain and extend.
Logical Hierarchy
It provides a way to create a logical hierarchy or classification. For instance, if
you have a general class Animal and specific classes like Dog and Cat, the
specific classes can inherit common properties and behaviors from the
Animal class. This models real-world relationships in a more natural and
understandable way.
Extensibility
Inheritance allows for extending existing code without modifying it. New
functionality can be added to the derived class, and polymorphism can be
used to alter or extend the behavior of the base class methods.
6
Cont…
Polymorphism
This is closely tied to inheritance. It allows objects to be
treated as instances of their parent class rather than
their actual class. This means a method can operate on
objects of different classes as long as they are derived
from the same base class. For example, a method
written to operate on Shape can work with Circle,
Square, or any other derived shape.
Encapsulation and Abstraction
By using inheritance, you can encapsulate common
functionality in the base class and keep specific
functionality in the derived classes. This helps in hiding
the complex details and exposing only what is
necessary, thus following the principles of abstraction
and encapsulation.
7
Creating Subclasses
The basic procedure for creating a subclass is simple. You
just use the extends keyword on the declaration for the
subclass. The basic format of a class declaration for a class
that inherits a base class is this:
public class DerivedClassName extends BaseClassName
{
// class body goes here
}
You can only specify one superclass for any subclass that
you create. Java does not support the inheritance of
multiple superclasses into a single subclass. This differs
from C++, in which you can inherit multiple base classes.
You can, however, create a hierarchy of inheritance in
which a subclass becomes a superclass of another
subclass. However, no class can be a superclass of itself.
8
Cont…
Example: a Cat is an Animal
public class Animal {
public void walk() {
[Link]("Animal walking…");
}
public void speak() {
[Link]("Animal sound…");
}
}
public class Cat extends Animal {
public void chaseMouse() {
[Link](“Chasing mouse…”);
}
public static void main(String[] args) {
Cat myCat = new Cat();
[Link]();
[Link]();
[Link]();
}
}
9
Cont…
A superclass’s public members are accessible
anywhere in its subclass types. A superclass’s
private members are accessible only in methods of
that superclass i.e. they are not acceissible in the
subclasses. Private members are generally not
inherited.
A superclass’s protected access members serve as
an intermediate level of protection between public
and private access. A superclass’s protected
members are accessible in the superclass, in the
subclasses and other classes in the same package
(protected members have package access).
10
Cont…
Example:public class Bicycle {
public int gear;
public int speed;
public Bicycle(int startSpeed, int startGear) {
gear = startGear;
speed = startSpeed;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
public class MountainBike extends Bicycle {
public int seatHeight;
public MountainBike(int startHeight, int startSpeed, int startGear) {
11
Cont…
super(startSpeed, startGear);
seatHeight = startHeight;
}
public void setHeight(int newValue) {
seatHeight = newValue;
}
public static void main(String args[]) {
Bike b = new Bike(20, 3);
MountainBike mb = new MountainBike();
}
}
12
Inheritance and Constructors
When you create an instance of a subclass, Java
automatically calls the default constructor of the
base class before it executes the subclass
constructor.
Example:
public class Ball {
public Ball() {
[Link](“Hello from the Ball constructor”);
}
}
public class BaseBall extends Ball {
public BaseBall() {
[Link](“Hello from the BaseBall constructor”);
}
public static void main(String args[]) {
BaseBall bb = new BaseBall();
}
}
13
Cont…
When you create an instance of the BaseBall
class, the following two lines are displayed on
the console:
Hello from the Ball constructor
Hello from the BaseBall constructor
If you want, it is possible to explicitly call a base
class constructor from a subclass by using the
super keyword. Because Java automatically calls
the default constructor for you, the only reason
to do this is to call a constructor of the base
class that uses a parameter.
14
Cont…
Example: calling superclass constructor
public class Vehicle {
public double speed;
public Vehicle(double speed) {
[Link] = speed;
}
}
public class Car extends Vehicle {
public double price;
public Car() {
super(80);
price = 300000;
}
}
Here, the Car constructor calls the Vehicle constructor to supply
a speed for the Vehicle.
15
Cont…
You need to obey a few rules and regulations when
working with superclass constructors:
If you use super to call the superclass constructor, you
must do so in the very first statement in the subclass
constructor.
If you don’t explicitly call super, the compiler inserts a
call to the default constructor of the base class. In that
case, the base class must have a default constructor. If
the base class doesn’t have a default constructor, the
compiler refuses to compile the program.
If the superclass is itself a subclass, the constructor for
its superclass is called in the same way. This continues
all the way up the inheritance hierarchy, until you get
to the Object class, which has no superclass.
16
Final Classes – Preventing
Inheritance
In object-oriented programming (OOP), a final class
is a class that cannot be inherited from. This means
that no other class can extend or derive from a final
class. The primary purpose of declaring a class as
final is to prevent inheritance and to ensure that
the class's implementation remains unchanged and
secure.
Declaring a class as final implicitly declares all of its
methods as final,Example:
too. As you might expect, it is
impossible to declare
final class A { a class as both abstract and
// ...
final since an abstract
}
class is incomplete by itself
and relies upon// The
itsfollowing
subclasses
class is illegal. to provide complete
class B extends A { // ERROR! Can't subclass A
implementations. // ...
}
17
Cont…
Here are some key points about final classes:
Prevents Inheritance
By marking a class as final, you make sure that the
class cannot be subclassed. This is useful when you
want to ensure that the behavior of the class remains
consistent and isn't altered through inheritance.
Enhanced Security and Integrity
Final classes can help protect the internal
implementation of a class. By preventing inheritance,
you ensure that the methods and properties of the
class are used as intended and not overridden or
extended in ways that could compromise their
integrity.
18
Cont…
Performance Optimization
In some cases, marking a class as final can allow
for performance optimizations by the compiler or
runtime environment. Since the compiler knows
that the class cannot be subclassed, it can make
certain assumptions that can lead to more efficient
code.
Clear Design Intent
Using final classes can make the design intent
clearer to other developers. It signals that the
class is meant to be used as-is and is not intended
to be a base class for other classes.
19
Use Cases for Final Classes
Immutable Objects
Classes that represent immutable objects, where the
state of the object cannot be changed once created,
are often made final to prevent subclassing that
might violate immutability.
Utility Classes
Classes that consist solely of static methods (e.g.,
utility or helper classes) are often final to prevent
instantiation or subclassing.
Security
Classes that manage sensitive data or critical
functionality might be made final to prevent
tampering or misuse through subclassing.
20
Types of inheritance
In Java, there are several types of inheritance, each
representing different ways in which classes can inherit
properties and methods from other classes. Here are the
primary types of inheritance in Java:
Single Inheritance
A class inherits from one and only one superclass.
This is the most straightforward type of inheritance where a subclass
extends a single superclass.
Example: class Animal {
void eat() {
[Link]("This animal eats food.");
}}
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}}
21
Cont…
Multilevel Inheritance
A class inherits from a superclass, and then another class
inherits from that subclass.
This forms a chain of inheritance where each class inherits
from its predecessor.
Example: class Animal {
void eat() {
[Link]("This animal eats food.");
}}
class Dog extends Animal {
void bark() {
[Link]("The dog barks."); }}
class dog22 extends Dog {
void fetch() {
[Link]("The Labrador fetches.");
}}
22
Cont…
Hierarchical Inheritance
Multiple classes inherit from a single superclass.
This type of inheritance is useful when different classes need
to share the common functionality of a single base class.
Example: class Animal {
void eat() {
[Link]("This animal eats food."); }
}
class Dog extends Animal {
void bark() { [Link]("The dog barks.");
}}
class Cat extends Animal {
void meow() { [Link]("The cat meows.");
}}
23
Cont…
Multiple Inheritance (via Interfaces)
Java does not support multiple inheritance with classes to avoid
complexity and ambiguity.
However, multiple inheritance is achievable through interfaces.
A class can implement multiple interfaces, thus inheriting the
abstract methods defined in those interfaces.
Example: interface CanFly {
void fly();}
interface CanSwim {
void swim();}
class Duck implements CanFly, CanSwim {
public void fly() {
[Link]("The duck flies."); }
public void swim() {
[Link]("The duck swims.");
}}
24
Cont…
Hybrid Inheritance (via Interfaces):
Hybrid inheritance is a combination of two or more types of inheritance.
Java doesn't support hybrid inheritance with classes to avoid ambiguity, but it
can be achieved using a combination of classes and interfaces.
Example: interface CanFly {
void fly();}
interface CanSwim {
void swim();}
class Animal {
void eat() {
[Link]("This animal eats food."); }}
class Duck extends Animal implements CanFly, CanSwim {
public void fly() {
[Link]("The duck flies."); }
public void swim() {
[Link]("The duck swims."); }}
25
Types of inheritance in java
26
Why multiple inheritance is not supported
in java?
To reduce the complexity and simplify the language, multiple
inheritance is not supported in java.
Consider a scenario where A, B and C are three classes. class
C inherits A and B.
If A and B classes have same method and you call it from child
class object, there will be ambiguity to call method
of A or B class.
Since compile time errors are better than runtime errors, java
renders compile time error if you inherit 2 classes.
So whether you have same method or different, there will be
compile time error now
27
Cont…
Example multiple inheritance
class A{
void msg(){[Link]("Hello");}
}
class B{
void msg(){[Link]("Welcome");}
}
class C extends A,B{//suppose if it were
Public Static void main(String args[]){
C obj=new C();
[Link]();//Now which msg() method would be invoked?
}}
//compile time error
28
Super
The super keyword in Java is a reference variable which is used to
refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent
class is created implicitly which is referred by super reference
variable.
The super keyword is similar to this keyword. Following are the
scenarios where the super keyword is used.
It is used to differentiate the members of superclass from the
members of subclass, if they have same names.
It is used to invoke the superclass method from subclass.
It is used to invoke the superclass constructor from subclass.
Differentiating the Members
If a class is inheriting the properties of another class. And if the
members of the superclass have the names same as the sub class, to
differentiate these variables we use super keyword as shown below.
[Link]
[Link]();
29
Cont…
Super is used to refer immediate parent class instance variable.
class Animal{
String color="white";
}
class Dog extends Animal{
String color="black";
void printColor(){
[Link](color);//prints color of Dog class
[Link]([Link]);//prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
}}
30
Cont…
Super can be used to invoke parent class method.
class Animal{
void eat(){[Link]("eating...");}
}
class Dog extends Animal{
void eat(){[Link]("eating bread...");}
void bark(){[Link]("barking...");}
void work(){
[Link]();
bark();
}
}
class TestSuper2{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
}}
31
Cont…
Super is used to invoke parent class constructor.
class Animal{
Animal(){[Link]("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
[Link]("dog is created");
}
}
class TestSuper3{
public static void main(String args[]){
Dog d=new Dog();
}}
32
Method Overriding and Overloading
Method overriding and overloading are two key
concepts in object-oriented programming (OOP) that
provide different ways to define and use methods in
classes. They both enhance the flexibility and
functionality of methods, but they do so in distinct ways.
Method Overriding
Method overriding occurs when a subclass provides a
specific implementation for a method that is already
defined in its superclass. The overridden method in the
subclass should have the same name, return type, and
parameter list as the method in the superclass.
Overriding is used to provide a specific implementation
that differs from or extends the behavior of the
superclass method.
33
Cont…
Key Points of Method Overriding:
Same Method Signature: The method in the subclass
must have the same name, return type, and parameters
as the method in the superclass.
Runtime Polymorphism: Overriding enables runtime
polymorphism, where the method to be invoked is
determined at runtime based on the object's actual type.
Annotations: In Java, the @Override annotation is used
to indicate that a method is intended to override a method
in the superclass. This helps catch errors at compile time.
Access Modifier: The access level of the overriding
method cannot be more restrictive than the method it
overrides.
34
Cont…
Note that to override a method, three conditions
have to be met:
The class must extend the class that defines the
method you want to override.
The method must be declared in the base class with
public or protected access. You can’t override a
private method.
The method in the subclass must have the same
signature as the method in the base class. In other
words, the name of the method and the parameter
types must be the same.
35
Cont…
Example:
public class Animal {
public static void speak() {
[Link]("The class method in Animal.");
}
public void walk() {
[Link]("The instance method in Animal.");
}
}
public class Cat extends Animal {
public static void speak() {
[Link]("The class method in Cat.");
}
public void walk() {
[Link]("The instance method in Cat.");
}
public static void main(String[] args) {
Animal myAnimal = new Cat();
[Link]();
[Link]();
}
}
36
Cont…
The Cat class overrides the instance method in Animal and hides the
class method in Animal. The main method in this class creates an
instance of Cat and calls speak() on the class and walk() on the instance.
The output from this program is as follows:
The class method in Animal.
The instance method in Cat.
The ability of a subclass to override a method allows a class to inherit
from a superclass whose behavior is "close enough" and then to modify
behavior as needed. The overriding method has the same name,
number and type of parameters, and return type as the method it
overrides. An overriding method can also return a subtype of the type
returned by the overridden method. This is called a covariant return
type.
When overriding a method, you might want to use
the @Override annotation that instructs the compiler that you intend to
override a method in the superclass. If, for some reason, the compiler
detects that the method does not exist in one of the superclasses, it will
generate an error.
37
Cont…
// Method overriding.
Example:
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
void show() {
[Link]("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// this overrides show() in A
@Override
void show() {
[Link]("k: " + k);
}
}
class TestOverride {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
[Link](); // this calls show() in B
}
}
38
Usage of Java Method Overriding
Method overriding in Java is a fundamental feature of object-oriented programming (OOP) that
allows a subclass to provide a specific implementation of a method already defined in its
superclass. This feature is used to ensure that a subclass can modify or extend the behavior of a
method inherited from its parent class. Here are some common usages and benefits of method
overriding:
Runtime Polymorphism (Dynamic Method Dispatch)
Method overriding is essential for achieving runtime polymorphism, where the
method to be executed is determined at runtime based on the object's actual type.
Providing Specific Implementations:
Subclasses can provide specific implementations of methods that are better suited
to their needs.
Implementing Abstract Methods:
When a subclass extends an abstract class, it must provide implementations for all
abstract methods declared in the abstract class.
Enhancing or Modifying Behavior:
Subclasses can enhance or modify the behavior of a superclass method while still
maintaining a common interface.
Providing a Consistent Interface:
Overriding ensures that different subclasses can be treated uniformly through their
common superclass interface.
39
Benefits of Method Overriding
Flexibility: Allows subclasses to provide specific behavior and
extend functionality.
Code Reusability: Promotes code reuse by allowing the same
method to be used in different contexts.
Polymorphism: Facilitates polymorphism, enabling objects to
be treated as instances of their superclass, with behavior
determined at runtime.
Maintenance: Improves code maintenance by localizing
changes in behavior to subclasses without modifying the
superclass.
40
Final methods – Preventing
Overriding
Sometimes, it may be necessary to prevent
overriding methods in a subcalss. This is done by
using final methods. A final method is a method that
can’t be overridden by a subclass. To create a final
method, you simply add the keyword final to the
method declaration.
Example:
public class SpaceShip {
double speed;
public final int getSpeed() {
return [Link];
}
}
41
Cont…
Here, the method getSpeed is declared as final. Thus,
any class that uses the SpaceShip class as a base
class can’t override the getSpeed method. If it tries,
the compiler issues an error message.
Here are some additional details about final methods:
Final methods execute more efficiently than non-final
methods. That’s because the compiler knows at compile
time that a call to a final method won’t be overridden by
some other method. The performance gain isn’t huge, but
for applications where performance is crucial, it can be
noticeable.
Private methods are automatically considered to be final.
That’s because you can’t override a method you can’t see.
42
Cont…
Example:
class A {
public final void meth() {
[Link]("This is a final method.");
}
}
class B extends A {
public void meth() { // ERROR! Can't override.
[Link]("Illegal!");
}
}
Because meth( ) is declared as final, it cannot be overridden in B.
If you attempt to do so, a compile-time error will result.
43
Method Overloading
Method overloading occurs when multiple methods
in the same class have the same name but different
parameter lists (different type, number, or both).
Overloading allows methods to be defined in such a
way that they can handle different types of input.
Suppose you have to perform addition of the given
numbers but there can be any number of
arguments, if you write the method such as a(int,int)
for two parameters, and b(int,int,int) for three
parameters then it may be difficult for you as well as
other programmers to understand the behavior of
the method because its name differs.
So, we perform method overloading to figure out the
44
program quickly.
Cont…
Advantage of method overloading
Method overloading increases the readability of the
program.
This provides flexibility to programmers so that they can call
the same method for different types of data.
This makes the code look clean.
This reduces the execution time because the binding is done
in compilation time itself.
Method overloading minimises the complexity of the code.
With this, we can use the code again, which saves memory.
There are two ways to overload the method in java
By changing number of arguments
By changing the data type
45
Cont…
1) Method Overloading: changing no. of
arguments
class Adder{
static int add(int a,int b){return a+b;}
static int add(int a,int b,int c){return a+b+c;}
}
class TestOverloading1{
public static void main(String[] args){
[Link]([Link](11,11));
[Link]([Link](11,11,11));
}}
46
Cont…
2) Method Overloading: changing data type of
arguments
class Adder{
static int add(int a, int b){return a+b;}
static double add(double a, double b)
{return a+b;}
}
class TestOverloading2{
public static void main(String[] args){
[Link]([Link](11,11));
[Link]([Link](12.3,12.6));
}}
47
Cont…
48
The Object Class
In Java, the Object class is the root of the class hierarchy. Every class
in Java is a descendant, directly or indirectly, of the Object class.
This means that the Object class provides a set of methods that are
available to every Java object.
Object class is present in [Link] package.
If a Class does not extend any other class then it is direct child class
of Object and if extends other class then it is an indirectly derived.
Therefore the Object class methods are available to all Java classes.
Hence Object class acts as a root of inheritance hierarchy in any Java
Program.
Using Object class methods
There are methods in Object class:
Student s = new Student();
// Below two statements are equivalent
[Link](s);
[Link]([Link]());
49
Cont…
50
Abstract Classes
In Java, an abstract class is a class that cannot be instantiated on its
own and is meant to be subclassed. Abstract classes are used to provide
a common template for other classes while allowing some methods to
be defined and others to be implemented by subclasses. This allows for
a high degree of flexibility and reusability in the code.
Abstraction refers to the ability to make a class abstract in OOP.
All other functionality of the class still exists, and its fields, methods,
and
constructors are all accessed in the same manner. You just cannot
create an instance of the abstract class.
This is typically how abstract classes come about during the design
phase.
A parent class contains the common functionality of a collection of child
classes, but the parent class itself is too abstract to be used on its own.
Use the abstract keyword to declare a class abstract.
The keyword appears in the class declaration somewhere before the
class keyword.
51
Cont…
Abstract class in Java is similar to interface
except that it can contain default method
implementation. An abstract class can have an
abstract method without body and it can have
methods with implementation also. abstract
keyword is used to create a abstract class and
method.
Abstract class in java can’t be instantiated. An
abstract class is mostly used to provide a base
for subclasses to extend and implement the
abstract methods and override or use the
implemented methods in abstract class.
52
Key Features of Abstract Classes
Cannot Be Instantiated
An abstract class cannot be instantiated directly. It can only be
used as a superclass for other classes.
Abstract Methods
Abstract classes can contain abstract methods, which are
declared without an implementation (i.e., no method body).
Subclasses of an abstract class must either provide
implementations for all abstract methods or be declared abstract
themselves.
Concrete Methods
Abstract classes can also have concrete (non-abstract) methods
with implementations. These methods can be inherited by
subclasses.
Constructors
Abstract classes can have constructors, which can be called
when creating instances of their subclasses.
53
Cont…
//abstract class
public abstract class Person {
private String name;
private String gender;
public Person(String nm, String gen){
[Link]=nm;
[Link]=gen;
}
//abstract method
public abstract void work();
@Override
public String toString(){
return "Name="+[Link]+"::Gender="+[Link];
}
public void changeName(String newName) {
[Link] = newName;
}
}
54
Cont…
Abstract Methods
If you want a class to contain a particular method but
you want the actual implementation of that method
to be determined by child classes, you can declare
the method in the parent class as an abstract.
abstract keyword is used to declare the method as
abstract.
You have to place the abstract keyword before the
method name in the method declaration.
An abstract method contains a method signature,
but no method body.
Instead of curly braces, an abstract method will
have a semi-colon (;) at the end.
55
Cont…
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){
[Link]("running safely..");}
public static void main(String args[]){
Bike obj = new Honda4();
[Link]();
}}
//output: running safely..
56
Cont…
Declaring a method as abstract has two results:
The class must also be declared abstract. If a class
contains an
abstract method, the class must be abstract as well.
Any child class must either override the abstract method
or
declare itself abstract.
A child class that inherits an abstract method must
override it. If they do not, they must be abstract, and any
of their children must override it.
Eventually, a descendant class has to implement the
abstract
method; otherwise, you would have a hierarchy of
abstract classes that cannot be instantiated.
57
Cont…
Abstract class important points
abstract keyword is used to create an abstract class in java.
Abstract class in java can’t be instantiated.
We can use abstract keyword to create an abstract method, an abstract method
doesn’t have body.
If a class have abstract methods, then the class should also be abstract using
abstract keyword, else it will not compile.
It’s not necessary for an abstract class to have abstract method. We can mark a
class as abstract even if it doesn’t declare any abstract methods.
If abstract class doesn’t have any method implementation, its better to use
interface because java doesn’t support multiple class inheritance.
The subclass of abstract class in java must implement all the abstract methods
unless the subclass is also an abstract class.
All the methods in an interface are implicitly abstract unless the interface
methods are static or default.
Java Abstract class can implement interfaces without even providing the
implementation of interface methods.
Java Abstract class is used to provide common method implementation to all the
subclasses or to provide default implementation.
We can run abstract class in java like any other class if it has main() method.
58
?
59