Jun 1, 2007

Inheritance in JAVA

//inheritance : class a is a superclass and class b is subclass. Class a is inherited class and class b that is inheriting.

class a // superclass
{
void add()
{
int a,b,c;
a=10;
b=20;
c=a+b;
System.out.println(c);
}
};

// creating a subclass
class b extends a
{
void sub()
{
int a,b,c;
a=10;
b=20;
c=a-b;
System.out.println(c);
}
};

class demo
{
public static void main(String[] args)
{
b b1; // by object of subclass we can access member of superclass.
b1 = new b();
b1.add();
b1.sub();
}
}

No comments:

Post a Comment