Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
28 views23 pages

Chapter 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 23

Chapter II

Object Oriented Programming in Java


Objectives
• សិក្សាអំពីរបៀបសរសេរកូដបែប OOP ក្នុង Java និង Feature របស់
OOP ក្នុង Java
• class/objects
• Object’s member
• OOP characters
What is OOP?
• OOP ជាទំរង់នៃការសរសេរ កម្មវិធីដែលតំរូវអោយ Programmer បង្កើត Objects រួច
តំរង់ទិសអោយ Objects (តាមរយៈផ្លា ស់ប្តូរតំលៃ Properties values/បញ្ជា Methods)
ដើម្បីបញ្ជា អោយ Objects ទាំងនោះធ្វើការ​ដោះស្រាយបញ្ហា ។

មានពីរវិធីដើម្បីបង្កើត Objects
- Instantiate from class (new keyword)
- Use factory methods.
1.Class vs object
• What is a class? In object-oriented programming, a class is a blueprint for
creating objects (a particular data structure), providing initial values for state
(member variables or attributes), and implementations of behavior (member
functions or methods).A class is a blueprint that defined and store in a files.
• What is a Object ? In the object-oriented programming paradigm object can
be a combination of variables, functions, and data structures; in particular in
class-based flavour of the paradigm it refers to a particular instance of a
class.
Class vs Object

Disk
RAM
Object’s Members
An objects is encapsulated its member which can be data member or method
meber.
• A data member or properties is a variable of a class for storing value to
describe objects which instantiated from the class.
• A method is a sub program of a class for storing codes to for processing
data (do action) when it’s invoked(call) by object action.
2. class definition
[access] class className
{
[access] type member1;
[access] type member2;
[access] method definition
……
Constructors
}
Class’s components
• Access level : for class has public and default(not write any definition) for member have 4 access
level:
• private access in only its class
• default: access in only same package
• protected access in same package and outside package if it’s sub class
• public all
• Constructor is a special methods that run when instantiating. Use to assign initial value to data
member. In java if do not define constructors a default constructor is automatically created without
any parameter, however when we add user define the default is remove, you can keep it by create a
overload constructor.
• Methods: process: there are 2 type
• Typed method: method that it’s name hold value when finish process.(function)
access type methodName(par1,par,…)
{
code
reurn val
}
• Un-typed or void methods: method for doing something without return any value.
Typed use to find something
Un-typed use to do something.
• Parameter in java: មានពីទំរង់ call by val if primitive type & call by ref if object
type
(Cannot change)
overloading methods
• In java like other OOP methods can be overloaded. Overloading methods
are methods have same name but different parameters.
Different number of parameter or data type of parameter.
Overloading methods use one block location of memory to store codes of all
methods. Codes will be loaded over and over in memory when it’s called.
static members
• A static member is member declare by key word ”static” . Static members
store it’s value or code in a class (class encapsulation).
• Static member can access without object instantiate
• For data member(variable) it’s transparency value over objects.
• Static member is a feature inherit from structure programming of OOP.

NOTE: static member អាច call បានឬប្រើប្រាស់បានតែ static ដូចគ្នា ក្នុង Level ជាមួយគ្នា ។
3. Inheritance
ជាទំរង់ដែល classថ្មី (java ហៅថា sub class) មួយអាចប្រើប្រាស់រចនាសម្ព័ន្ធ​
របស់ class ចាស់ដែលបាន compile រួច (ក្នុងjava ហៅថា super class )
ដោយបន្ថែម membersថ្មីៗ ហើយ Objects sub class បានទទួលលក្ខណៈ

របស់ super class និងអាចជា Object របស់ super class.


Syntax

[access] class ClassName extends SuperClass{


-​New data member
-New methods
- New constructor
}
Note: ក្នុ ង​Java 1 extend មានតែ Super Classតែមួយគត់(Single Class
Inheritant)
Inheritance con
• ក្នុង Java object of sub class when instantiate(ពេលបង្កើត)​មួយផ្ក
ចេញពី​Sub Class និង មួយផ្កចេញពី
​ Super Class​មានន័យថា Object
របស់ Sub Class ស្វ័យប្វត្តជា Object របស់ Super Class ប៉ុន្មិនអាផ្ទុយ
មកវិញទេ។
Example
class Person
{
String name;
String sex;
int age;
void showInfo(){….}
}
class Employee extends Person
{
String post;
int salary;
String org;
void showEmp()
{……}
}
Example
Employee objEmp=new Employee();
objEmp.name=…
………
Person p=objEmp; // OK
Person pObj=new Person();
pObj.name=…..
….
Employee eobj=pObj;// Cannot error.
this and super keyword
• this ជា Keyword តំណាងអោយ current class ដើម្បីពេលប្រើជាមាន ករណី Overing ឬកណី
អថេរស្ទួននឹង Data Member.
Syntax: this.membername(…);
• super: ជា Keyword​តំណាងអោយ super class។ Keyword super ប្រើ ក្នុងពីរទំរង់
• Access super class members គរណី មាន overriding
• Syntax: super.memberName
• Call constructor របស់ super class ពីក្នុង constructor របស់​sub class
• Syntax: super(arg,…);
• Note: super(arg,…) អាចត្រូវតែជា first statement in constructor.
4.Overriding and polymorphism
• Overriding ជាលក្ខណៈដែល Members របស់ super class ត្វបានគេ
សរសេរឡើងវិញ ឬប្កាសឡើងវិញនៅលើ
លើ Sub class.
• Overridden members can see(access) by sub class by using keyword this
and super but in object sub class object can see only overriding member
only.
• polymorphism method ជាលគ្ខណៈដែល Overridden methods ត្វបានគេ
Implement overriding ច្នដងផ្ងៗគ្នា
ដើម្អោយ ដើម្បីអោយ
បី
Overridden មួយរបស់ super class ធ្វើការផ្ងៗគ្នា ពេលទទួលតំលៃពី Sub
Class។
• polymorphism ​ជាលក្ខណៈដែល sub class បំពេញការងារអោយ Super
class.
5.Abstract class and Interface
• Abstract ជា Class បណ្តោះអាសន្នដែលមិនទាន់អាចបង្កើត Objects បាន ពោលគេប្រើសំរាប់តែ Overriding and polymorphism ក្នុង

ទំរង់ការបង្ខំ. Abstract class អាចមាន ទាំងabstract methods និង methods ពេញលេញ។

• Abstract methods ជា methods ដែលមានតែ Structure ពោលគឺជាទំរង់ ប្រកាសសុទ្ធសាធ​និងតំរូវអោយត្រូវតែ Override.

Syntax

[access] abstract class ClassName

data member declaration

Methods definition// implemented methods

abstract methodName1(par,..);

abstract methodName2(par,..);

}
Interface
• Interface ជា special abstract class ដែលផ្ទុកទៅដោយ abstract methods.
• ចំនុចខុសគ្នា ចំបង រវាង abstract class vs interface
• ក្នុង Interface មានសុទ្ធតែ public abstract methods ចំនែក abstract class មានគ្ប់
ទាំងអស់។
• Methods របស់ Interface សុទ្ធតែ Abstract ដោយពុំប្ Keyword abstract ក្នុង abstract
class ត្វតែកំណត់ច្បាស់លាស់។
• ក្នុង Interface គ្ប់ abstract methods ជា public
• ដើម្បីបង្ខំអោយ sub class override methods interface គេប្ keyword
implements InterfaceName ខុសពី abstract class, class មួយអាច
implements interface ច្ន។
Interface
Syntax define Interface
access interface interfaceName
{
void methodName(par,…);
type methodName(par,…);
…….
}
Syntax implement interface
class ClassName extends SuperClass implements IntefaceName1,….{
overriding method implements
}
NOTE: Sub class ដែល Implement interface or abstract class ត្វOverride
​ all abstract methods otherwise
this class must be abstract too.
5. Final keyword.
final keyword ប្ក្នុង៣ទំរង់
​៣ ​ទំ
រង់
• 1. ប្កាសថេរ
ថេរ !
• Syntax: final type VarName=Value;
• Example final String connection=“db\...”
• 2. កំណត់methodដើម្បីកំអោយគេ Override.
• Syntax: finale methodName(….){…..}
• 3. កំនត់ class កុំអោយគេ extends.
final class ClassName….{
Codes;
}​
6. Package
• Package ជាឈ្មោះ មុខតំណររបស់ class និង Folder ជាទីតាំងសំរាប់ទុក​
Classes.
• Package ប្បដូចជាទីតាំងកំណត់ដង្ហើមរបស់ classes.
• Syntax
package packageName;
class ClassName{…..}
• ដើម្បី ប្ប្ស់ class ក្នុង Package ប្​keyword imports
import package.*; // use all classes in package
import package.ClassName; use only one class of package.

You might also like