The following topics have been completed in the class:
Introduction to Programming
OOP vs POP, C vs C++, C vs Java
Properties of OOP and Java
Data Types & Keywords in Java
Class & Objects
Constructor & Encapsulation
Polymorphism
Inheritance
Keywords: super, this, final & abstract
Abstraction & Interface
Keywords: Super, This, Abstract & Final
These all keywords are reserved in java; we cannot use these words as variable or identifier.
super: it is used to refer superclass’s instance as well as static member.
this: it is used to refer current-class’s instance as well as static member.
Example:-
class Rectangle
{
int i=10,j=20; //assume the values
int getarea()
{
int l=this.i; //get reference of length form current class
int b=this.j; //get reference of breadth form current class
return l*b;
}
}
class Box extends Rectangle
{
int i=5; //assume the value
int getvolume()
{
int a=super.getarea(); //get area from parent class
int h=this.i; //get height from current class
return a*h;
}
}
class VolandArea extends Box
{
public static void main(String... s)
{
VolandArea va = new VolandArea();
va.find();
}
void find()
{
int ar=super.getarea(); //get area from Rectangle parent class
int v=super.getvolume(); //get volume from Box parent class
System.out.print("Area: "+ar+" "+"Volume: "+v);
}
}
-- Static member cannot refer to this or super in any way.
-- super may only be used within a subclass constructor method.
-- The parameter in the super call must match the order and type of the instance variable declared in the superclass.
abstract: it is used for abstraction. It can be a class or a method.
Abstraction is one of the properties of java. It means “Hide the functionality and show the name”.
final: it is used to prevent the access from anywhere, it can only be used as copy. It can be a class or a method or a variable. By using this keyword, we make an instance as constant, it means it cannot be inherit or override or change.
There may be necessary in some situations to restrict the access to certain variables and methods from outside the class. This is achieved by applying visibility modifiers to the instance variables and methods. The visibility modifiers are also known as access modifiers. There are following access modifiers:
public Access
protected Access
friendly (default) Access
private protected Access
private Access
As we know that A Strange thing cannot be created without any mistakes. So, in programming, a program cannot be created without any errors. These errors can be created by programmer or user.
In computer programming, errors are of two types:-
Compile-time error
Run-time error
Compile-time errors are usually generated by programmer, and it can be solved at programming level by the programmer. Example: missing semicolons, missing brackets, mismatch brackets, wrong use of reserved keywords... etc.
Run-time errors are usually generated after the compilation successfully of program. It can be system-error. It is also called Exceptions. And it is also generated by user.
Compile-time Errors
All syntax errors will be detected and displayed by the Java compiler and therefore those errors are known as compile-time errors. Most of the compile-time errors are due to typing mistakes. The most common errors are:
- Missing semicolons
- Missing or mismatch the brackets
- Wrong spelling of reserved keywords
- Missing double quotes in string
- Use of undeclared variables
- Bad references to the objects
- Use of operators wrongly
- And so on…
Run-time Errors
After compilation the program successfully, a program may produce wrong results due to wrong logic or may terminate due to errors, these errors are known as run-time errors. The most common errors are:
- Dividing an integer by zero
- Find the square root of negative number
- Accessing an element that is out of the bounds of an array
- Trying to store a value into an array of incompatible type
- Trying to illegally change the state of a thread
- Attempting to use a negative size for an array
- Converting invalid string to a number
- And so on…
An exception is a condition that is caused by a run-time error in the program. When the java interpreter encounters an error, it creates an exception object and throws (inform) it. If we want to prevent the error message at run-time, then this process is known as exception handling.
Error handling performs the following tasks:
Find the problem (Hit the exception)
Inform that an error has occurred (Throw the exception)
Receive the error information (Catch the exception)
Take corrective actions (Handle the exception)