1 / 9

Agenda

About Homework for BPJ lesson 15 Overloading constructor Overloading methods Calling constructor/method Encapsulation Homework. Agenda. Overloading constructor. Constructors can be overloaded to provide more options for instantiating an object. Dog() - default constructor

elda
Télécharger la présentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. About Homework for BPJ lesson 15 • Overloading constructor • Overloading methods • Calling constructor/method • Encapsulation • Homework Agenda

  2. Overloading constructor • Constructors can be overloaded to provide more options for instantiating an object. Dog() - default constructor { breed = “Huskie”; color = “black”; leg = 4; size= ‘S’; }

  3. Overloading constructor public Dog(String oBreed, String oColor) { breed = b; color = c; size = ‘L’; leg=4; } public Dog(String oBreed, String oColor, char oSize) { breed = oBreed; color = oColor; size = oSize; leg=4; }

  4. How do we know which constructor is being called??? Dog aDog = new Dog(); Dog bDog = new Dog(“Terrier”, “brown”); Dog cDog = new Dog(“chihuahua”, “white”, ‘s’);

  5. Overloading methods • Same method name with different number of parameters. public void eating(){ System.out.println(“eating dog food.”); } public void eating(String aFood){ System.out.println(“eating ” + aFood); }

  6. BankAccount class • Create a BankAccount class. • This class has the following instance variables: • Balance, account number, password • A constructor which have 3 parameters. • This class has the following methods: getBalance() deposit() withdraw()

  7. Encapsulation • Protecting an object’s data from code outside the class. • Those instance variables marked as private. • They can only be seen or modified through the use of public accessor and mutator methods. • Try type in the Cube class(next slide) in Eclipse and create a main(), instantiate a Cube object and type in <object>. to see the display list

  8. public class Cube{ private int length; public int breadth; private int height; public Cube() { length = 10; breadth = 10; height = 10; } public Cube(int l, int b, int h) { length = l; breadth = b; height = h; } public int getVolume() { return (length * breadth * height); } }

  9. Homework • BPJ Lesson 16 Project ..Gas mileage • Follow all the instructions to create an Automobile class and tester program

More Related