Professional Documents
Culture Documents
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");
}
}
-------
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();
}
}
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);
}
}
}
Encapsulation
hiding the data
private
accessors/mutators - public getter/setter
Employee - eid,name,sal
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;
}
}
try
{}
catch(Ex e){}
------
try{}
catch(Ex1 e1){}
catch(Ex2 e2){}
...
-------
Throwable
| |
Error Exception
cannot can be
handle handled
DemoProgram
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);
}
}
---------------
package excppkg;
public class NumberLessThanTenException extends Exception {
public NumberLessThanTenException() {
super();
}
public NumberLessThanTenException(String msg) {
super(msg);
}
}
*********************