15-nov-15 classes and objects in java. 2 classes and objects a java program consists of one or more...

Download 15-Nov-15 Classes and Objects in Java. 2 Classes and Objects A Java program consists of one or more classes A class is the description; while objects

Post on 13-Jan-2016

217 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

  • Classes and Objects in Java

  • Classes and ObjectsA Java program consists of one or more classesA class is the description; while objects are actual examplesHere is an example class:class Dog { ...description of a dog goes here... }Here are some objects of that class:

  • More ObjectsHere is another example of a class:class Window { ... }Here are some examples (objects) of Windows:

  • Classes are like blueprintsWhile the house made from the blueprint is the object

  • Classes contain data definitionsClasses describe the data held by each of its objectsExample:class Dog { String name; int age; ...rest of the class... }

  • Classes contain methodsA class may contain methods that describe the behavior of objectsExample:class Dog { ... void bark() { System.out.println("Woof!"); } }

    When we ask a particular Dog to bark, it says Woof!Only Dog objects can bark; the class Dog cannot bark

  • Methods contain statementsA statement causes the object to do something(A better word would be commandbut it isnt)Example:System.out.println("Woof!");This causes the particular Dog to print (actually, display on the screen) the characters Woof!

  • Methods may contain temporary dataData described in a class exists in all objects of that classExample: Every Dog has its own name and ageA method may contain local temporary data that exists only until the method finishesExample:void wakeTheNeighbors( ) { int i = 50; // i is a temporary variable while (i > 0) { bark( ); i = i 1; } }

  • Classes always contain constructorsA constructor is a piece of code that constructs, or creates, a new object of that classIf you dont write a constructor, Java defines one for you (behind the scenes)The constructor always has the same name as your class.Example:class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } }

  • ConstructorsYou can have multiple constructorsExample:class Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } Dog(int age) { name = UNKNOWN; this.age = age; } Dog() { name = UNKNOWN; age = 0; }}

    There are 3 constructors:It will construct an object based on the # of arguments.

  • Diagram of program structureA program consists of one or more classesTypically, each class is in a separate .java file

  • SummaryA program consists of one or more classesA class is a description of a kind of objectIn most cases, it is the objects that do the actual workA class describes data, constructors, and methodsAn objects data is information about that objectAn objects methods describe how the object behavesA constructor is used to create objects of the classMethods (and constructors) may contain temporary data and statements (commands)

  • Getting startedQuestion: Where do objects come from?Answer: They are created by other objects.Question: Where does the first object come from?Answer: Programs have a special main method, not part of any object, that is executed in order to get things startedpublic static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); // creates a Dog }

  • A complete programclass Dog { String name; int age; Dog(String n, int age) { name = n; this.age = age; } void bark() { System.out.println("Woof!"); } } public class Kennel{public static void main(String[ ] args) { Dog fido = new Dog("Fido", 5); fido.bark(); }

    } // ends the class