Polymorphism

 

Polymorphism

Sub Class වල තියෙන් Run කරන එක Polymorphism නම් වේ. එනම් Super Class එකක Method එකක් Upcasting කරලා Super Class එකේ Reference එක හරහා Sub Method Run කිරීමේ ක්‍රියාවලිය වේ.

ඒ සදහා අපට Inheritance , Override , Upcasting පිළිබද දැනුම අවශ්‍ය වේ.

Ex: 

 
 public class Shapes {
void draw(){} // This is a meaningless Method
}
class Rectangle extends Shapes{
@Override
void draw() {

System.out.println("Drawing Rectangle!");
}
}

class Circle extends Shapes{
@Override
void draw() {
System.out.println("Drawing Circle!");
}
}

class Triangle extends Shapes{
@Override
void draw() {
System.out.println("Drawing Triangle");
}
}

class test1{
public static void main(String[] args) {
Shapes s;
s = new Circle();s.draw();
s = new Triangle();s.draw();

}
}

මෙහිදී Body එකක් නොමැති( Meaningless ) Method එකක් සාදා එමගන් Sub class වල ඇති Methods Call කර ඇත. 

Comments

Popular posts from this blog

What are the 'Java Standards' ???? 🤔

What are the 'Java Data Types' and 'Access Modifiers' ???? 🤔

Inheritance in java

Java Main Method and Construtor

Java Method Overloading

Encapsulation

Abstraction in Java