You are on page 1of 7

Day2:

Abstraction
Abstract class - 0 - 100
abstract class AbsClass //100
{
abstract void method(); //abstract method
}
abstract class DerivedClass extends AbsClass
{
void newMethod(){}
}
-------
abstract class Absclass //0
{
void method() //non-abstract method
{
System.out.println("non-abstract method");
}
}

from Athma M to everyone: 12:40 PM

-------
abstract class AbsClass //50
{
void method1() //non-abstract method
{
System.out.println("non-abstract method");
}
abstract void method2(); //abstract method
}
class DerivedClass extends AbsClass
{
void method2(){
System.out.println("method2 in DerivedClass");
}
}
class TestClass
{
public static void main(String[] a)
{
AbsClass ab = new DerivedClass(); //new AbsClass(); XXXXX
ab.method2();
ab.method1();
}
}

from Athma M to everyone: 12:40 PM

Interface - 100
interface MyInterface
{
int method();
}
class DerivedClass implements I1,I2,...
{

}
interface I1
{
public abstract void m1();
}
interface I2 extends I1 //IS-A type of I1
{
//void m1();
int m2();
}
interface I3
{
int m3(int n);
}
interface I4 extends I1,I3
{
//void m1();
//int m3(int n);
int m4(intn1, int n2);
}

from Athma M to everyone: 12:41 PM


class DerivedClass implements I1
{
public void m1()
{

}
}
Encapsulation
hiding the data
private
accessors/mutators - public getter/setter
Employee - eid,name,sal

from Athma M to everyone: 12:41 PM

class Employee
{
private int eid;
private String name;
private int sal;
public int getEid(){ return this.eid; }
public void setEid(int eid){
//validate eid
this.eid = eid;
}
public String getName(){ return this.name; }
public void setName(String name){
//validate name
this.name = name;
}
public int getSal(){ return this.sal; }
public void setSal(int sal){
//validate sal
this.sal = sal;
}
}

from Athma M to everyone: 12:41 PM


Exception
unexpected event - affect normal execution
Exception
checked unchecked
compile runtime
IOEXception RuntimeException
FileNotFoundException ArithmeticException

from Athma M to everyone: 12:41 PM

throws - to declare Exception


void method() throws Exception
class Test
{
public static void main(String[] a)
{
int l = a.length;
if(l>1)
String str1 = a[0];
String str2 = a[1];
}
}
handle
try--catch--finally

from Athma M to everyone: 12:42 PM

try
{}
catch(Ex e){}
------
try{}
catch(Ex1 e1){}
catch(Ex2 e2){}
...
-------
Throwable
| |
Error Exception
cannot can be
handle handled

from Athma M to everyone: 12:42 PM

user defined/custom Exception:

class MyException extends Exception //IS-A type of Exception


{
public Myxception()
{
super();
}
public MyException(String msg)
{
super(msg);
}
public String toString()
{
return "MyException[ message ]";
}
}
**************
to raise/throw an exception/error
throw
-----
throw new Error();
or
throw new Exception();
throw new Exception("msg - regarding the exception");
**********************

from Athma M to everyone: 12:42 PM

DemoProgram

from Athma M to everyone: 12:42 PM

package excppkg;
public class TestException {
public static void main(String[] args) throws NumberLessThanTenException
{
try {
int n1 = Integer.parseInt(args[0]); // 10;
int n2 = Integer.parseInt(args[1]); // 5;
System.out.println("n1 = " + n1 + " n2= " + n2);
if(n1 < 10 || n2 < 10)
//throw new NumberLessThanTenException();
throw new NumberLessThanTenException("input foud to be less than 10");
else {
int result = n1 / n2;
System.out.println("Result = " + result);
}
}

from Athma M to everyone: 12:43 PM

catch (ArithmeticException arex) {


System.out.println(arex.toString());
System.out.println(arex.getMessage());
System.out.println("Division by zero not possible..!!");
}
catch(NumberFormatException nmfe)
{
System.out.println("Wrong input Format");
}
catch(ArrayIndexOutOfBoundsException arie)
{
System.out.println("Not enough input values");
}
catch(NumberLessThanTenException nlte)
{
System.out.println(nlte.getMessage());
}
catch(RuntimeException re)
{}
catch(Exception e){}

finally //always excecute


{
System.out.println("Program completed");
}
}
}

from Athma M to everyone: 12:43 PM

---------------
package excppkg;
public class NumberLessThanTenException extends Exception {
public NumberLessThanTenException() {
super();
}
public NumberLessThanTenException(String msg) {
super(msg);
}

public String toString() {


return "NumberLessThanTenException [input values found to be less than 10]";
}

}
*********************

You might also like