Introduction to Objects What is a constructor?
Use type to create a variable Use class to create an object int x; Circle mycircle = new Circle ();
Let’s start simple A circle
What are some of the attributes of a circle? Radius (most obvious) Color Border Position
Let’s start with a simple Circle class Just a radius –No borders or colors A means of asking it for it’s area. This will serve as the basis (a type or class) for creating lots of circles
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; }
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } Heading for the class
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } A property of each circle
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } A method named Area that will calculate the area of that specific circle
Circle() class Circle { double radius; Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } A constructor Used to initialize the circle Let’s see how in the next slide -Name same as the class -No type
Creating circles Circle circle1 = new Circle(10); Radius:10 circle1
Creating circles Circle circle1 = new Circle(10); radius:10 circle1 Circle circle2 = new Circle(15); radius:15 circle2
What’s the difference? Circle circle1 = new Circle(10); Circle circle1; Creates a REFERENCE Like having a telephone number for a friend.. a means to find them. But this one is a contact without a number.
Creation requires a new new uses the constructor
Circle() a default constructor class Circle { double radius; Circle() { radius = 1.0; } Circle(double r) { radius = r; } double Area() { double this area = radius*radius*Math.PI; } Another constructor This constructor chooses a radius (1) for us.
Creating circles Circle circle1 = new Circle(); radius:1 circle1 Circle circle2 = new Circle(15); radius:15 circle2 default constructor
Circle() : more constructors class Circle { double radius,x,y; Circle() { radius = 1.0; x=0.0; y=0.0; } Circle(double r) { radius = r; x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r; x=xpos; y=ypos; } }
Circle() : more constructors class Circle { double radius,x,y; Circle() { radius = 1.0; x=0.0; y=0.0; } Circle(double r) { radius = r; x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r; x=xpos; y=ypos; } } Circle c1 = new Circle();
Circle() : more constructors class Circle { double radius,x,y; Circle() { radius = 1.0; x=0.0; y=0.0; } Circle(double r) { radius = r; x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r; x=xpos; y=ypos; } } Circle c2 = new Circle(10.0);
Circle() : more constructors class Circle { double radius,x,y; Circle() { radius = 1.0; x=0.0; y=0.0; } Circle(double r) { radius = r; x=0.0; y=0.0; } Circle(double r,double xpos, double ypos ) { radius = r; x=xpos; y=ypos; } } Circle c3 = new Circle(10.0,1.0,2.0);
As many constructors as you want!