Remember to check "Create project from template" so that the first class is already created for you
public class Shape {
public double area(){
return 0;
}
public void printInfo(){
System.out.println("just some random shape");
}
}
public class Main {
public static void main(String[] args) {
Shape randomShape = new Shape();
randomShape.printInfo();
System.out.println("The are of the shape is: " + randomShape.area());
}
}
Shape randomShape = new Shape();
public double area(){}
A method has:
We will create the subclasses Circle and Rectangle that inherit from the Shape class
Create the new class Circle and add the following code:
public class Circle extends Shape{
private double radius;
private final double PI = Math.PI;
public Circle(int r){
radius = r;
}
public double area() {
//Math.pow(radius, 2) = radius * radius
return PI * Math.pow(radius, 2);
}
public void printInfo(){
System.out.println("I am a circle and know the value of PI: " + PI);
}
}
Create the new class Rectangle and add the following code:
public class Rectangle extends Shape{
private double width;
private double length;
public Rectangle(int w, int l){
width = w;
length = l;
}
public double area(){
return width * length;
}
}
Modify the Main class in order to create 2 new objects besides the previously created Shape object:
public static void main(String[] args) {
Shape randomShape = new Shape();
randomShape.printInfo();
System.out.println("The are of the shape is: " + randomShape.area());
System.out.println(); //this is added only as a separator to clarify the output
Circle circle = new Circle(4);
circle.printInfo();
System.out.println("The are of the circle is: " + circle.area());
System.out.println();
Rectangle rectangle = new Rectangle(4, 5);
rectangle.printInfo();
System.out.println("The are of the rectangle is: " + rectangle.area());
}
Do you see the difference in output?
Check any of your classes from the Drawing project. You will see all have the first following line:
package com.base;
It defines the package the class is part of.
It is the first line of code in any class.
They are a way to organize your code in order to:
Packages in the Java language itself begin with java. or javax.
It's the same as creating a new class
The Main class was modified automatically and the following code was added:
import com.shape.Circle;
import com.shape.Rectangle;
import com.shape.Shape;
These 3 lines of code are called import statements
//import com.shape.Circle;
//import com.shape.Rectangle;
//import com.shape.Shape;
What is happening? Can you still compile and run your project?
import com.shape.Circle;
import com.shape.Rectangle;
import com.shape.Shape;
Replace the import statements:
with:
import com.shape.*;
default is what you get when there is no access modifier
In our examples all our class members have been public, but this is not good practice
Make class Circle private
What happens? Check the messages from your IDE. Can you still compile the code?
The IDE knows
Make the method area in class Shape private
Remove the access modifier of class Shape.
What happens to the code in the Main class?
Is the project still compiling? Why?