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

Chapter6 Polymorphism Overloading Overriding Abstract

Chapter 6 discusses key concepts in Java programming including polymorphism, method overloading, method overriding, abstract classes, and interfaces. It explains how polymorphism allows methods to be overridden in subclasses, while overloading enables multiple methods with the same name but different parameters. The chapter also highlights the differences between abstract classes and interfaces, emphasizing that interfaces can contain only abstract methods and no data members.

Uploaded by

vuthnarak2005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter6 Polymorphism Overloading Overriding Abstract

Chapter 6 discusses key concepts in Java programming including polymorphism, method overloading, method overriding, abstract classes, and interfaces. It explains how polymorphism allows methods to be overridden in subclasses, while overloading enables multiple methods with the same name but different parameters. The chapter also highlights the differences between abstract classes and interfaces, emphasizing that interfaces can contain only abstract methods and no data members.

Uploaded by

vuthnarak2005
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Chaper6: Polymorphism, Overloading, Overriding,

Abstract និង Interface ​


• 6.1.Polymorphism: គឺជាលក្ខណៈមួយដែល Method មួយក្នុង Super Class
ត្រូវបានបង្កើត Override ច្រើនដងក្នុង Sub Classes មួយចំនួន។
Example1: class Animal { public void animalSound() {
System.out.println("The animal makes a sound"); } }
class Pig extends Animal {
public void animalSound() { System.out.println("The pig says: wee wee"); } }
class Dog extends Animal {
public void animalSound() { System.out.println("The dog says: bow wow"); } }

+Remember from the Inheritance chapter that we use the extends keyword to inherit
from a class.
+Now we can create Pig and Dog objects and call the animalSound() method on both of
them: Example2: class Animal {
public void animalSound() { System.out.println("The animal makes a sound"); } }
class Pig extends Animal {
public void animalSound() { System.out.println("The pig says: wee wee"); } }
class Dog extends Animal {
public void animalSound() { System.out.println("The dog says: bow wow"); } }
class Main { public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
Animal myDog = new Dog(); // Create a Dog object
myAnimal.animalSound(); myPig.animalSound(); myDog.animalSound(); } }
Chaper6: Polymorphism, Overloading, Overriding,
Abstract និង Interface ​
6.2.What is Method Overloading in Java?
Overloading: គឺជា Methods មួយចំនួនដែលមានឈ្មោះ​
ដូចគ្នាទាំងស្រុង ក៏ប៉ុន្តែខុសគ្នាត្រង់ Parameters ដូចជាប្រភេទ​
ទិន្នន័យខុសគ្នាឬចំនួនរបស់ Parameters ខុសគ្នា។
Java allows the creation of multiple methods in a class, which can
have the same names. But all the methods will be assigned with different
parameters. This feature is known as Java Method Overloading. The primary
significance of this feature is that it increases the readability of programs.
Method Overloading is often known as Compile-time polymorphism.
Method Overloading can also be accomplished by having different
parameter types with the same name. Here is an example of method
overloading in Java where multiple methods differ in data type:
class Overload {
public int sum(int a, int b){return a + b;}
public double sum(double a, double b){return a + b; }
public String sum(String a, String b){return a + b; }
public static void main(String[] args){
Overload ex = new Overload(); int result1 = ex.sum(2, 3);
double result2 = ex.sum(2.5, 3.5); String result3 = ex.sum("Hello ", "World");
System.out.println("Result1: " + result1); System.out.println("Result2: " +
result2);
Chaper6: Polymorphism, Overloading, Overriding,
Abstract និង Interface ​
6.3.Overriding: ជា Method (Data or Method) របស់ Super class ដែលត្រូវ
បានគេបង្កើតម្តងទៀត(ប្រកាស រឺ code ) ឡើងវិញនៅក្នុង Sub class។​Override method អាច
Access បានពីក្នុង Sub class ផ្ទាល់ប៉ុន្តែមិនអាចមាននៅក្នុង​object របស់ Sub class
ទេ។​
- Overriding អាចកើតមានដរាបណាមាន Inheritance
-ដើម្បី Access member របស់ Super class or Sub class ត្រូវតែប្រើ this រឺ
super
Ex: កម្មវិធីបញ្ជាក់ពីការប្រើ Overriding
class People{ String name; String sex;
void Show(){System.out.println("Name=" + name +" \nSex=" + sex); } }
class Student extends People{ String code; String dept;
void Show(){ super.Show(); System.out.println("Code=" + code + "\nDept=" +dept); }}
class Mstudent{ public static void main(String args[ ] ) {
Student stu = new Student(); stu.code =“001”; stu.name = “Samnang”;
stu.sex = “Male”; stu.dept = “Math”; stu.Show(); } }
Chaper6: Polymorphism, Overloading, Overriding,
Abstract និង Interface ​
• 6.4. Abstract class: គឺជា Class ដែលមាន Method មួយចំនួនមិនទាន់
មានលក្ខណៈ​ពេញលេញ(Abstract method) ។
+Abstract method គឺជា Method ដែលពុំមានដងខ្លួន(Body) មិនទាន់
Implementation ដែលមានលក្ខណៈដូចទៅនឹង Function Declaration។
+Abstract class មិនអាចបង្កើត object បានទេ គេប្រើសំរាប់តែធ្វើជា
Super class អោយ​Sub class overrived abstract method ។ Abstract class មាន
ទ្រង់ទ្រាយ៖
abstract class AbstractClass{ abstract void methodName1();
… abstract type methodNameN(); }
+Abstract class ត្រូវបានបង្កើតឡើងសំរាប់ធ្វើជាពុម្ពមេមួយ
ដើម្បីអោយ Classes មួយចំនួនផ្សេងទៀតអាច extends ទៅកាន់ Abstract class នេះ
ក្នុងការបង្កើតនូវដងខ្លួនរបស់ Abstract methods ទាំងអស់នោះ។
ចំពោះ Classes មួយ​ចំនួនដែល extends ទៅកាន់ Abstract class មានទ្រង់ទ្រាយ​

class ClassName1 extends AbstractClassName{ public void
methodName1(…){… }
… public type methodNameN(…){… } }
Example1: // Abstract class
abstract class Animal { // Abstract method (does not have a body)
public abstract void animalSound(); // Regular method
public void sleep() { System.out.println("Zzz"); } }
// Subclass (inherit from Animal)
Chaper6: Polymorphism, Overloading,
Overriding, Abstract និង Interface ​
Ex2:គេមានទំរង់​Abstract ត្រូវបានសំដែងចេញដូចខាងក្រោម
package pro_abstract;
import java.lang.System;
import java.util.Scanner;
abstract class Animal {
public abstract void animalSound(); //Abstract method (does not have a body)
public void sleep() // Regular method
{ System.out.println("Zzz"); } }
class Pig extends Animal // Subclass (inherit from Animal)
{ public void animalSound() // The body of animalSound() is provided here
{ System.out.println("The pig says: wee wee"); } }
abstract class NewyearA{ public abstract void Message(); }
class KhmerA extends NewyearA{
public void Message(){System.out.println("Sour Sdey Chhname Tmey 2024"); } }
class EnglishA extends NewyearA{
public void Message(){System.out.println("Happy New Year 2024!"); } }
public class Pro_Abstract {
public static void main(String args[ ] ){
System.out.println("Use object of Abstract class");
NewyearA Kh = new KhmerA(); Kh.Message(); //KhmerA kh = new KhmerA();
kh.Message();
NewyearA En = new EnglishA(); En.Message(); //EnglishA En = new EnglishA();
En.Message();
Chaper6: Polymorphism, Overloading,
Overriding, Abstract និង Interface ​
package pro_abstract_interface;
import java.lang.System;
import java.util.Scanner;
//Abstrac class
abstract class NewyearA{ public abstract void Message(); }
class KhmerA extends NewyearA{
public void Message(){System.out.println("Sour Sdey Chhname Tmey 2024"); } }
class EnglishA extends NewyearA{
public void Message(){System.out.println("Happy New Year 2024!"); }}
//Interface class
interface Newyear{ void Message(); }
class Khmer implements Newyear{
public void Message(){System.out.println("Sour Sdey Chhname Tmey"); }}
class English implements Newyear{
public void Message(){System.out.println("Happy New Year!"); } }
public class Pro_Abstract_Interface {
public static void main(String args[ ] ){
System.out.println("1.Use Abstract class");
//KhmerA kh = new KhmerA(); kh.Message();
//EnglishA En = new EnglishA(); En.Message();
NewyearA KhA = new KhmerA(); KhA.Message();
NewyearA EnA = new EnglishA(); EnA.Message();
System.out.println("2.Use Interface class");
Newyear Kh = new Khmer(); Kh.Message();//Khmer kh = new Khmer(); kh.Message();
Newyear En = new English(); En.Message();//English En = new English(); En.Message();
}
}
Chaper6: Polymorphism, Overloading,
Overriding, Abstract និង Interface ​
6.5. Interface: គឺជា Special abstract class សំរាប់ផ្ទុកនូវ
abstract method ទាំងអស់ដោយពុំចាំបាច់ដាក់ពាក្យ abstract ពីមុខ​
void or datatype​របស់ method ទាំងនោះឡើយ។​
+ Interface មានលក្ខណៈប្រហាក់ប្រហែលជាមួយabstract class
ដែរ
+ចំនុចខុសគ្នាជាមូលដ្ឋានរវាង abstract និង Interface គឺ
-All method របស់ Interface ត្រូវតែជា abstract method
-គ្មាន data member
-All method ក្នុង Interface ត្រូវ​public access ទោះបីជាមិនបាន
កំណត់ក៏ដោយ ។
-មិនចាំបាច់ប្រើ keyword abstract នៅខាងមុខ method
Interface មានទ្រង់ទ្រាយដូចខាងក្រោម៖
interface InterfaceName{ void methodName1(…);
…type methodNameN(…); }
ចំពោះ Class នីមួយៗអាច implements ទៅកាន់ Interfaces ជាច្រើនដើម្បី
បង្កើតដងខ្លួន​របស់ Methods ទាំងអស់ដែលស្ថិត នៅក្នុង Interfaces ទាំង​នោះ។
ហេតុនេះ Class នីមួយៗដែល implements ទៅកាន់​Interfaces មានទ្រង់ទ្រាយដូច
ខាងក្រោម៖
class ClassName implements InterfaceName1,InterfaceName2, …,
InterfaceNameN{
Chaper6: Polymorphism, Overloading, Overriding,
Abstract និង Interface ​
Ex1:គេមានទំរង់​Interface ត្រូវបានសំដែងចេញដូចខាងក្រោម
interface Newyear{ void Message(); }

class Khmer implements Newyear{


public void Message(){System.out.println(“Sour Sdey Chhname
Tmey”); }
}
class English implements Newyear{
public void Message(){System.out.println(“Happy New Year!”); }
}

class Test_Interface{
public static void main(String args[ ] ){
Khmer kh = new Khmer(); Kh.Message();
English En = new English(); En.Message();}
}
Note: Interface មួយអាច Implement​បានច្រើនដងបើ class ផ្សេងៗគ្នា ​
លក្ខណ់បែបនេះគេហៅថា Polymorphism។
Chaper6: Polymorphism, Overloading, Overriding,
Abstract និង Interface ​
Ex2:គេមានកម្មវិធីទំរង់​Interface ត្រូវបានសំដែងចេញដូច
ខាងក្រោម
package pro_abstract_interface;
import java.lang.System;
import java.util.Scanner;
interface Series{ int getNext();// return next number in series
void reSet();//restart
void setStart(int x);//set starting value
}
class ByTows implements Series{ int start; int value;
ByTows(){ start = 0; value = 0; }
public int getNext(){ value +=2; return value; }
public void reSet(){start =0; value =0; }
public void setStart(int x){ start = x; value = x; } }
public class Pro_Abstract_Interface {
public static void main(String args[ ] ){ ByTows obj = new ByTows();
for(int i=0; i<5;i++)
System.out.println("Next value is:"+obj.getNext());
System.out.println("\nReseting"); obj.setStart(450);
for(int i=0; i<5;i++)
System.out.println("Next value is:"+obj.getNext());
System.out.println("\nReseting"); obj.setStart(200);
for(int i=0; i<5;i++)
Program: OOP in Java with Interface
Ex3:គេមានទំរង់​Interface ត្រូវបានសំដែងចេញដូចខាងក្រោម
//WaterModel.java is class interface
interface WaterModel { double getVolume(); String getType(); }
//Cylinder.java is sub-class implements to interface
class Cylinder implements WaterModel{
private double radius; private double height;
public Cylinder(){ }
public Cylinder(double r, double h){ radius = r; height = h; }
public void setRadius(double r){ radius = r;​}
public void setHeight(double h){ height = h; }
public double getRadius(){ return radius; }
public double getHeight(){ return height; }
public double getVolume(){ double s;
s = Math.PI*getRadius()*getRadius();
return(s*getHeight()); }
public String getType(){ return "Cylinder"; }
public String toString(){ String st;
st = "Volume of " + getType() + " is " + getVolume();
return st; } }
Program: OOP in Java with Interface
//Prolepipet.java is sub-class implements to interface
class Prolepipet implements WaterModel{
private double length; private double height; private double width;
public Prolepipet(){ }
public Prolepipet(double l, double h, double w){length= l;
height=h;width=w;}
public void setLength(double l){ length = l; }
public void setHeight(double h){ height = h; }
public void setWidth(double w){ width = w; }
public double getLength(){ return length; }
public double getHeight(){ return height; }
public double getWidth(){ return width; }
public double getVolume(){ return
getHeight()*getLength()*getWidth(); }
public String getType(){ return "Prolepipet"; }
public String toString(){ String st;
st = "Volume of " + getType() + " is " + getVolume();
return st; }
}
យើងត្រូវ Build Classes ដូចជា WaterModel.java, Cylinder.java និង
Program: OOP in Java with Interface
//VolumeCylinder.java is Main file
import ​java.util.Scanner;
public class VolumeCylinder { public static void main(String[] args) {
double height; double radius;
Scanner cin = new Scanner(System.in); Cylinder ​cyd = new
Cylinder();
System.out.print("Input height: "); height = cin.nextDouble();
cyd.setHeight(height); System.out.print("Input radius: "); radius =
cin.nextDouble();
cyd.setRadius(radius); System.out.println(cyd.toString()+ "m3"); } }
//VolumeProlepipet.java is Main file
import java.util.Scanner;
public class VolumeProlepipet { public static void main(String[] args) {
double height, length, width; Scanner cin = new
Scanner(System.in);
System.out.print("Input height: "); height = cin.nextDouble();
System.out.print("Input length: "); length = cin.nextDouble();
System.out.print("Input width: "); width = cin.nextDouble();
Prolepipet p = new Prolepipet(length,height,width);
System.out.println(p.toString()+ "m3"); } }

You might also like