Classes, objects in JAVA

3,142 views
2,806 views

Published on

Published in: Education, Technology
1 Comment
1 Like
Statistics
Notes
  • i want solution of the EXAMPLE FOR CREATING CLASSES Design a class Account that stores customer name, account number, and type of account. Include necessary methods to achieve following tasks:-  Deposit money.  Display balance.  Permit withdrawal and update balance.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
3,142
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
219
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Classes, objects in JAVA

  1. 1. CLASSES, OBJECTS & METHODS Module-2
  2. 2. TOPICS TO COVER Introduction. Defining a class. Creating Objects. Accessing Class Members.
  3. 3. INTRODUCTION Underlying structure of each JAVA programs is CLASSES. CREATE CLASS FIELDS DATA ITEMS basic program OBJECTS METHODS FUNCTIONS Components CREATE OBJECTS METHODS
  4. 4. DEFINING A CLASS A class is a user-defined data type. Variables and Functions can be created within class SYNTAX EXAMPLE INSATNCE VARIABLESclass classname class area { { field declaration; Declaring variables int side;Instance variables are declared int length;exactly as LOCAL variables } method declaration;}
  5. 5. METHOD DECLARATION Without methods class has NO LIFE. Since objects created by such class cannot respond to any messages. Thus, methods are necessary for MANIPULATING DATA. SYNTAX EXAMPLE class areatype method-name(parameter list) { { int side; int length; void get(int s, int l) } { side = s; length = l; Type of the value the method returns. It can be } void, int, float, double }
  6. 6. EXAMPLE FOR CREATING CLASSES Design a class Account that stores customer name, account number, and type of account. Include necessary methods to achieve following tasks:-  Deposit money.  Display balance.  Permit withdrawal and update balance.
  7. 7.  An object in JAVA is essentially a block of memory that contains space to store all the instance variables. Creating an object also refers to INSTANTIATING AN OBJECT. Objects in JAVA are created using new. The new operator dynamically allocates memory for an object an returns a reference to it. Indicates that it does not point class area null Allocates at run-time to any object { area a1; a1 SYNTAX:- int side;classname objectname; a1 = new area(); int length;objectname = new classname(); void get(int s, int l) combined { a1 side = s; area a1 = new area(); length = l; }
  8. 8.  class area Values are to be assigned to variables in order to use them in { our programs. int side; int length; Since we are outside the class, we cannot access the instance void get(int s, int l) { variables and methods directly. side = s; length = l; Object and dot operator are used to do this. } area a1 = new area(); SYNTAX:- objectname.variablename = value; . a1 side = 10; . objectname.methodname(parameter-list); a1 get (10, 15);
  9. 9.  Constructors  Method Overloading Constructor Overloading  Nesting of methods
  10. 10.  JAVA allows objects to initialize themselves when they are created CONSTRUCTOR PROPERTIES:- • Initializes an object immediately upon creation. • Same name as the class in which it resides and syntactically similar to a method. • Is called automatically after the object is created. • Does not have return type. EXAMPLE
  11. 11. NO RETURN TYPE OUTPUT
  12. 12.  Methods have same name, but different parameter list . Is used when objects are required to perform similar tasks but using different input parameters. Also known as POLYMORPHISM. Here, the aim is to provide several method definitions all with same name, but different parameter lists. The difference may either in number or type of arguments Method’s return type does not play any role in this
  13. 13.  In addition to overloading methods, you can also overload constructor method EXAMPLE
  14. 14.  A method of a class can be called only by an object of that class using dot operator. A method can be called by using only its name by another method of the same class NESTING OF METHODS
  15. 15. STATIC MEMBERS Is used to define a member that is common to all objects and accessed without using a particular object. Thus member belongs to the class as a whole rather than the objects created from the class. Used when we want to have a variable common to all instances of a class.SYNTAX:-static int count; STATIC MEMBERS Referred to asstatic int max(int x, inty) Class variables and Class methods
  16. 16. EXAMPLE
  17. 17. RESTRICTIONS FACED BY STATIC METHODS:- STATIC METHODS ARE CALLED USING CLASS NAME They can only call other static methods. They can only access static data. They cannot refer to this or super in any way.
  18. 18. USING OBJECTS as PARAMETERS We know how to pass simple types as parameters to methods. It is possible, correct and common to pass OBJECTS to methods. EXAMPLE
  19. 19. CALL by VALUE vs. CALL by REFERENCECALL BY VALUE CALL BY REFERANCEThis method copies the value In this method, reference toof an argument into the an argument is passed to theformal parameter of the parameter.subroutineDoes not access actual This reference is used toargument access the actual argument.Thus, changes made to Thus, changes made toparameter of the subroutine parameter will have an effecthave no effect on the on the argument.argument.
  20. 20. EXAMPLE REMEMBE RCALL by VALUE:- Simpletype arguments are passed to methods.CALL by REFERENCE:-Objects are passed tomethods
  21. 21. RECURSION JAVA supports recursion. Recursion is the process of defining something in terms of itself. A method that calls itself is said to be recursive.
  22. 22. VISIBILITY CONTROL Visibility modifiers areused to restrict the access to certain variables and methods from outside the class. Also known as ACCESS MODIFIERS. Visibility Labels PUBLIC PROTECTED PRIVATE
  23. 23. VISIBILITY CONTROL (Contd..)PUBLIC Visible to entire class in which it is defined and All the class OutsidePRIVATE Enjoys highest degree of protection. Accessible only with their own class. Cannot be inherited, thus not accessible in sub-class.Friendly When no access modifier is specified then the default version of public accessibility is known as “FRIENDLY”PUBLICPROTECTED ItsFriendly between PUBLIC ACCESS & FRIENDLY level lies ACCESS.Makes fields visible in all Makes the fields visibleonly only to all classes and Makes fields visible not inclasses, regardless of their subclasses in same package but also to subclasses in the same package.packages other packages

×