Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 61

Object Oriented Programming

19CSE (2nd Semester)


Chapter#06: Inheritance in Java

1
Outline
Inheritance
What is inheritance?
Some important terms
Why we use inheritance?
Types of inheritance
Two types of relationships
Inheritance v/s composition
Forms of inheritance
Member access rules
Example programs
Uses of keyword super
When constructors are called?
Polymorphism
Overriding methods
Dynamic method dispatching
Overloading v/s overriding
Uses of keyword final
Abstract classes
Summary 2
Inheritance
What is Inheritance?
 Inheritance is an important pillar of OOP.
 Inheritance is the process of creating new class from existing
class.
 It is the mechanism in java by which one class is allow to
inherit the features(fields and methods) of another class.
Important Terms
 Sub Class/Child Class: Subclass is a class which inherits the
other class. It is also called a derived class, extended class,
or child class.
 Super Class/Parent Class: Superclass is the class from which
a subclass inherits the features. It is also called a base class
or a parent class.
Inheritance
Important Terms
 Reusability: As the name specifies, reusability is a
mechanism which facilitates you to reuse the fields and
methods of the existing class when you create a new
class. You can use the same fields and methods already
defined in the previous class.
Why we use Inheritance?
 For Method Overriding (so runtime polymorphism can
be achieved).
 For Code Reusability.
Types of Inheritance
Single Inheritance
 In single inheritance, subclasses inherit the features of one
superclass. In image below, the class A serves as a base class for
the derived class B.
Multiple Inheritance
 In Multiple inheritance ,one class can have more than one
superclass and inherit features from all parent classes.
 Java does not support multiple inheritance with classes.
 In java, we can achieve multiple inheritance only through
Interfaces. In image below, Class C is derived from interface A and
B.
Types of Inheritance
Multilevel Inheritance
 In Multilevel Inheritance, a derived class will be inheriting a base
class and as well as the derived class also act as the base class to
other class.
 In below image, the class A serves as a base class for the derived
class B, which in turn serves as a base class for the derived class
C.
Hierarchical Inheritance
 In Hierarchical Inheritance, one class serves as a superclass (base
class) for more than one sub class.
 In below image, the class A serves as a base class for the derived
class B,C and D.
Types of Inheritance
Hybrid Inheritance(Through Interfaces) :
 It is a mix of two or more of the above types of inheritance.
 Since java doesn’t support multiple inheritance with classes, the
hybrid inheritance is also not possible with classes.
 In java, we can achieve hybrid inheritance only through
Interfaces.
Types of Inheritance
Inheritance v/s Composition
Inheritance
 Inheritance is “IS A” or “Kind of “ relationship. When a class is derived from
another class i.e. Object “is an” object of another class. We use keyword extends.
 Examples:
 A Teacher is an Employee
 A car is a vehicle, Orange is a fruit.
 A surgeon is a doctor, A cat is an animal.
Composition
 Composition is “Has A” relationship
 It is also used for code reusability in Java.
 In Java, a Has-A relationship simply means that an instance of one class has a
reference to an instance of another class or an other instance of the same class.
 Examples:
 a car has an engine
 a dog has a tail and so on.
 In Java, there is no such keyword that implements a Has-A relationship. But we
mostly use new keywords to implement a Has-A relationship in Java.
Forms of Inheritance
Inheritance is used in a variety of way and for a
variety of different purposes .
Inheritance for Specialization
Inheritance for Specification
Inheritance for Construction
Inheritance for Extension
Inheritance for Limitation
Inheritance for Combination
One or many of these forms may occur in a single
case.
Forms of Inheritance
Inheritance for Specialization
 By far the most common form of inheritance is for specialization.
 Always creates a subtype
 Child class is a specialized form of parent class
 Principle of substitutability holds
 A good example is the Java hierarchy of Graphical components in
the AWT:
 Component
 Label
 Button
 TextComponent
 TextArea
 TextField
 CheckBox
 ScrollBar
Forms of Inheritance
Inheritance for Specification
 The next most common form of inheritance involves
specification.
 The parent class specifies some behavior, but does not
implement the behavior
Child class implements the behavior
Similar to Java interface or abstract class
When parent class does not implement actual behavior
but merely defines the behavior that will be
implemented in child classes
 Example, Java 1.1 Event Listeners:
ActionListener, MouseListener, and so on specify
behavior, but must be subclassed.
Forms of Inheritance
Inheritance for Construction
The parent class is used only for its behavior, the
child class has no is-a relationship to the parent.
Child modify the arguments or names of
methods
An example might be sub-classing the idea of a Set
from an existing List class.
Child class is not a more specialized form of
parent class; no substitutability
Forms of Inheritance
Inheritance for Extension
The child class generalizes or extends the parent
class by providing more functionality
In some sense, opposite of sub-classing for
specialization
The child doesn't change anything inherited from
the parent, it simply adds new features
Often used when we cannot modify existing base
parent class
Example, ColoredWindow inheriting from Window
Add additional data fields
Override window display methods
Forms of Inheritance
Inheritance for Limitation
The child class limits some of the behavior
of the parent class.
Example, you have an existing List data type,
and you want a Stack
Inherit from List, but override the methods
that allow access to elements other than top
so as to produce errors.
Forms of Inheritance
Inheritance for Limitation
occurs when the behavior of the subclass is
smaller or more restrictive that the behavior
of its parent class.
The child class limits some of the behavior of the
parent class.
Example, you have an existing List data type,
and you want a Stack
Inherit from List, but override the methods
that allow access to elements other than top
so as to produce errors.
Forms of Inheritance
Inheritance for Combination
 This types of inheritance is known as multiple
inheritance in Object Oriented Programming.
 Although the Java does not permit a subclass to
be formed be inheritance from more than one
parent class, several approximations to the
concept are possible.
 Example of this type is Hole class defined as;
class Hole extends Ball implements PinBallTarget{
// body of class
}
Forms of Inheritance
 Specialization. The child class is a special case of the parent class;
in other words, the child class is a subtype of the parent class. E.g
Alto Car is a Car
 Specification. The parent class defines behavior that is implemented
in the child class but not in the parent class.
 Construction. The child class makes use of the behavior provided by
the parent class, but is not a subtype of the parent class. E.g.
formation of a Set from an existing List class.
 Generalization. The child class modifies or overrides some of the
methods of the parent class.
 Extension. The child class adds new functionality to the parent class,
but does not change any inherited behavior.
Example, ColoredWindow inheriting from Window.
 Limitation. The child class restricts the use of some of the behavior
inherited from the parent class.
 Example, you have an existing List data type, and you want a Stack
 Combination. The child class inherits features from more than one
parent class. This is multiple inheritance
Member Access rules
 Visibility modifiers determine which class members are accessible
and which do not
 Members (variables and methods) declared with public visibility
are accessible, and those with private visibility are not
 Problem: How to make class/instance variables visible only to its
subclasses?
 Solution: Java provides a third visibility modifier that helps in
inheritance situations: protected
 public : can be accessed from outside the class definition.
 protected : can be accessed only within the class definition in
which it appears, within other classes in the same package, or within
the definition of subclassess.
 private : can be accessed only within the class definition in
which it appears.
 default-access (if omitted) features accessible from inside the
current Java package
Example Program:
Single Inheritance
Calculator
 Data members
 num1,num2
 Methods
 Constructors
 add Calculator
 sub

MyCalculator
 Methods MyCalculator
 Constructors
 mul
 div
Example program: Single Inheritance (coding)
class MyCalculator extends Calculator
class Calculator
{
{
MyCalculator(double n1,double n2)
double num1;
{
double num2;
super(n1,n2);
Calculator(double n1,double n2)
}
{ void mul()
num1=n1; {
num2=n2; System.out.println("The multiplication
} between two numbers is>>"+(num1*num2));
void add() }
{ void div()
System.out.println("The sum of two numbers {
is>>"+(num1+num2)); System.out.println("The division between two
} numbers is>>"+(num1/num2));
void sub() }
{ }
System.out.println("The difference between
two numbers is >>"+(num1-num2));
}
}
Example program: Single Inheritance (coding)
public class MainCalculator
{
public static void main(String []args)
{
MyCalculator cal=new MyCalculator(50,10);
cal.add();
cal.sub();
cal.mul();
cal.div();
}
}
Uses of Keyword Super
Super keyword is used to access the members of
super class within sub/child class.
super’ is a keyword used to refer to hidden variables of
super class from sub class.
super.a=a;
It is used to call a constructor of super class from
constructor of sub class which should be first statement.
super(a,b);
It is used to call a super class method from sub class
method to avoid redundancy of code
super.addNumbers(a, b);
Why is super needed to access super-class members?
 When a sub-class declares the variables or methods with the
same names and types as its super-class:
 Uses of Keyword Super
Why is super needed to access super-class members?
 When a sub-class declares the variables or methods with the same
names and types as its super-class:
class A
{
int i = 1;
}
class B extends A
{
int i = 2;
System.out.println(“i is “ + i);
}
 The re-declared variables/methods hide those of the super-class.
Example program: Hierarchical Inheritance (coding)
class Education
class Person
{
{
String schoolname;
String name;
String degree;
int age;
void show()
void show()
{
{
System.out.println("School
System.out.println("Name="+name); name="+schoolname);
System.out.println("Age="+age); System.out.println("Highest degree is
} "+degree);
void get() }
{ void get()
Scanner input=new {
Scanner(System.in); Scanner input=new Scanner(System.in);
System.out.print("Enter name>>"); System.out.print("Enter School name>>");
name=input.next(); schoolname=input.next();
System.out.print("Enter age>>"); System.out.print("Enter highest
age=input.nextInt(); degree>>");
} degree=input.next();
} }
}
Example program:
 Implement following class hierarchy.
Hybrid Inheritance
 Use the specific data for each of the classes. Each of these class should have
overridden methods: get and show; to get data from the user and display the
data respectively.
 Use key word super to access super member with in the child class.
 Write a test program by creating instances of a peon, teacher and professor,
get data of these and display their data.

Person

IS A IS A

Peon Teacher Education


Has AN

IS A

Professor
Example program: Hybrid Inheritance (coding)
class Education
class Person
{
{
String schoolname;
String name;
String degree;
int age;
void show()
void show()
{
{
System.out.println("School
System.out.println("Name="+name); name="+schoolname);
System.out.println("Age="+age); System.out.println("Highest degree is
} "+degree);
void get() }
{ void get()
Scanner input=new {
Scanner(System.in); Scanner input=new Scanner(System.in);
System.out.print("Enter name>>"); System.out.print("Enter School name>>");
name=input.next(); schoolname=input.next();
System.out.print("Enter age>>"); System.out.print("Enter highest
age=input.nextInt(); degree>>");
} degree=input.next();
} }
}
Example program: Hybrid Inheritance (coding)
class Teacher extends Person class Peon extends Person
{ {
String designation;
int lenservice; }
Education edu=new Education();
class Professor extends Teacher
void show()
{
{
int pub;
super.show(); void show()
edu.show(); {
System.out.println("the super.show();
designation="+designation); System.out.println("The number of publications
System.out.println("the length of are="+pub);
service="+lenservice); }
} void get()
void get() {
{ super.get();
super.get(); Scanner input=new Scanner(System.in);
edu.get(); System.out.println("Enter number of publications>>");
pub=input.nextInt();
Scanner input=new Scanner(System.in);
}
System.out.print("Enter designation>>");
}
designation=input.next();
System.out.print("Enter length of service>>");
lenservice=input.nextInt();
}
}
Example program: Hybrid Inheritance (coding) Output
Enter data of t1
import java.util.Scanner; Enter name>>Shahzad
public class MainClass Enter age>>35
Enter School name>>MUET
{ Enter highest degree>>MCS
public static void main(String []args) Enter designation>>AP
Enter length of service>>10
{ Enter data of p1
Teacher t1=new Teacher(); Enter name>>Suhail
Enter age>>55
Peon p1=new Peon(); Enter data of professor pf
Professor pf=new Professor(); Enter name>>Faheem
Enter age>>52
System.out.println("Enter data of t1"); Enter School name>>QUEST
t1.get(); Enter highest degree>>PhD
Enter designation>>Professor
System.out.println("Enter data of p1"); Enter length of service>>23
p1.get(); Enter number of publications>>
16
System.out.println("Enter data of professor pf");
the data of t1 is
pf.get(); Name=Shahzad
System.out.println("the data of t1 is "); Age=35
School name=MUET
t1.show(); Highest degree is MCS
System.out.println("the data of p1 is "); the designation=AP
the length of service=10
p1.show(); the data of p1 is
System.out.println("the data of professor pf is "); Name=Suhail
Age=55
pf.show(); the data of professor pf is
} Name=Faheem
Age=52
} School name=QUEST
Highest degree is PhD
the designation=Professor
the length of service=23
The number of publications are=16
Example program: Multilevel Inheritance
Box
 Data members WeightBox
Length  Data members
 weight
Width
 Methods
Height  Constructors
 Methods  Show
Constructors  Get
Show
get
volume Shipment
 Data members
 Cost
 Methods
 Constructors
 Show
 Get
Example program: Multilevel Inheritance (coding)
class Box
{
double volume()
double length;
{
double width;
return length*width*height;
double height;
}
//constructors
void show()
Box()
{
{
System.out.println("Length="+length);
length=0.0;
System.out.println("width="+width);
width=0.0;
System.out.println("height="+height);
height=0.0;
}
}
void get()
Box(double l,double w,double h)
{
{
Scanner input=new Scanner(System.in);
length=l;
System.out.println("Enter length>>");
width=w;
length=input.nextDouble();
height=h;
System.out.println("Enter width>>");
}
width=input.nextDouble();
Box(double len)
System.out.println("Enter height>>");
{
height=input.nextDouble();
length=len;
}
width=len;
}// end of Box class
height=len;
}
Example program: Multilevel Inheritance (coding)
class BoxWeight extends Box
{ class Shipment extends BoxWeight
double weight; {
//constructors double cost;
BoxWeight()
//constructors
Shipment()
{
{
super();
super();
weight=0.0;
cost=0.0;
} }
BoxWeight(double l,double w,double h,double wt) Shipment(double l,double w,double h,double wt,double c)
{ {
super(l,w,h); super(l,w,h,wt);
weight=wt; cost=c;
} }
BoxWeight(double len,double wt) Shipment(double len,double wt,double c)
{ {
super(len); super(len,wt);
weight=wt; cost=c;
} }
void show()
void show()
{
{
super.show();
super.show();
System.out.println("Cost="+cost);
System.out.println("Weight="+weight); }
} void get()
void get() {
{ Scanner input=new Scanner(System.in);
Scanner input=new Scanner(System.in); super.get();
super.get(); System.out.println("Enter cost>>");
System.out.println("Enter weight>>"); cost=input.nextDouble();
weight=input.nextDouble(); }
} }
}
Example program: Multilevel Inheritance (coding)
import java.util.Scanner;
public class DeriverClass
{
public static void main(String[] args)
{
BoxWeight wb=new BoxWeight(1.1,1.2,1.3,10.0);
Shipment sp=new Shipment(2.3,15.0,500.0);
System.out.println("the Boxweight wb=");
wb.show();
System.out.println("Its volume is "+wb.volume());
System.out.println("the Shimpment box sp=");
sp.show();
System.out.println("Its volume is "+sp.volume());
}
} Output
the Boxweight wb=
Length=1.1
width=1.2
height=1.3
Weight=10.0
Its volume is 1.7160000000000002
the Shimpment box sp=
Length=2.3
width=2.3
height=2.3
Weight=15.0
Cost=500.0
Its volume is 12.166999999999996
When Constructors Are Called?
 When a class hierarchy is created, in what order are the
constructors for the classes that make up the hierarchy called?
 For example, given a subclass called B and a superclass called A, is
A’s constructor called before B’s, or vice versa? The answer is that
in a class hierarchy, constructors are called in order of derivation,
from superclass to subclass.
 Further, since super( ) must be the first statement executed in a
subclass’ constructor, this order is the same whether or not super( )
is used. If super( ) is not used, then the default or parameterless
constructor of each superclass will be executed.
 The following program illustrates when constructors are executed:
When Constructors Are Called?
// Demonstrate when constructors are called.
// Create a super class.
class A {
A() {
System.out.println("Inside A's constructor.");
}
}
// Create a subclass by extending class A.
class B extends A {
B() {
System.out.println("Inside B's constructor.");
}
}
// Create another subclass by extending B.
class C extends B {
C() {
System.out.println("Inside C's constructor.");
}
}
When Constructors Are Called?
class CallingCons
{
public static void main(String args[])
{
C c = new C();
}
}
The output from this program is shown here:
Inside A’s constructor
Inside B’s constructor
Inside C’s constructor
As you can see, the constructors are called in order of derivation.
If you think about it, it makes sense that constructors are executed in order of
derivation. Because a superclass has no knowledge of any subclass, any initialization it
needs to perform is separate from and possibly prerequisite to any initialization
performed by the subclass. Therefore, it must be executed first.
Polymorphism
 Polymorphism is one of three pillars of object-orientation.
 Polymorphism: many different (poly) forms of objects that
share a common interface respond differently when a method of
that interface is invoked:
 a super-class defines the common interface
 sub-classes have to follow this interface (inheritance), but
are also permitted to provide their own implementations
(overriding)
 A sub-class provides a specialized behaviors relying on the
common elements defined by
L 7.1
its super-class.
Polymorphism
 A polymorphic reference can refer to different types of objects at
different times
 In java every reference can be polymorphic except of references to
base types and final classes.
 It is the type of the object being referenced, not the reference type, that
determines which method is invoked
 Polymorphic references are therefore resolved at run-time, not during
compilation; this is called dynamic binding
 Careful use of polymorphic references can lead to elegant, robust
software designs
Overridden Methods
 In a class hierarchy, when a method in a subclass has the
same name and type signature as a method in its
superclass, then the method in the subclass is said to
override the method in the superclass.
 When an overridden method is called from within the
sub-class:
1) it will always refer to the sub-class method
2) super-class method is hidden
Overridden Methods: Example
class A
{
int i, j;
A(int a, int b) When show() is invoked on an object of
{ type B, the version of show() defined in B
i = a; j = b; is used:
}
void show()
{
System.out.println("i and j: " + i + " " + j);
}
class Override
} {
public static void main(String args[])
class B extends A
{
{
int k; B subOb = new B(1, 2, 3);
B(int a, int b, int c) subOb.show();
{ }
super(a, b); }
k = c;
}
void show()
{ The version of show() in A is hidden
System.out.println("k: " + k); through overriding
}
}
Overridden v/s Overloaded Methods

 Overloading deals with  Overriding deals with two


multiple methods in methods, one in a parent
the same class with the class and one in a child
same name but class, that have the same
different signatures signature
 Overloading lets you  Overriding lets you define
define a similar a similar operation in
operation in different different ways for
ways for different data different object types
Why Overridden Methods?
 Overridden methods allow Java to support run-time
polymorphism.
 Polymorphism is essential to object-oriented
programming for one reason: it allows a
 general class to specify methods that will be common to all of its
derivatives, while allowing subclasses to define the specific
implementation of some or all of those methods.
 Overridden methods are another way that Java
implements the “one interface, multiple methods” aspect
of polymorphism.
What is Dynamic Method Dispatch?
 Method overriding forms the basis for one of Java’s most
powerful concepts: dynamic method dispatch.
 Dynamic method dispatch is the mechanism by which a
call to an overridden method is resolved at run time,
rather than compile time.
 Dynamic method dispatch is important because this is
how Java implements run-time polymorphism.
Example Program:
Hierarchical Inheritance

Figure
Rectangle Triangle Circle
 Data members
 Methods  Methods  Data members
 Methods
 Constructors  Constructors  radius
 Constructors
 area  area  Meth
 area
 Constructors
 area
Example Program: Hierarchical Inheritance
// Using run-time polymorphism.
class Figure
{
double dim1;
double dim2;
Figure(double a, double b)
{
dim1 = a;
dim2 = b;
}
double area()
{
System.out.println("Area for Figure is undefined.");
return 0;
}
}
Example Program: Hierarchical Inheritance
class Rectangle extends Figure
{
Rectangle(double a, double b)
{
super(a, b);
}
// override area for rectangle
double area()
{
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}
Example Program: Hierarchical Inheritance
class Triangle extends Figure
{
Triangle(double a, double b)
{
super(a, b);
}
// override area for right triangle
double area()
{
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
Example Program: Hierarchical Inheritance
class Circle Figure
{
Double radius;
Circle(double a, double b, double rad)
{
super(a, b);
radius=rad;
}
// override area for right triangle
double area()
{
System.out.println("Inside Area for Circle.");
return 3.14*radius*radius;
}
}
Example Program: Hierarchical Inheritance
class FindAreas
{
public static void main(String args[])
{
Figure f = new Figure(10, 10);
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
figref = f;
System.out.println("Area is " + figref.area());
}
}
Uses of final keyword
Final keyword in java is used for three purposes:
1) Used to make variables constant
2) Used to restrict method overriding
3) Used to restrict inheritance
Uses of final keyword
 If a variable is declared with the final keyword, its value cannot be changed once
initialized.
 Example:1 Example:2
public class MyClass
class FinalVariable {
{ final int x = 10;
final int var = 50; public static void main(String[] args)
{
var = 60 MyClass myObj = new MyClass();
//This line would give an error myObj.x = 25;
} // will generate an error: cannot assign a value to a final variable
System.out.println(myObj.x);
}
 If you ever see the final keyword with
} a parameter variable, it means that the value of this
variable cannot be changed anywhere in the function.
 Example:3
class finalParameter
{
public static void example( final int parameter )
{
parameter = 4; //attempting to reassign a value to a parameter throws an error
}
}
Uses of final keyword
 For a final reference variable you cannot change what object it refers to. You can,
however, modify the object itself.
 Example:4
class Reference
{
public int value = 5;
}
class frVariable
{
public static void main( String args[] )
{
final Reference example = new Reference(); //declaration
example.value = 6; // Modifying the object creates no disturbance
Reference another = new Reference();
example = another; // Attempting to change the object it refers to, creates an error
}
}
Uses of final keyword
Preventing Overriding with final
 A method declared final cannot be overridden in any sub-class:
 All methods and variables can be overridden by default in subclasses.
 This can be prevented by declaring them as final using the keyword “final” as a modifier.
 This ensures that functionality defined in this method cannot be altered any.
 Example
class A
{
final void meth()
{
System.out.println("This is a final method.");
}
}
//This class declaration is illegal:
class B extends A
{
void meth()
{
System.out.println("Illegal!");
}
}
Uses of final keyword
Preventing Classes being extended
 A class declared final cannot be inherited – has no sub-classes.
final class A { … }
 This class declaration is considered illegal:
 class B extends A { … }
 Declaring a class final implicitly declares all its methods final.
 It is illegal to declare a class as both abstract and final.
 We can prevent an inheritance of classes by other classes by declaring them as final classes.
 This is achieved in Java by using the keyword final
final class Person
{
// members
}
final class Student extends Person
{
// members
}
Any attempt to inherit these classes will cause an error.
Abstract Classes
 When we define a class to be “final”, it cannot be
extended. In certain situation, we want to
properties of classes to be always extended and
used. Such classes are called Abstract Classes.

 An Abstract class is a conceptual class.


 An Abstract class cannot be instantiated – objects
cannot be created.

 Abstract classes provides a common root for a


group of classes, nicely tied together in a package:
Abstract Class Syntax
abstract class ClassName
{
...

abstract Type MethodName1();


Type Method2()
{
// method body
}
}
 When a class contains one or more abstract methods, it should be
declared as abstract class.
 The abstract methods of an abstract class must be defined in its subclass.
 We cannot declare abstract constructors or abstract static methods.
Abstract Class -Example
• Shape is a abstract class.

Shape

Circle Rectangle
The Shape Abstract Class
public abstract class Shape {
public abstract double area();
public void move() { // non-abstract method
// implementation
}
}

• Is the following statement valid?


– Shape s = new Shape();
• No. It is illegal because the Shape class is an abstract class,
which cannot be instantiated to create its objects.
Abstract Classes
public Circle extends Shape {
protected double r;
protected static final double PI
=3.1415926535;
public Circle() { r = 1.0; )
public double area() { return PI * r * r; }

}
public Rectangle extends Shape {
protected double w, h;
public Rectangle() { w = 0.0; h=0.0; }
public double area() { return w * h; }
}
Abstract Classes Properties
• A class with one or more abstract methods is
automatically abstract and it cannot be instantiated.
• A class declared abstract, even with no abstract
methods can not be instantiated.
• A subclass of an abstract class can be instantiated if it
overrides all abstract methods by implementation
them.
• A subclass that does not implement all of the
superclass abstract methods is itself abstract; and it
cannot be instantiated.
Summary
 If you do not want (properties of) your class to be extended or
inherited by other classes, define it as a final class.
 Java supports this is through the keyword “final”.
 This is applied to classes.
 You can also apply the final to only methods if you do not
want anyone to override them.
 If you want your class (properties/methods) to be extended
by all those who want to use, then define it as an abstract
class or define one or more of its methods as abstract
methods.
 Java supports this is through the keyword “abstract”.
 This is applied to methods only.
 Subclasses should implement abstract methods; otherwise, they
cannot be instantiated.

You might also like