Java Method 3 Class Object Structure Class Structure
ម តក • Java Method 3 • Class & Object ▪ Structure ▪ Class Structure ▪ Return type ▪ Constructor ▪ Access modifier ▪ this & this() ▪ Parameter & Varargs ▪ Object creation
១. Java Method : ����� code ����������� Method ��� 4 ������� ▪ Method ����� return type ����� parameter ▪ Method ����� return type ��� parameter ▪ Method ��� return type ����� parameter ▪ Method ��� return type ��� parameter 4
១. ១ Method Structure ���� method ������ Java method programming header នង method body។ • Method Header ▪ ]Access Modifier [ ▪ Return type ▪ Method Name ▪ Parameter list ▪ Exception • Method body ▪ Statements or line of code 5
១. ១ Method Structure (ត ( Syntax for write a method ] Access_modifiers] return_Type method_Name ([Param 1, Param 2}([…, ] Code Statement[; ……… ] return_Type; [ { 6
១. ៣ Access Modifier Access modifier ������� Java �������� • private • default Access Modifier within class within package outside package by subclass only • protected Private Y N N N Default Y Y N N Protected Y Y Y N Public Y Y • public 8
១. ៤ Parameter & Vararge Parameter • Parameter ����� method ������� បនទ បពឈន method។ គ បរ វ សមរ ប ផតល value ទ ឲយ method។ ក រ បងក ត Parameter ���������� return type parameter variable name ��� �� • Parameter Types ▪ primitive data types : Integer number, character, floating number ▪ Reference data type : objects ��� arrays Example: int sum(int a, int b) // method that contain 2 parameter 9 sum(2, 2); // when method is invoked
១. ៤ Parameter & Vararge (ត ( • Syntax of varargs: return_type method_name(data_type. . . variable. Name {}( 11
២. class & object 12
២. ១ class structure Package_Name; ]access modifier] class Class_Name} ]instatnce variable; [ ]Constructor; [ ]Method; [ ]block; [ ] { 13 nested class and interface[
២. ២ constructor Constructor ������ Java ����� method ������� ហ យម នឈម ដច class។ Constructor គបរ សមរ បផតលតមល ដបង នង បងក ត object។ Constructor �������� • Default constructor (no-parameter constructor( • Parameterized constructor 14
( ២. ២ constructor (ត Constructor Overloading : ������� Java programming ������ f overload constructor ��������� constructor ������� ខសគន បណណ ។ public class Person} private String name; private int age; public Person{}() public Person(String name, int age}( this. name=name; this. age=age; { 15 {
២. ២ constructor (ត ( Difference between constructor and method in java Java Constructor Java Method Constructor is used to initialize the state of an Method is used to expose behavior of an object. Constructor must not have return type. Method must have return type. Constructor is invoked implicitly. Method is invoked explicitly. The java compiler provides a default constructor if Method is not provided by compiler in any case. you don't have any constructor. Constructor name must be same as the class name. Method name may or may not be same as class name. 16
២. ២ constructor (ត ( Copy Constructor ន កនង Java programming អតម ន ក រ copy constructor ទ ។ ត យ ងអ ចធវ ក រ value������ object ��������� copy • By constructor • By assigning the values of one object into another • By clone() method of Object class 17 copy value ������
២. ៣ this & this () This keyword �� reference variable �������� (refer) current objec ក របរ បរ ស this keyword ម ន ៦ចនច៖ • this can be used to refer current class instance variable. • this can be used to invoke current class method (implicitly( • this() can be used to invoke current class constructor. • this can be passed as an argument in the method call. • this can be passed as argument in the constructor call. • this can be used to return the current class instance from the method. 18
២. ៣ this & this () • this can be passed as an argument in the method call : ជ ទទ គ បរទ ល event handling • this can be passed as argument in the constructor call. ជ ទទ គ ចងបរ ត មយ object ដ លន កនង class ផស ងគន ជ ចរ ន 19
២. 4 Object Creation ក របងក ត object គម ន ៥ បរភ ទ៖ 20 Using new keyword → {constructor gets called Using new. Instance() method of Class class → {constructor gets called Using new. Instance() method of Constructor class → {constructor gets called Using clone() method → {no constructor call Using deserialization → {no constructor call
២. 4 Object Creation • Using new. Instance() method of Class class Person person= (Person) Class. for. Name("oop. Person"). new. Instance; () Person person 2= Person. class. new. Instance; () • Using new. Instance() method of Constructor class Constructor<Person> constructor= Person. class. get. Constructor; () Person person=constructor. new. Instance; () • Using clone() method Person person =new Person; () Person person 2=(Person) person. clone; () 21
៣. ឯកស រយ ង • https: //dzone. com/articles/5 -different-ways-to-create-objects-in-java-with-ex • http: //www. javatpoint. com/varargs • http: //www. javatpoint. com/this-keyword • http: //www. javatpoint. com/object-and-class-in-java • http: //www. javatpoint. com/constructor 22
- Slides: 23