0% found this document useful (0 votes)
2 views

Introduction to OOPS with Java

The document provides an overview of Object-Oriented Programming (OOP) concepts using Java, including classes, objects, inheritance, and packages. It explains the structure of classes, access modifiers, and how to create objects and classes in Java. Key topics include the definition of classes, the instantiation of objects, and the principles of inheritance in OOP.

Uploaded by

nagaraju.rns3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to OOPS with Java

The document provides an overview of Object-Oriented Programming (OOP) concepts using Java, including classes, objects, inheritance, and packages. It explains the structure of classes, access modifiers, and how to create objects and classes in Java. Key topics include the definition of classes, the instantiation of objects, and the principles of inheritance in OOP.

Uploaded by

nagaraju.rns3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 92

Object Oriented Programming

[CSE202]

Introduction to OOP with Java


SAURABH SRIVASTAVA
ASSISTANT PROFESSOR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
IIT (ISM) DHANBAD
Highlights
We’ll start by discussing the concept of a class and an object
◦ We’ll (formally) discuss how classes and objects are created in Java
◦ We’ll talk about the different visibility levels of a class in Java

We’ll talk about packages


We’ll discuss the idea of inheritance
We’ll discuss members of a class
◦ We’ll talk about the different modifiers that can be attached to a member (except static and final)

We’ll talk about abstract methods and abstract classes


We’ll talk about Interfaces

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


What is a Class?
A class is a collection of related data items, together with some operations over them

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


What is a Class?
A class is a collection of related data items, together with some operations over them
A class groups together data of different types
◦ Arrays on the other hand groups data of the same type

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


What is a Class?
A class is a collection of related data items, together with some operations over them
A class groups together data of different types
◦ Arrays on the other hand groups data of the same type

A class consists of two types of members


◦ The data items or variables, called fields
◦ The operations or functions that operate over the fields, called methods

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


What is a Class?
A class is a collection of related data items, together with some operations over them
A class groups together data of different types
◦ Arrays on the other hand groups data of the same type

A class consists of two types of members


◦ The data items or variables, called fields
◦ The operations or functions that operate over the fields, called methods

A class is a “template”, declaring a class doesn’t occupy any memory

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Defining a Class in Java
Classes are defined in Java using the class keyword
[public] [abstract] class <Class Name> {
// Members details
}

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Defining a Class in Java
Classes are defined in Java using the class keyword
[public] [abstract] class <Class Name> {
// Members details
}
If the class keyword is preceded by the public keyword
◦ The class is visible to all the other Java classes (it must be in their CLASSPATH)

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Defining a Class in Java
Classes are defined in Java using the class keyword
[public] [abstract] class <Class Name> {
// Members details
}
If the class keyword is preceded by the public keyword
◦ The class is visible to all the other Java classes (it must be in their CLASSPATH)

If the class keyword is preceded by the abstract keyword


◦ No objects of the class can be instantiated
◦ It is an indication that the class may only be used for inheritance (we’ll talk about it shortly)

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Defining a Class in Java
Classes are defined in Java using the class keyword
[public] [abstract] class <Class Name> {
// Members details
}
If the class keyword is preceded by the public keyword
◦ The class is visible to all the other Java classes (it must be in their CLASSPATH)

If the class keyword is preceded by the abstract keyword


◦ No objects of the class can be instantiated
◦ It is an indication that the class may only be used for inheritance (we’ll talk about it shortly)

A class can be public and abstract, public or abstract, or none

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


public vs default access to a class
The public keyword is an access modifier

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


public vs default access to a class
The public keyword is an access modifier
Access modifiers control the access to a class
◦ There are access modifiers for fields and methods too, that we’ll discuss soon

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


public vs default access to a class
The public keyword is an access modifier
Access modifiers control the access to a class
◦ There are access modifiers for fields and methods too, that we’ll discuss soon

A class can either be declared public or without any modifier

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


public vs default access to a class
The public keyword is an access modifier
Access modifiers control the access to a class
◦ There are access modifiers for fields and methods too, that we’ll discuss soon

A class can either be declared public or without any modifier


A non-public class is said to have the default access

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


public vs default access to a class
The public keyword is an access modifier
Access modifiers control the access to a class
◦ There are access modifiers for fields and methods too, that we’ll discuss soon

A class can either be declared public or without any modifier


A non-public class is said to have the default access
A default access class is only visible to classes within its package

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


public vs default access to a class
The public keyword is an access modifier
Access modifiers control the access to a class
◦ There are access modifiers for fields and methods too, that we’ll discuss soon

A class can either be declared public or without any modifier


A non-public class is said to have the default access
A default access class is only visible to classes within its package
There can be at most one public class in a .java file
◦ If a .java file contains a public class, it must be named as <name of the public class>.java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


public vs default access to a class
The public keyword is an access modifier
Access modifiers control the access to a class
◦ There are access modifiers for fields and methods too, that we’ll discuss soon

A class can either be declared public or without any modifier


A non-public class is said to have the default access
A default access class is only visible to classes within its package
There can be at most one public class in a .java file
◦ If a .java file contains a public class, it must be named as <name of the public class>.java

There can be any number of non-public classes in a .java file


◦ If a .java file does not contain any public class, there are no restrictions on its name

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Creating Classes in Java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Creating Classes in Java

The class Player has public access

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Creating Classes in Java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Creating Classes in Java
The class Venue has default access

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


What is an Object?
An object is an instantiation of a class

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


What is an Object?
An object is an instantiation of a class
It is this instantiation that allocates memory for the fields of the class

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


What is an Object?
An object is an instantiation of a class
It is this instantiation that allocates memory for the fields of the class
The objects are created with the new keyword
<class name> <object name> = new <class name>(constructor arguments);

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


What is an Object?
An object is an instantiation of a class
It is this instantiation that allocates memory for the fields of the class
The objects are created with the new keyword
<class name> <object name> = new <class name>(constructor arguments);
A constructor is a special method of the class that is invoked when an object is initialized
◦ We’ll discuss constructors when we discuss methods

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


What is an Object?
An object is an instantiation of a class
It is this instantiation that allocates memory for the fields of the class
The objects are created with the new keyword
<class name> <object name> = new <class name>(constructor arguments);
A constructor is a special method of the class that is invoked when an object is initialized
◦ We’ll discuss constructors when we discuss methods

Every object has a different copy of all the fields of the class
◦ Except static fields, which we will discuss in the next lecture

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


What is an Object?
An object is an instantiation of a class
It is this instantiation that allocates memory for the fields of the class
The objects are created with the new keyword
<class name> <object name> = new <class name>(constructor arguments);
A constructor is a special method of the class that is invoked when an object is initialized
◦ We’ll discuss constructors when we discuss methods

Every object has a different copy of all the fields of the class
◦ Except static fields, which we will discuss in the next lecture

Every object can invoke methods of the class, which operate on the object’s copy of the fields
◦ Except static methods, which may be invoked just with the class too, which we will discuss in the next lecture

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Creating Objects in Java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Creating Objects in Java
Here, we are creating an object of the
class Venue

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Creating Objects in Java
Here, we are creating an object of the
class Venue

We are supplying two arguments, which


invoke the two arguments constructor of
the Venue class

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Creating Objects in Java
Two objects of CricketMatch class are
being created here

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Creating Objects in Java
Two objects of CricketMatch class are
being created here

They use different constructors, with two


and five arguments respectively

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


About Java packages
Packages are collections of Java classes

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


About Java packages
Packages are collections of Java classes
A package can have subpackages

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


About Java packages
Packages are collections of Java classes
A package can have subpackages
However, subpackages are only a means for organising classes
◦ A subpackage does not, for example, get access to classes with default access, in its parent package
◦ The classes in a subpackage are not imported when a * is used in an import statement, e.g.
import examples.oop.*;
will import all classes in the examples.oop package, but none from the examples.oop.cricket package

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


About Java packages
Packages are collections of Java classes
A package can have subpackages
However, subpackages are only a means for organising classes
◦ A subpackage does not, for example, get access to classes with default access, in its parent package
◦ The classes in a subpackage are not imported when a * is used in an import statement, e.g.
import examples.oop.*;
will import all classes in the examples.oop package, but none from the examples.oop.cricket package

A package is usually referred to by its fully qualified name


◦ The fully qualified name of a package is <fully qualified name of its parent>.<package>
◦ The fully qualified name of the topmost package in the hierarchy is the name of the package itself

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


The idea of Inheritance
Inheritance is the core concept of Object-Oriented Programming

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


The idea of Inheritance
Inheritance is the core concept of Object-Oriented Programming
In the real-world, objects of different types may still share some properties
◦ A motorbike and a pickup truck, even though are worlds apart, both have wheels
◦ A Cricket match, a Tennis match and a Football match, all start with the toss of a coin

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


The idea of Inheritance
Inheritance is the core concept of Object-Oriented Programming
In the real-world, objects of different types may still share some properties
◦ A motorbike and a pickup truck, even though are worlds apart, both have wheels
◦ A Cricket match, a Tennis match and a Football match, all start with the toss of a coin

Common properties between two objects, can be abstracted into an object of a super type

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


The idea of Inheritance
Inheritance is the core concept of Object-Oriented Programming
In the real-world, objects of different types may still share some properties
◦ A motorbike and a pickup truck, even though are worlds apart, both have wheels
◦ A Cricket match, a Tennis match and a Football match, all start with the toss of a coin

Common properties between two objects, can be abstracted into an object of a super type
The concept of Inheritance formalizes this observation
◦ A class is said to be derived out of one or more classes, if it inherits their properties
◦ One or more classes, from which this class is derived, are known as the super classes of the class

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java
All Java classes, are implicitly derived from the class java.lang.Object
◦ In other words, the Object class is a super class for all classes in Java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java
All Java classes, are implicitly derived from the class java.lang.Object
◦ In other words, the Object class is a super class for all classes in Java

A class can explicitly derive itself from another Java class, using the extends keyword
<access modifiers> class <derived class> [extends <super class>] {
// Members specific to the derived class
}

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java
All Java classes, are implicitly derived from the class java.lang.Object
◦ In other words, the Object class is a super class for all classes in Java

A class can explicitly derive itself from another Java class, using the extends keyword
<access modifiers> class <derived class> [extends <super class>] {
// Members specific to the derived class
}
Even if it does so, the inheritance hierarchy still continues up to the Object class
◦ Because the super class may have been derived from Object, or the super super class, and so on

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java
All Java classes, are implicitly derived from the class java.lang.Object
◦ In other words, the Object class is a super class for all classes in Java

A class can explicitly derive itself from another Java class, using the extends keyword
<access modifiers> class <derived class> [extends <super class>] {
// Members specific to the derived class
}
Even if it does so, the inheritance hierarchy still continues up to the Object class
◦ Because the super class may have been derived from Object, or the super super class, and so on

Java does not support multiple inheritance (having more than one super class for a class)
◦ There is an indirect way to apply multiple inheritance though – via Interfaces, which we’ll discuss shortly

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java
All Java classes, are implicitly derived from the class java.lang.Object
◦ In other words, the Object class is a super class for all classes in Java

A class can explicitly derive itself from another Java class, using the extends keyword
<access modifiers> class <derived class> [extends <super class>] {
// Members specific to the derived class
}
Even if it does so, the inheritance hierarchy still continues up to the Object class
◦ Because the super class may have been derived from Object, or the super super class, and so on

Java does not support multiple inheritance (having more than one super class for a class)
◦ There is an indirect way to apply multiple inheritance though – via Interfaces, which we’ll discuss shortly

Because of this, a variable of a super class, can hold reference to an object of a derived class
◦ Check the Homework ;)

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java
The class CricketMatch has 5 fields

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java

The code creates some objects for the


class, and invokes a method for describing
the match on those objects

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java

This is how the descriptions for two


objects look like

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java
The class IPLCricketMatch is derived
from the class CricketMatch

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java

This is how the descriptions for object


looks like…

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java

This is how the descriptions for object


looks like…

Notice that some part of the description is


coming up from the CricketMatch
class, while some more IPL specific details
are being added

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java

This is how the description for


IPLCricketMatch uses the description
of CricketMatch

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Inheritance in Java

This is how the description for


IPLCricketMatch uses the description
of CricketMatch

We’ll discuss the Override keyword in


the next lecture

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Fields of a class
A class can have fields of primitive types as well as objects

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Fields of a class
A class can have fields of primitive types as well as objects
Except for static fields, all fields are associated with a specific object

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Fields of a class
A class can have fields of primitive types as well as objects
Except for static fields, all fields are associated with a specific object
A field can be accessed by putting a dot after a specific object e.g. <object>.<field>

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Fields of a class
A class can have fields of primitive types as well as objects
Except for static fields, all fields are associated with a specific object
A field can be accessed by putting a dot after a specific object e.g. <object>.<field>
A field can have four access levels – public, private, protected and default

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Fields of a class
A class can have fields of primitive types as well as objects
Except for static fields, all fields are associated with a specific object
A field can be accessed by putting a dot after a specific object e.g. <object>.<field>
A field can have four access levels – public, private, protected and default
These modifiers control the visibility of the field, as shown below
Access Modifier within class within package outside package by outside package
subclass only

private Y N N N
default (no modifier) Y Y N N
protected Y Y Y N
public Y Y Y Y

Source: https://www.javatpoint.com/access-modifiers
SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD
Methods of a class
A method operates on the fields of the class

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Methods of a class
A method operates on the fields of the class
Except for static methods, all methods require an object of the class to be invoked

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Methods of a class
A method operates on the fields of the class
Except for static methods, all methods require an object of the class to be invoked
When a method is invoked, it operated on fields associated with the invoking object

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Methods of a class
A method operates on the fields of the class
Except for static methods, all methods require an object of the class to be invoked
When a method is invoked, it operated on fields associated with the invoking object
Out of all the methods, there is one special set of methods – constructors
◦ Constructors have a special signature – they do not return anything (not even void)
◦ If you do not define a constructor, a no arguments constructor is added implicitly (with no statements)
◦ A no-argument constructor can also be defined explicitly; in either case, it is called the default constructor
◦ If you define any constructor, with or without arguments, the implicit constructor becomes inaccessible
◦ This is an important point – convince yourself that you understand what this means

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Methods of a class
A method operates on the fields of the class
Except for static methods, all methods require an object of the class to be invoked
When a method is invoked, it operated on fields associated with the invoking object
Out of all the methods, there is one special set of methods – constructors
◦ Constructors have a special signature – they do not return anything (not even void)
◦ If you do not define a constructor, a no arguments constructor is added implicitly (with no statements)
◦ A no-argument constructor can also be defined explicitly; in either case, it is called the default constructor
◦ If you define any constructor, with or without arguments, the implicit constructor becomes inaccessible
◦ This is an important point – convince yourself that you understand what this means

All methods, including a constructor, have one of those four access levels
◦ In addition, any method other than the constructor can also be abstract

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract methods
Any method in the class can be declared as abstract
[access modifier] abstract <return type> <name>(<parameters>);

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract methods
Any method in the class can be declared as abstract
[access modifier] abstract <return type> <name>(<parameters>);
An abstract method does not have a body
◦ In other words, it is only an indication, that its details are not yet available

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract methods
Any method in the class can be declared as abstract
[access modifier] abstract <return type> <name>(<parameters>);
An abstract method does not have a body
◦ In other words, it is only an indication, that its details are not yet available

abstract methods represent operations which are too abstract for a class
◦ However, these operations are concrete for any derived class

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract methods
Any method in the class can be declared as abstract
[access modifier] abstract <return type> <name>(<parameters>);
An abstract method does not have a body
◦ In other words, it is only an indication, that its details are not yet available

abstract methods represent operations which are too abstract for a class
◦ However, these operations are concrete for any derived class

Example – A method called checkAllTheWheels() in a class called Automobile


◦ In order to check all the wheels, we must know how many wheels are there in the automobile
◦ For a motorbike, it will be 2; for a pickup truck, it will be 4
◦ So, the method checkAllTheWheels() should be declared abstract
◦ Two different definitions must be given in classes MotorBike and PickupTruck

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract classes
A class that contains even a single abstract method, can not be instantiated
◦ … because if an object invokes the abstract method, there is no definition which can be executed

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract classes
A class that contains even a single abstract method, can not be instantiated
◦ … because if an object invokes the abstract method, there is no definition which can be executed

A class that cannot be instantiated, is called an abstract class

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract classes
A class that contains even a single abstract method, can not be instantiated
◦ … because if an object invokes the abstract method, there is no definition which can be executed

A class that cannot be instantiated, is called an abstract class


An abstract class is meant to be a super class for other classes
◦ In other words, it is not supposed to be instantiated, but only extended

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract classes
A class that contains even a single abstract method, can not be instantiated
◦ … because if an object invokes the abstract method, there is no definition which can be executed

A class that cannot be instantiated, is called an abstract class


An abstract class is meant to be a super class for other classes
◦ In other words, it is not supposed to be instantiated, but only extended

While a class with even one abstract method must be abstract, the converse is not true
◦ You can declare a class as abstract, without any abstract method also

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract classes
A class that contains even a single abstract method, can not be instantiated
◦ … because if an object invokes the abstract method, there is no definition which can be executed

A class that cannot be instantiated, is called an abstract class


An abstract class is meant to be a super class for other classes
◦ In other words, it is not supposed to be instantiated, but only extended

While a class with even one abstract method must be abstract, the converse is not true
◦ You can declare a class as abstract, without any abstract method also

A class can be made abstract, by putting the keyword abstract before the class keyword
[public] abstract class <Abstract Class Name> {
// Members details
}

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract methods and classes in
Java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract methods and classes in
Java
Example of an abstract class -
Tournament

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract methods and classes in
Java
Example of an abstract class -
Tournament

It has one abstract method called


getMatches()

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract methods and classes in
Java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract methods and classes in
Java

The class IPL extends Tournament

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


abstract methods and classes in
Java

The class IPL extends Tournament


So IPL must provide a definition for
getMatches()

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java
Remember that Java does not allow multiple inheritance
◦ It means that you provide at the most one class with the extends keyword

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java
Remember that Java does not allow multiple inheritance
◦ It means that you provide at the most one class with the extends keyword

However, Java does allow multiple inheritance with some restrictions via Interfaces

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java
Remember that Java does not allow multiple inheritance
◦ It means that you provide at the most one class with the extends keyword

However, Java does allow multiple inheritance with some restrictions via Interfaces
An Interface is a pure abstract class
◦ It means that all the methods of an interface are abstract
◦ Since they are all abstract, you don’t need to write abstract in the method declaration

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java
Remember that Java does not allow multiple inheritance
◦ It means that you provide at the most one class with the extends keyword

However, Java does allow multiple inheritance with some restrictions via Interfaces
An Interface is a pure abstract class
◦ It means that all the methods of an interface are abstract
◦ Since they are all abstract, you don’t need to write abstract in the method declaration

An interface cannot have any fields, except for static members

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java
Remember that Java does not allow multiple inheritance
◦ It means that you provide at the most one class with the extends keyword

However, Java does allow multiple inheritance with some restrictions via Interfaces
An Interface is a pure abstract class
◦ It means that all the methods of an interface are abstract
◦ Since they are all abstract, you don’t need to write abstract in the method declaration

An interface cannot have any fields, except for static members


A class can implement one or more interfaces with the implements keyword
[public] class <Class Name> implements Interfacei[,Interfacej …]{
// Members details
}

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java
Remember that Java does not allow multiple inheritance
◦ It means that you provide at the most one class with the extends keyword

However, Java does allow multiple inheritance with some restrictions via Interfaces
An Interface is a pure abstract class
◦ It means that all the methods of an interface are abstract
◦ Since they are all abstract, you don’t need to write abstract in the method declaration

An interface cannot have any fields, except for static members


A class can implement one or more interfaces with the implements keyword
[public] class <Class Name> implements Interfacei[,Interfacej …]{
// Members details
}
The class will have to provide definitions for all the methods of the interfaces it implements

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java
Interface names usually end with the
suffix “able”, e.g. Procurable

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java

IPLPlayer extends Player and


implements Procurable

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Interfaces in Java

IPLPlayer extends Player and


implements Procurable

So it must provide a definition for the


getOwner() method

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD


Homework (want to write some code?)
See the OtherClasses.java file
◦ What observations can you draw?

Read the main() method for the class CricketMatch


◦ This should give you an idea of the overall hierarchy

Open the class IPL, and add a main() method in it


◦ You should create two instances of IPLCricketMatch in the getMatches() method
◦ Add players to the teams, and add teams, venue and toss to the match
◦ From the main() method, you should invoke their describeMatch() method on the two instances

You may submit it over email to me (I won’t mind :P)

SAURABH SRIVASTAVA | ASSISTANT PROFESSOR | IIT(ISM) DHANBAD

You might also like