Inheritance in Java
Inheritance in Java
1
public class GeometricObject {
private String color = "RED";
private boolean filled;
private java.util.Date creationDate;
public GeometricObject() {
creationDate = new java.util.Date();
}
@Override
public String toString() { /* The toString() method returns a string
representation of the object.*/
return "Created on: "+creationDate;
}
2
public class Circle extends GeometricObject{
private double radius;
}
/*******************************************************************************/
public class Rectangle extends GeometricObject {
3
public class Test {
public static void main(String[] args) {
Circle c = new Circle(14);
System.out.println("A circle: "+ c.toString());
System.out.println("The color is: "+ c.getColor());
System.out.println("Is filled: "+c.isFilled());
System.out.println("The area: "+c.getArea());
4
It can be used in two ways:
.
.
public Circle(double radius, String color, boolean filled, Date creationDate) {
super(color, filled, creationDate);
this.radius = radius;
}
.
.
5
public class ConstructorChaining {
public static void main(String[] args) {
new employee();
}
}
class person{
public person(){
System.out.println("1- Person Here");
}
}