inheritance
hacks for clases
Popcorn Hacks
public class Shape {
protected String name;
private int length;
private int width;
// Default constructor
public Shape() {
this.name = "Shape";
this.length = 10;
this.width = 5;
}
// Parameterized constructor
public Shape(String name, int length, int width) {
this.name = name;
this.length = length;
this.width = width;
}
// Getter methods
public String get_name() {
return this.name;
}
public int get_length() {
return this.length;
}
public int get_width() {
return this.width;
}
// Setter methods
public void set_name(String n) {
this.name = n;
}
public void set_length(int a) {
this.length = a;
}
public void set_width(int b) {
this.width = b;
}
}
class Circle extends Shape{
private int radius;
Circle() {
this.name = "Circle";
this.radius = 1;
}
Circle(String name, int radius) {
this.name = name;
this.radius = radius;
}
public double get_area(){
double rad = (double)this.radius;
return Math.PI * Math.pow(rad,2);
}
}
class Hexagon extends Shape{
private int sideLength;
Hexagon() {
this.name = "Circle";
this.sideLength = 1;
}
Hexagon(String name, int sideLength) {
this.name = name;
this.sideLength = sideLength;
}
public double get_area(){
double side = (double)this.sideLength;
return 3*Math.sqrt(3)/2 * Math.pow(side,2);
}
}
System.out.println((new Circle("my Circle", 1)).get_area());
System.out.println((new Hexagon("my Hexagon", 2)).get_area());
3.141592653589793
10.392304845413264
class Bird{
public String name;
Bird(String name){
this.name = name;
}
}
class Parrot extends Bird{
Parrot(String name){
super(name);
}
}
void printBirdStuff(Parrot parr){
System.out.println(parr.name);
}
Bird myParrot = new Parrot("Parrot!");
printBirdStuff((Parrot)myParrot);
Parrot!