Java Classes, Objects, and Methods Overview

Download Presenatation
nazareth college of arts and science affiliated n.w
1 / 34
Embed
Share

Dive into the fundamentals of Java programming with a focus on classes, objects, and methods. Understand the core concepts and syntax, including field declarations and method definitions, essential for object-oriented programming in Java.

  • Java Programming
  • Object-Oriented
  • Classes
  • Methods

Uploaded on | 3 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. NAZARETH COLLEGE OF ARTS AND SCIENCE Affiliated To University Of Madras Re-accredited by NAAC with B grade WEB TECHNOLOGY UNIT II CLASSES OBJECTS AND METHODS CLASS :II B.SC CS SEMESTER: EVEN(2022-2023) STAFF NAME: MS.R.KAVIYARASI DEPARTMENT: COMPUTER SCIENCE

  2. 1.Classes objects and methods: Class is a central core of java programs. The class forms the basis for object oriented programming in Java. All type of concepts in java are implemented or encapsulated within a class. A class defines a new data type. A class is a template of an object. An object is an instance of a class. Classes provided a convenient method for packing together a group of logically related data items and functions that work on them. In java data items are called fields. Function are called methods The class defines the structure the state and behaviors of the program.

  3. Syntax: class classname { type variablename1; type variablename2; Fields declaration ....... ........ type variablenamen; type methodname1(parameter list) ) Method decleration { // body of the method; } }

  4. The data or variable defined inside the class are called instance variable or fields. The function defined inside the class are called as methods. The fields and methods are called as class methods.

  5. Field Declaration Data is encapsulated in a class by placing data fields inside the body of the class definition. The variables defined inside the class is called as instance variable or fields or member variables Syntax: class classname { datatype variablename1; datatype variablename2; ....... ........ datatype variablenamen; }

  6. Example class simple { int p,n,r,si; }

  7. Method declaration The function defines inside the class is called as method or member functions The methods are necessary for manipulating the data continued in the class. Syntax: type methodname1(parameter list) { // body of the method; }

  8. Method declaration has four parts: Name of the methods (method name) The type of the value the method returns(type) A list of parameter(parameter list) The body of the method

  9. Example void getdata(int a,int b,int c) { p=a; n=b; r=c; }

  10. Declaring objects: Declaring or objects of a class is a two step process We must declare a variable of the class type. We must acquire an actual physical copy of the object and assign it to that variable It is done by using the new operator. The new operator dynamically allocates at runtime memory for an object and returns a reference to it.

  11. Syntax: classname objectname; objectname= new constructname(); or classname objectname= new constructname(); Example: simple obj; obj=new simple(); or simple obj=new simple();

  12. Accessing class members: The member variable and the member function or the fields and the methods can be accessed by using .dot operator Syntax: 1) objectname.field=value. 2) objectname.methodname(); 3) objectname.methodname(parameter list); Example: 1. obj.p=2000 (or) 2. obj.getdata(); 3. obj.getdata(34,55,66);

  13. Example program: class simple { int p,n,r,si; } class main { public static void main (String args[]) { obj.p=2000; obj.n=2; obj.r=3; obj.si=obj.p*obj.n*obj.r/100; System.out.println("simple interest"+obj.si); } }

  14. Example 2: Fileds/methods(without arguments) class simple { int p,n,r,si; } void getdata() { p=2000; n=2; r=3; }

  15. void calc() { si=p*n*r/100; } void disp() { System.out.println("simple interest"+si); } Note: The value of the fields are assigned by using the objects.

  16. class main { public static void main (String args[]) { simple obj= new simple(); obj.getdata(); obj.calc(); obj.disp(); } }

  17. 2.Constructors Constructor as the name indicates it is used to construct the values to the variables. A constructor initializes an object immediately upon creation. Java allows objects to initialize themselves at the time of the objects is first created. This automatic initialization is performed through the use of constructors.

  18. Syntax: Constructor has the same name as the class name. It has no return type not even void. Note: The construct is invoked automatically when the object is created before the new operator completes. Types of constructors: Default constructor. parameterized constructor.

  19. Default constructor: The default constructor initialize values to the parameter. Syntax: class classname { type variablename1; .... type variablen; classname() { //body of the constructor }

  20. Example: class simple { int p,n,r,si; simple() { p=2000; n=2; r=3; } void calc() { si=p*n*r/100; }

  21. void disp() { System.out.println(si); } } class main1 { public static void main(String args[]) { simple obj= new simple(); obj.calc(); obj.disp(); } }

  22. (ii) Parameter constructor: The constructor with parameters. The parameter declared inside the class is called Actual parameters The parameter declared inside the constructor is called formal parameters or dummy parameters.

  23. Syntax class classname { type variablename1; ..... type variablenamen; classname(parameterlist) { // body of the method; } return type methodname() { //body of the method; }}

  24. Example class simple { int p,n,r,si; simple(int a,int b,int c) { p=a; n=b; r=c; } void calc() { si=p*n*r/100; }

  25. void disp() { System.out.println(si); } } class main { public static void main(String args[]) { simple obj= new simple(2000,3,4); obj.calc(); obj.disp(); } }

  26. 3.Methods The methods are called as member functions which defines the behavior of the class. The data items or fields are manipulated inside the methods. Java gives more power and flexibility to methods. Syntax: return type methodname() { //body of the method; } }

  27. Components of a function: return type(the type of the value the method returns) Method name->name of the function. Parameter list(list of the parameters) Body of the methods.

  28. Types Parameter-less method Parameterized method Parameter-less Method: This method is also called as default method. It is used to assign values to the variables. The method with no signature or no parameter is called parameter-less method. Syntax: return type methodname() { //body of the method; }

  29. Example class simple { int p,n,r,si; void getdata() { p=2000; n=2; r=3; } void calc() { si=p*n*r/100; }

  30. void disp() { System.out.println(si); } } class main { public static void main(String args[]) { simple obj= new simple(); obj.getdata(); obj.calc(); obj.disp(); } }

  31. Parameterized methods The method with signature or with parameters. The variables declared inside the class is called field or actual parameters The variables declared inside the method is called formal or dummy parameters. Syntax: return type methodname(parameter list) { //body of the method; }

  32. Example class simple { int p,n,r,si; Void getdata(int a,int b,int c) { p=a; n=b; r=c; } void calc() { si=p*n*r/100; }

  33. void disp() { System.out.println(si); } } class main { public static void main(String args[]) { simple obj= new simple(); obj.getdata(2000,2,5); obj.calc(); obj.disp(); } }

  34. Thank you

Related


More Related Content