Unit4 PPL One Shot Annotated

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Unit 4: Inheritance, Packages and Exception Handling

using Java

Inheritances: member access and inheritance, super class references, Using super, multilevel
hierarchy, constructor call sequence, method overriding, dynamic method dispatch, abstract
classes, Object class.
Packages and Interfaces: defining a package, finding packages and CLASSPATH,
access protection,importing packages, interfaces (defining, implementation, nesting, applying)
variables in interfaces,extending interfaces, instance of operator. fundamental,
exception types, uncaught exceptions, try, catch, throw, throws, finally, multiple catch clauses
nested try statements, built-in exceptions,custom exceptions (creating your own exception
sub classes).

Managing I/O: Streams, Byte Streams and Character Streams, Predefined Streams,
Reading console Input, Writing Console Output, Print Writer class
Inheritance

* Inheritance is one of the OOP's paradigm.


* As the name says, we inherit something i.e. data members and methods
* In this the derived class can access the properties of base class from which it is derived.

* For creating inheritance, we use "extends" keyword.

* It produces various advantages like:


-> Code readability
-> Extensibility
-> Reduces redundancy

* Example :-
Types of inheritance

* There are different types of inheritance


-> Single inheritance
# In single inheritance, there is a single super/base/parent class for a
sub/derived/child class.

# It is the simplest form of inheritance.

-> Multiple Inhertiance


# In multiple inheritance, there are multiple base classes for a single derived class.
# Java doesn't implement multiple inheritance directly.4

-> Multilevel Inheritance


# In multilevel inheritance, a derived class is derived from a base class which is
derived from another base class.
Types of inheritance

* There are different types of inheritance


-> Hybrid Inheritance
# It is a combination of two or more inheritance are combined together.
# It can be very easy and sometimes be very complex.
Super Keyword

* Super keyword is used to refer to the superclass.


* It has basically 3 main usages:
-> Use of super with variables
# This scenario occurs when a derived class and base class have the same
data members. In that case, there is a possibility of ambiguity.

# Example:

-> Use of super with methods


# This is used when we want to call the parent class method.
# So whenever a parent and child class have the same-named methods then to
resolve ambiguity we use the super keyword.
Super Keyword

* Super keyword is used to refer to the superclass.


* It has basically 3 main usages:
-> Use of super with constructors
# The super keyword can also be used to access the parent class constructor.
# It can be both used as a parametrised as well as default constructor.
Abstract Classes

* Abstract classes are types of classes which help in defining a global template for various
subclasses.

* We cannot instantiate an abstract class i.e. we can't create an object for it.
Packages

* A package in Java is like a folder that groups together related classes and interfaces.
* You can imagine it as a way to organise your files so that you can easily find them.

* Some properties of packages are:


-> Organisation
-> Avoiding name conflicts
-> Access controls

Defining a package
* To define a package we use package keyword.
* The syntax is as follows:

* A simple example of a package:

CLASSPATH

* a parameter that tells the Java Virtual Machine (JVM) and the Java compiler where to look for
user-defined classes and packages when running or compiling a Java program.

* By default, the JVM looks for classes in the current directory.


Interfaces

* An interface in Java is like a contract or a blueprint that defines a set of methods that a
class must implement.
* It is a way to specify what a class should do, without saying how it should do it.

* Key points:
-> Method declaration
-> No implementation
-> multiple implementations
Defining an interface
* To define an interface we use interface keyword.
* The syntax is as follows:

* A simple example of a interface:


How to implement multiple inheritance using interface
How to implement multiple inheritance using interface
Exception Handling

* An exception in Java is an event that occurs during the execution of a program that
disrupts the normal flow of the program's instructions.

* Handling these exceptions is necssary as it may lead to failure or unusual results.

* Java provides various keywords like try, catch, finally and throw.

* There are different types of exception:


-> Checked exception : These types of exception are handled by try-catch block.
-> Unchecked exception : These types of exception are handled directly by JVM and no
need of explicit handling.

* Advantages of exception handling


-> Increased program readbility
-> Handling different types of errors
-> Improves code quality
-> Better error management
try, catch, finally and throw

* try keyword is used to mark the block of code which might create an exception.
* For example

* catch keyword as the name says is used to catch the exception and perform the necessary
operations.

* The throw keyword is used to explicitly throw an exception. You can throw either a predefined
exception or a custom exception.

* The finally block is used to execute code that must run regardless of whether an exception
was thrown or not.
Built In Exceptions
Streams

* Stream can be defined as the channel used to transfer data from the sender and
receiver.

* An input object that reads the stream of data from a file is called input stream.
* An output object that writes the stream of data to a file is called output stream.

* Byte Stream
-> They handle I/O of raw binary data.
-> InputStream: The super classes of all the input stream of bytes.
# FileInputStream : Reads input from the file
# BufferInputStream : used to read data from an underlying input stream more
efficiently.

-> OutputStream: The super classes of all the output stream of bytes.
# FileOutputStream: Writes bytes to the files.
# BufferOutputStream: used to write data to the underlying output stream more
efficiently

* Character Stream
-> Character streams handle I/O of character data. They read and write data in
characters

-> Reader : The superclass of all classes representing an input stream of characters.
# FileReader : Reads characters from the file.
# BufferedReader : Buffers input for efficient reading of characters.

-> Writer : The superclass of all classes representing an output stream of characters.
#FileWriter : Writes characters to a file.
# BufferedWriter : Buffers output for efficient writing of characters.

You might also like