1 / 4

Constructor overloading

Constructor overloading. Siti Halisah 0857201000464.

konala
Télécharger la présentation

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constructor overloading SitiHalisah 0857201000464

  2. 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.

  3. 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 ) ;    } }

  4. 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 ) ;    } }

More Related