Fundamentals of Object Oriented Programming

Similar documents
Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Introduction. Exceptions: An OO Way for Handling Errors. Common Runtime Errors. Error Handling. Without Error Handling Example 1

BBM 102 Introduction to Programming II Spring Exceptions

More on Exception Handling

More on Exception Handling

Full file at Chapter 2 - Inheritance and Exception Handling

Object Oriented Programming

ECE 122. Engineering Problem Solving with Java

BBM 102 Introduction to Programming II Spring 2017

C16b: Exception Handling

Exception Handling in Java

CS 11 java track: lecture 3

Exception Handling CSCI 201 Principles of Software Development

COE318 Lecture Notes Week 10 (Nov 7, 2011)

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories


EXCEPTIONS. Java Programming

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Programming II (CS300)

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

Exception-Handling Overview

Object Oriented Programming

National University. Faculty of Computer Since and Technology Object Oriented Programming

Download link: Java Exception Handling

Correctness and Robustness

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

CS159. Nathan Sprague

Object Oriented Programming Exception Handling

CSC 1214: Object-Oriented Programming

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

EXCEPTION-HANDLING INTRIVIEW QUESTIONS


COMP-202 Unit 9: Exceptions

For more details on SUN Certifications, visit

COMP-202 Unit 9: Exceptions

CSCI 261 Computer Science II

CS159. Nathan Sprague

Std 12 Lesson-10 Exception Handling in Java ( 1

Programming II (CS300)

Introduction to Software Design

Data Structures. 02 Exception Handling

Recitation 3. 2D Arrays, Exceptions

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Chapter 12 Exception Handling

Unit 5 - Exception Handling & Multithreaded

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

COMP 213. Advanced Object-oriented Programming. Lecture 17. Exceptions

Exception Handling. Exception Handling

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

CS1020 Data Structures and Algorithms I Lecture Note #8. Exceptions Handling exceptional events

Here is a hierarchy of classes to deal with Input and Output streams.

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

14. Exception Handling

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Exception Handling. Handling bad user input. Dr. Siobhán Drohan Maireád Meagher. Produced by:

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Exceptions Handling Errors using Exceptions

WOSO Source Code (Java)

Lecture 28. Exceptions and Inner Classes. Goals. We are going to talk in more detail about two advanced Java features:

Internal Classes and Exceptions

Java Errors and Exceptions. Because Murphy s Law never fails

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

ITI Introduction to Computing II

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

INHERITANCE Mrs. K.M. Sanghavi

Class, Variable, Constructor, Object, Method Questions

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

ITI Introduction to Computing II

Chapter 13 Exception Handling

Programming - 2. Common Errors

CH. 2 OBJECT-ORIENTED PROGRAMMING

Unit III Rupali Sherekar 2017

What are Exceptions?

Fundamentals of Object Oriented Programming

Chapter 11 Exception Handling

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Exception in thread "main" java.lang.arithmeticexception: / by zero at DefaultExceptionHandling.main(DefaultExceptionHandling.

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

CO Java SE 8: Fundamentals

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Exceptions - Example. Exceptions - Example

CS 209 Programming in Java #10 Exception Handling

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

UNIT - V. Inheritance Interfaces and inner classes Exception handling Threads Streams and I/O

EXCEPTION HANDLING. Summer 2018

CS Programming Language Java. Fall 2004 Sept. 29

6.Introducing Classes 9. Exceptions

Transcription:

INDIAN INSTITUTE OF TECHNOLOGY ROORKEE Fundamentals of Object Oriented Programming CSN- 103 Dr. R. Balasubramanian Associate Professor Department of Computer Science and Engineering Indian Institute of Technology Roorkee Roorkee 247 667 balarfcs@iitr.ac.in https://sites.google.com/site/balaiitr/

protected in Java http://goo.gl/qsjyto 2

http://goo.gl/gkwslc 3

protected http://goo.gl/kfcqjx 4

Access Modifiers (Revisit) Lecture 35: Access Modifiers, Method overriding, Run Time Polymorphism (25/10/18) 5

Role of Private Constructor class A{ private A(){}//private constructor void msg(){system.out.println("hello java");} } public class Simple{ public static void main(string args[]){ A obj=new A();//Compile Time Error } } 6

default access modifier //save by A.java package pack; class A{ void msg(){system.out.println("hello");} } //save by B.java package mypack; import pack.*; class B{ public static void main(string args[]){ A obj = new A();//Compile Time Error obj.msg();//compile Time Error } } 7

protected access modifier The protected access modifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class. 8

protected access modifier http://goo.gl/ftwfjy 9

10

public access modifier Done in Lecture 35, Slide nos. 4 and 5 12

Visibility Modifiers Accessible to: public protected Package (default) private Same Class Yes Yes Yes Yes Class in package Yes Yes Yes No Subclass in different package Non-subclass different package Yes Yes No No Yes No No No 13 13

Java access modifiers with method overriding If you are overriding any method, overridden method (i.e. declared in subclass) must not be more restrictive. http://goo.gl/clshtt 14

Interface in Java An interface in java is a blueprint (a design plan) of a class. It has static constants and abstract methods only. The interface in java is a mechanism to achieve fully abstraction. There can be only abstract methods in the java interface not method body. It is used to achieve fully abstraction and multiple inheritance in Java. 15

Why use Java interface? It is used to achieve fully abstraction. By interface, we can support the functionality of multiple inheritance. 16

interface 17

Understanding relationship between classes and interfaces 18

interface http://goo.gl/ugxxlg 19

Multiple inheritance in Java by interface 20

Multiple inheritance in Java by interface http://goo.gl/noohds 21

Multiple inheritance is not supported through class in java but it is possible by interface, why? it is supported in case of interface because there is no ambiguity as implementation is provided by the implementation class. 22

Multiple inheritance http://goo.gl/vmck75 23

Interface inheritance http://goo.gl/x4pqpf 24

Nested Interface in Java An interface can have another interface i.e. known as nested interface. interface printable{ void print(); interface MessagePrintable{ void msg(); } } 25

Difference between abstract class and interface Abstract class 1) Abstract class can have abstract and nonabstract methods. 2) Abstract class doesn't support multiple inheritance. Interface Interface can have only abstract methods. Interface supports multiple inheritance. 3) Abstract class can have final, non-final, static and non-static variables. 4) Abstract class can have static methods, main method and constructor. Interface has only static and final variables. Interface can't have static methods, main method or constructor. 26

Difference between abstract class and interface 5) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class. 6) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface. 7) Example: public abstract class Shape{ public abstract void draw(); } Example: public interface Drawable{ void draw(); } 27

Exception Handling in Java The exception handling in java is one of the powerful mechanisms to handle the runtime errors so that normal flow of the application can be maintained. In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime. 28

Introduction: Errors in coding are categorised as: syntax errors compilation errors. Semantic/logical errors leads to programs producing unexpected outputs. runtime errors most often lead to abnormal termination of programs or even cause the system to crash. 29

Common Runtime Errors: Dividing a number by zero. Accessing an element that is out of bounds of an array. Trying to store incompatible data elements. Using negative value as array size. Trying to convert from string data to a specific data value (e.g., converting string abc to integer value). File errors: opening a file in read mode that does not exist or no read permission Opening a file in write/update mode which has read only permission.... 30

Without Error Handling Example class NoErrorHandling{ public static void main(string[] args){ int a,b; a = 7; b = 0; Program does not reach here } } System.out.println( Result is + a/b); System.out.println( Program reached this line ); No compilation errors. While running it reports an error and stops Without executing further statements: java.lang.arithmeticexception: / by zero at Error2.main(Error2.java:10) 31

Advantage of exception handling The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling. Let's take a scenario: statement 1; statement 2; statement 3; statement 4; statement 5;//exception occurs statement 6; statement 7; statement 8; 32

Exceptions What are they? An exception is a representation of an error condition or a situation that is not the expected result of a method. Exceptions are built into the Java language and are available to all program code. Exceptions isolate the code that deals with the error condition from regular program logic. 33

34

Types of Exception There are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception. 1) Checked Exception The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.ioexception, SQLException etc. Checked exceptions are checked at compile-time. 2) Unchecked Exception The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime. 35

Checked Exception 36

Checked Exceptions 37

Common scenarios where exceptions may occur 1) Scenario where ArithmeticException occurs int a=50/0;//arithmeticexception http://goo.gl/l07yxg 38

2) Scenario where NullPointerException occurs http://goo.gl/oordjb 39

3) Scenario where NumberFormatException occurs http://goo.gl/xwojk6 40

4) Scenario where ArrayIndexOutOfBoundsException occurs http://goo.gl/z8onuc 41

Java Exception Handling Keywords There are 5 keywords used in java exception handling. try catch finally throw throws 42

Exception-Handling: Java exception handling is managed by via five keywords: try, catch, throw, throws, and finally. Program statements to monitor are contained within a try block. If an exception occurs within the try block, it is thrown. Code within catch block catch the exception and handle it. System generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throws clause. 43

44

45

Exception Handling Mechanism try Block Statements that causes an exception Throws exception Object catch Block Statements that handle the exception 46

Syntax of Exception Handling Code try { // statements } catch( Exception-Type e) { // statements to process exception }.... 47

Use of try catch http://goo.gl/rvmzju 48 48

http://goo.gl/rvmzju 49

Internal working of java try-catch block 50

Multiple catch Clauses In some cases, more than one exception could be raised by a single piece of code. To handle this type of situation, you can specify two or more catch clauses, each catching a different type of exception. When an exception is thrown, each catch statement is inspected in order, and the first one whose type matches that of the exception is executed. After one catch statement executes, the others are bypassed, and execution continues after the try/catch block. The example given in next slide traps three different exception types: 51

Multiple Catching in JAVA http://goo.gl/jnz92x 52

Multiple Catching in JAVA At a time only one Exception is occurred and at a time only one catch block is executed. All catch blocks must be ordered from most specific to most general i.e. catch for ArithmeticException must come before catch for Exception. 53

Multiple Catching in JAVA http://goo.gl/lvxb0m 54

55