JAVA
SEMESTER 4
UNIT - 2
HI COLLEGE
INHERITANCE TYPES
Single inheritance
where a subclass extends a single superclass.
Multilevel inheritance
where a subclass extends a superclass, and that subclass is further extended by another
subclass.
Hierarchical inheritance
where multiple subclasses extend a single superclass.
HI COLLEGE 01
INTERFACES IN JAVA [ ⚠️ IMP]
Conceptual identity similar to class
Supports multiple inheritance
It contains methods which are public and abstract by default.
It contains variables which are static and final by default.
Multiple inheritance (through interfaces)
where a subclass implements multiple interfaces.
Hybrid inheritance
a combination of two or more types of inheritance, such as multiple inheritance and
hierarchical inheritance.
HI COLLEGE 02
SUPER KEYWORD
The Super keyword in Java is used to refer to the parent class of a subclass.
It is often used to call the constructor or method of the parent class from
the subclass.
COVARIANT RETURN TYPE
We know what method overriding is, when there are multiple functions
of the same name, within a class or different classes( when they are
interlinked with the method of inheritance, so they too override).
Now covariant return type feature allows users to, keep different return
types for overriding functions in different classes.
Let’s say there is a function add in class A, its return type is int.
Now in class B, there is a add function with return type void.
Now, these classes are inherited and different return types of same
name functions add( int & void ) creates no issues in method overriding.
HI COLLEGE 03
ABSTRACT CLASSES
A class which is declared with the abstract keyword is known as an
abstract class in Java. It can have abstract and non-abstract methods.
An abstract class is a class that cannot be instantiated directly. It serves
as a blueprint or template for other classes to extend or inherit from.
An abstract class can also have constructors and instance variables, and
it can implement interfaces.
However, because it cannot be instantiated directly, an abstract class
must be subclassed or extended to be used in a program.
HI COLLEGE 04
DIFFERENCE BETWEEN INTERFACE AND
ABSTRACT CLASSES
HI COLLEGE 05
PACKAGES
Packages in java can be categorised in two forms, built-in package and user-
defined package.
Built in packages like util, lang, io etc.
User defined packages are the ones which a user can create for himself.
For example package hello;
Now this line package hello, will create a package hello and this should be the
first line of the source code.
You can now define sub packages or classes inside this package.
To import this package in your
IMPORTING A PACKAGE
Firstly we decide which package we want to import, like scanner is a
class of java.util package.
So, we write import package_name at the top of the program and
then use its classes.
For ex: import java.util.*;
Scanner sc = new Scanner(System.in);
EXCEPTION HANDLING [V.IMP]
Exceptions are used to handle errors and unexpected situations in java.
We use try and catch blocks in java to manage exceptions.
Try block contains the code which might contain any error.
While catch block contains the specification that it can handle the result of
try with commands provided in catch block and display them.
Now finally block exception contains that piece of code which will
eventually run if any exception in try block occurs or not.
Throw method is used to explicitly throw an exception, this is useful when
we want to create our own custom exceptions.
Throws method is basically used to declare that any method will throw an
exception. It is useful for programmers to tell others that this method will
throw an exception so it needs to be handled.
HI COLLEGE 06
HI COLLEGE 07
USE OF TRY, CATCH, THROW, THROWS,
FINALLY
EXAMPLE OF THROW:-
HI COLLEGE 08
EXAMPLE OF THROWS:-
HI COLLEGE 09
EXCEPTION CLASS/USER-DEFINED
EXCEPTIONS
Custom class that represents a specific type of exception.
One can create their own exception types and throw specific
error conditions.
This class defines a new exception type called "A" that can be
thrown in response to a specific error condition. The constructor
takes a s string that can be used to provide additional
information about the error.
BUILT-IN CHECKED AND UNCHECKED
EXCEPTIONS
The two main types of exceptions in Java are checked exceptions and
unchecked exceptions.
Checked exceptions are those that must be caught or declared to be
thrown by a method, while unchecked exceptions do not have this
requirement.
To create a custom exception class in Java, you can define a new class
that extends either the "Exception" class (for checked exceptions) or
the "RuntimeException" class (for unchecked exceptions).
HI COLLEGE 10