Top Banner
Introduction to Objects A way to create our own types
32

Introduction to Objects

Jan 16, 2016

Download

Documents

karan

Introduction to Objects. A way to create our own types. Type and Variables. Until this point we were only able to create simple types, actually call “primitive” types integer double float char String (actually not primitive). We want to do better. Bank Account Math Calculator Stock - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Introduction to Objects

Introduction to ObjectsA way to create our own types

Type and VariablesUntil this point we were only able to create simple types, actually call primitive typesintegerdoublefloatcharString (actually not primitive)

We want to do betterBank AccountMath CalculatorStock CarVending machineTelephone

Model whatever as softwareMake a computer act like one

Use type to create a variableUse class to create an objectint x;Circle mycircle = new Circle();

Consider methodsTake a known processPackage it for reuseif ((a >= b) && (a>=c)) max = aelse if ((b >= a) && (b>=c)) max = belse max = c;int maxof3(int a, int b, int c){ int max; if ((a >= b) && (a>=c)) max = a else if ((b >= a) && (b>=c)) max = b else max = c; return max;}

How are object like this?Take a group of methods and data and package those for reuse.a( )b( )c( )int iint ka( )b( )c( )int iint kclass Testclass

int i;int j;void a(){}void b(){}void c(){}class TestClass{int i;int j;

void a() {}

void b() {}

void c() {}

}

How do we decide what goes inside?The problem will guide us.The things we put inside will define What the object will do How we can interact with it

These things will be the Bank Accounts, Students, etc

Lets start simpleA circle

What are some of the attributes of a circle?Radius (most obvious)ColorBorderPosition

How do we interact with a circle?Change its sizeMove itAsk it for its area depending on the problems needs

Lets start with a simple Circle classJust a radiusNo borders or colorsA means of asking it for its 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 thisarea = radius*radius*Math.PI; return thisarea; }

}

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 thisarea = radius*radius*Math.PI; return thisarea; }

}A property ofeach circle

Circle()class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; }

}A method named Areathat will calculate thearea of that specific circle

Circle()class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; }

}A constructorUsed to initialize the circleLets see how in the next slide

Creating circlesCircle circle1 = new Circle(10);Radius:10circle1

Creating circlesCircle circle1 = new Circle(10);radius:10circle1Circle circle2 = new Circle(15);radius:15circle2

Asking about areaCircle circle1 = new Circle(10);System.out.println(circle1.Area());radius:10circle1Circle circle2 = new Circle(15);System.out.println(circle2.Area());radius:15circle2Each circle will return its own area

Whats the difference?Circle circle1 = new Circle(10);

Circle circle1;Creates a REFERENCELike having a telephone number for a friend.. a means to find them. Butthis one is a contact without a number.

Whats the difference?Circle circle1 = new Circle(10);

Circle circle1;Creates the objectand defines the reference to the objectIn this case, circle1 actually refers to a realCircle.

Only a reference.. No circleCircle circle1;circle1

A reference with a circleCircle circle1 = new Circle(10);Radius:10circle1

Put it Together!public class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String[ ] args) { Circle circle1 = new Circle(10); System.out.println(circle1.Area());

Circle circle2 = new Circle(15); System.out.println(circle2.Area()); }

}314.1562..706.8583..

Do I need new()?Can I define a variable and just reference from the main?How about this? TRY ITpublic class Circle() {

double radius=5;

public static void main(String args[]) { System.out.println(radius); }

}Error: non-static variable radius cannotbe referenced from a static context

You never new()ed one. No radius exists.

Only a reference.. No circleCircle circle1;circle1

Do I need new()?Here there is no Area() or radius defined.Because NO new() has occurred!public class Circle() {

double radius=5;

double Area() { double this area = radius*radius*Math.PI; }

public static void main(String args[]) { System.out.println(Area()); }

}Error : nonstatic method can not be referenced from static method

This one creates the object.Then a radius and Area() exists to use no errors.public class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { Circle circle1 = new Circle(10); System.out.println(circle1.Area());

Circle circle2 = new Circle(15); System.out.println(circle2.Area()); }

}These are created when you new()

public class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { int i = 5; System.out.println(i);

Circle circle1 = new Circle(10); System.out.println(circle1.Area()); }

}Why can I declare ilike this in the main,but not radius in the previous example?This is legal!

public class Circle {

double radius;

Circle(double r) { radius = r; }

double Area() { double thisarea = radius*radius*Math.PI; return thisarea; } public static void main(String args[]) { int i = 5; System.out.println(i);

Circle circle1 = new Circle(10); System.out.println(circle1.Area()); }

}Because main is static.Static needs more explanation.

Conclusion:

new before using

static is coming!