0% found this document useful (0 votes)
43 views

Inheritance in Java

The document describes a GeometricObject superclass with common properties and methods for shape objects like Circle and Rectangle subclasses. It shows constructor chaining where subclasses call super class constructors to initialize common properties before initializing subclass specific properties. The Test class creates Circle and Rectangle objects and outputs their properties.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Inheritance in Java

The document describes a GeometricObject superclass with common properties and methods for shape objects like Circle and Rectangle subclasses. It shows constructor chaining where subclasses call super class constructors to initialize common properties before initializing subclass specific properties. The Test class creates Circle and Rectangle objects and outputs their properties.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ABSTRACT

Eng. Mustafa J. Dalloul


T.A. at Computer Engineering Dept.,
IUG
Superclass
Common Properties & methods

extend extend extend

Supclass A Supclass B Supclass C

1
public class GeometricObject {
private String color = "RED";
private boolean filled;
private java.util.Date creationDate;

public GeometricObject() {
creationDate = new java.util.Date();
}

public GeometricObject(boolean filled, Date creationDate) {


this.filled = filled;
this.creationDate = creationDate;
}

public String getColor() {


return color;
}

public void setColor(String color) {


this.color = color;
}

public boolean isFilled() {


return filled;
}

public void setFilled(boolean filled) {


this.filled = filled;
}

public Date getCreationDate() {


return creationDate;
}

public void setCreationDate(Date creationDate) {


this.creationDate = creationDate;
}

@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 Circle(double radius) {


this.radius = radius;
}

public Circle(double radius, String color, boolean filled, Date creationDate) {


super(color, filled, creationDate);
this.radius = radius;
}

public double getRadius() {


return radius;
}

public void setRadius(double radius) {


this.radius = radius;
}

public double getArea(){


return radius*radius*Math.PI;
}

}
/*******************************************************************************/
public class Rectangle extends GeometricObject {

private double width, height;

public Rectangle(double width, double height) {


this.width = width;
this.height = height;
}

public void setHeight(double height) {


this.height = height;
}

public void setWidth(double width) {


this.width = width;
}

public double getHeight() {


return height;
}

public double getWidth() {


return width;
}

public double getArea(){


return width*height;
}
}

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());

Rectangle r = new Rectangle(200, 300);


System.out.println("A rectangle: "+ r.toString());
System.out.println("The color is: "+ r.getColor());
System.out.println("Is filled: "+r.isFilled());
System.out.println("The area: "+r.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");
}
}

class stuff extends person {


public stuff(){
this("2- Invoke Stuff's overloaded constructor");
System.out.println("3- Stuff Here");
}
public stuff(String message){
System.out.println(message);
}
}

class employee extends stuff{


public employee(){
System.out.println("4- Employee Here");
}
}

You might also like