In this lesson, we will learn how to define Object methods. An Object is a Bundle of StateAnd Behavior. Data Fields Define the State, Methods Define the behavior. Check out this program. The Rectangle class has fields width and height And A constructor. In the main method, A new Object is created
we've added a method getWidth() to the Rectangle class. the method returns the value of the width field.
In the Main Method getWidth() is called on the New Rectangle Method with the dot notation. the object is passed implicitly as a parameter, which can be accessed by the keyword this inside the getwidth() method. Guess what the program prints?
The getWidth() method returns the value of 100 of the width field
now add a method getHeight() which returns The rectangle's height see the codes below
in the main method call getheight() and print the result
Add a method getArea() which calculates and returns the Rectangle Area.
in the main method call the getArea() Method
and print the result
0 Comments