Beruflich Dokumente
Kultur Dokumente
Objectives: Following lecture cover concept of abstraction, abstract class, abstract functions, key points of
abstract class and an example with ambiguity and its solution using abstraction. Search
Packages are used in Java in order to prevent naming conflicts, to control access, to make
searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.
A Package can be defined as a grouping of related types (classes, interfaces, enumerations and
annotations) providing access protection and namespace management. This lecture covers
definition of exception, understanding of exception using example then hierarchy of exception
class and techniques to handle exception using try catch mechanism and explanation in detail.
Content: This lecture covers introduction of inheritance, types of inheritance, benefits of inheritance,
method overriding concept and demonstration of key word ‘super’.
.
Methods: Following lecture cover concept of abstraction, abstract class, abstract functions, key points of
abstract class and an example with ambiguity and its solution using abstraction. Search
Packages are used in Java in order to prevent naming conflicts, to control access, to make
searching/locating and usage of classes, interfaces, enumerations and annotations easier, etc.
A Package can be defined as a grouping of related types (classes, interfaces, enumerations and
annotations) providing access protection and namespace management. This lecture covers
definition of exception, understanding of exception using example then hierarchy of exception
class and techniques to handle exception using try catch mechanism and explanation in detail.
Resources: Besides the lecture handout, this lesson will draw from the following Text books: Java - The
Complete Reference, 9th Edition - Herbert Schildt;
Evaluation: In this lecture I will evaluate them by drawing an example on the board and ask them one by
one to write possible output.
Lecture
Contents of Lecture Strategy
(Remarks)
Abstraction in Java
Abstraction is a process of hiding the implementation details and showing only
functionality to the user. Another way, it shows only important things to the user
and hides the internal details for example sending sms, you just type the text and
send the message. You don't know the internal processing about the message
delivery.
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
double area() {
System.out.println("Area for Figure is
undefined.");
return 0;
ZA/Lesson 12 -2- Modern Programming Language (CS-432)
}
}
Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
//abstract method
abstract double area();
}
}
}
Packages are used in Java in order to prevent naming conflicts, to control access, to
int id,age;
float salary;
Interfaces in Java
Like a class, an interface can have methods and variables, but the methods declared
in interface are by default abstract (only method signature, no body).
• Interfaces specify what a class must do and not how. It is the blueprint of the
class.
• An Interface is about capabilities like a Player may be an interface and any
class implementing Player must be able to (or must implement) move(). So,
it specifies a set of methods that the class has to implement.
• If a class implements an interface and does not provide method bodies for all
functions specified in the interface, then class must be declared abstract.
// Driver Code
public static void main (String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
Output:
Geek
10
A real-world example:
Let’s consider the example of vehicles like bicycle, car, bike………, they have
common functionalities. So, we make an interface and put all these common
functionalities. And lets Bicylce, Bike, car ….etc implement all these functionalities
in their own class in their own way.
import java.io.*;
interface Vehicle {
int speed;
int gear;
// to change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
@Override
public void speedUp(int increment){
// to decrease speed
@Override
public void applyBrakes(int decrement){
int speed;
int gear;
// to change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
@Override
ZA/Lesson 12 -9- Modern Programming Language (CS-432)
public void speedUp(int increment){
// to decrease speed
@Override
public void applyBrakes(int decrement){
}
class GFG {
System.out.println("Bicycle present
state :");
bicycle.printStates();
Another feature that was added in JDK 8 is that we can now define static methods in
interfaces which can be called independently without an object. Note: these methods
are not inherited.
//An example to show that interfaces can
//have methods from JDK 1.8 onwards
interface in1
{
final int a = 10;
static void display()
{
System.out.println("hello");
}
}
hello
Exception-Handling Fundamentals
A Java exception is an object that describes an exceptional (that is, error) condition
that has occurred in a piece of code. When an exceptional condition arises, an object
representing that exception is created and thrown in the method that caused the
error. That method may choose to handle the exception itself or pass it on. Either
way, at some point, the exception is caught and processed. Exceptions can be
generated by the Java run-time system, or they can be manually generated by your
code. Exceptions thrown by Java relate to fundamental errors that violate the rules
of the Java language or the constraints of the Java execution environment. Manually
generated exceptions are typically used to report some error condition to the caller
of a method. Java exception handling is managed via five keywords: try, catch,
throw, throws, and finally. Briefly, here is how they work. Program statements that
you want to monitor for exceptions are contained within a try block. If an exception
occurs within the try block, it is thrown. Your code can catch this exception (using
catch) and handle it in some rational manner. System-generated exceptions are
automatically thrown by the Java runtime 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. Any code that absolutely must be executed
after a try block completes is put in a finally block.
This is the general form of an exception-handling block:
try {
// block of code to monitor for errors
} catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
} catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
finally {
// block of code to be executed after try block ends
ZA/Lesson 12 -12- Modern Programming Language (CS-432)
}
Exception Hierarchy
All exception classes are subtypes of the java.lang.Exception class. The exception
class is a subclass of the Throwable class. Other than the exception class there is
another subclass called Error which is derived from the Throwable class.
Errors are abnormal conditions that happen in case of severe failures, these are not
handled by the Java programs. Errors are generated to indicate errors generated by
the runtime environment. Example: JVM is out of memory. Normally, programs
cannot recover from errors.
The Exception class has two main subclasses: IOException class and
RuntimeException Class.
2. Text file of Execute able code and hand writing code after scanning will be submitted through
email