150 likes | 541 Views
Head First Java – Ch. 2. Classes v. Objects. Page 34. What is a Class? – 2 major pieces Things that the Class Knows – Instance Variables Things that the Class Does – Methods Television Class – specific Manufacturer; size; color; pixel quality Television Method
E N D
Head First Java – Ch. 2 Classes v. Objects
Page 34 • What is a Class? – 2 major pieces • Things that the Class Knows – Instance Variables • Things that the Class Does – Methods • Television Class – specific • Manufacturer; size; color; pixel quality • Television Method • Turn on, play a show or movie, turn off; adjust sound, etc.
Page 35 • Difference between a class and an object • A class is not an object. (but it’s used to construct them. • A class is a blueprint for an object • Tells the virtual machine how to make an object of that particular type • Example – a Dog – Dog Class – a blueprint that the dog objects will be made from • Methods – all dogs bark, drink, eat, poop, etc.
Examples • Rolodex cards – • Each has blank fields • Blueprint for a house - class • Object – houses built from blueprint.
Page 36 – Making your 1st Object • Dog Class – instance variables and a method • TestDrive class- will have main method – runs program • Class has instance variables and methods • At top of class you declare the variables • Next, create a method – thing the dog can do • In TestDrive; create a new Dog object • Dog d = new Dog(); //makes a dog object • d.size = 40; // uses dot operator to set size of dog. • d.bark(); //calls the bark method • The Dot Operator – page 36
Code the class and object All of the classes that you create objects from need to get compiled first. In example, dog class compiled first, then DogTestDrive. Dog class won’t run on it’s own – no main method Need to execute the class with the main method The method that runs is the Bark method
YouTube Demo • We will look at demo of code on LTC’s video