Classes can inherit from another class. This is accomplished by putting a colon after the class name when declaring the class, and naming the class to inherit from—the base class—after the colon, as follows:
public class Point
{
//----class properties------//
private int x{set;get;}
private int y{set;get;}
//----default constructor----//
public Point(){
}
//custom constructor
public Point(int a,int b){
this.x=a;
this.y=b;
}
//----class methods-----//
public void getX(){
return this.x;
}
public void getY(){
return this.y;
}
public int setX(int a){
this.x=a;
}
public int setY(int b){
this.y=b;
}
}
//Line class is Inherited from Point class here
public class Line : Point
{
private Point p1{set;get;}
private Point p2{set;get;}
public Point(){
}
public Point(Point a,Point b){
this.p1=a;
this.p2=b;
}
...
}
The new class—the derived class—then gains all the non-private data and behavior of the base class in addition to any other data or behaviors it defines for itself. The new class then has two effective types: the type of the new class and the type of the class it inherits.
Comments 0