Constructor overloading
This presentation is the property of its rightful owner.
Sponsored Links
1 / 4

Constructor overloading PowerPoint PPT Presentation


  • 40 Views
  • Uploaded on
  • Presentation posted in: General

Constructor overloading. Siti Halisah 0857201000464.

Download Presentation

Constructor overloading

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Constructor overloading

Constructor overloading

SitiHalisah

0857201000464


Constructor overloading

Konstruktordigunakanuntukmenetapkannilaiawaluntukvariabel instance darikelas. Sebuah default constructor tanpaargumenakandipanggilsecaraotomatisoleh Java Virtual Machine (JVM). Konstruktorselaludisebutoleh operator baru. Konstruktordideklarasikansepertisetelahkamimenyatakanmetode, kecualibahwa constructor tidakmemilikitipekembali. Konstruktordapatkelebihanbebanasalkanmerekaharusmemilikiargumen yang berbedakarena JVM membedakankonstruktoratasdasarargumenberlaludalam constructor.

Setiapkali kitamemberikannamametode yang samasepertinamakelas. Ingatmetodeinitidakharusmemilikitipekembali. Inidisebutsebagaikonstruktor overloading.

Kamitelahmembuatsatu program pada constructor overloading, setelahmelaluiitukonsep overloading constructor akanmendapatkanlebihjelas. Padacontohdibawahkamitelahmembuattigakonstruktorkelebihanbebanmasing-masingmemilikiargumen yang berbedajenissehingga JVM dapatmembedakanantaraberbagaikonstruktor.


Kode program ini diberikan di bawah ini

Kode program inidiberikandibawahini:

  • public class ConstructorOverloading {    public static void main ( String args []){    Rectangle rectangle1= new Rectangle ( 2 , 4 ) ; intareaInFirstConstructor=rectangle1.first () ; System.out.println ( " The area of a rectangle in first constructor is : " + areaInFirstConstructor ) ;    Rectangle rectangle2= new Rectangle ( 5 ) ; intareaInSecondConstructor=rectangle2.second () ; System.out.println ( " The area of a rectangle in first constructor is : " + areaInSecondConstructor ) ;    Rectangle rectangle3= new Rectangle ( 2.0f ) ;    float areaInThirdConstructor=rectangle3.third () ; System.out.println ( " The area of a rectangle in first constructor is : " + areaInThirdConstructor ) ;    Rectangle rectangle4= new Rectangle ( 3.0f , 2.0f ) ;    float areaInFourthConstructor=rectangle4.fourth () ; System.out.println ( " The area of a rectangle in first constructor is : " + areaInFourthConstructor ) ;    } }


Constructor overloading

  • class Rectangle { int l, b; float p, q; public Rectangle ( int x, int y ){    l = x;    b = y;    } public int first (){ return ( l * b ) ;    } public Rectangle ( int x ){    l = x;    b = x;    } public int second (){ return ( l * b ) ;    } public Rectangle ( float x ){    p = x;    q = x;    } public float third (){ return ( p * q ) ;    } public Rectangle ( float x, float y ){    p = x;    q = y;    } public float fourth (){ return ( p * q ) ;    } }


  • Login