Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

Sunday, August 14, 2016

Constructors Dalam Java


Constructor adalah blok kode yang memungkinkan Anda untuk membuat sebuah objek dari kelas.
Ini juga bisa disebut menciptakan sebuah instance. 
Constructor terlihat seperti metode tapi bukan, misalnya metode dapat memiliki nilai kembali (return)atau tidak (dianggap sebagai void)
Jenis Constructor ada tiga jenis Constructor : 

  • default 
  • no- arg 
  • parameterised
1. Constructor default : Jika Anda tidak mendefinisikan Constructor apapun didalam kelas, Maka java akan otomatis membuatkan satu untuk Anda secara default. Constructor ini dikenal sebagai Constructor default. Anda tidak akan menemukannya dalam source code program Anda, contoh Constructor default pada kelas Demo


    public Demo() { 
    }

2. no- arg constructor : adalah constructor tanpa argumen, no-arg constructor hampir sama dengan constructor default, contoh constructor no- arg

    class Demo { 
          public Demo() { 
          System.out.println("This is a default constructor"); 
       }
    }

3. Parameter Constructor : Constructor dengan argumen dikenal sebagai Constructor diparameterisasi.

Bagaimana memanggil konstruktor ? Untuk memanggil konstruktor menggunakan kata kunci baru , diikuti dengan nama kelas , diikuti dengan parameter jika ada . Misalnya untuk membuat objek dari kelas Demo , Anda dapat memanggil konstruktor seperti ini : 

     new Demo()

1. Pembuatan object menggunakan constructor Kita akan membuat kelas dengan nama "Demo" 
    
     class Demo;

2. Pembuatan obyek - Memanggil constructor default untuk menciptakan objek dari kelas Demo (kata kunci baru diikuti dengan nama kelas )

     new Demo();

3. Sekarang, aku menempatkan objek untuk sebuah referensi

     class Demo = new Demo();

Bagaimana jika Anda tidak menulis konstruktor di kelas ? Seperti dibahas di atas, jika Anda tidak menulis konstruktor di kelas Anda, java akan otomatis membuatkannya. Mari kita lihat kode di bawah ini untuk memahaminya.

     class contoh {
        public void demoMethod() 
        { 
               System.out.println("hello");
        }
        public static void main(String args[]) 

        {
               Example obj = new Example();
               obj.demoMethod();
        }
}

Dalam public static void main saya menciptakan objek kelas "contoh" diatas adalah contoh default constructor dari kelas Contoh.
perhatikan tidak ada constructor new Contoh().
Tapi dari mana saya menulis constructor ? TIDAK PERNAH
java lah yang membuatnya.


Wednesday, July 6, 2016

Object-Oriented Programming Concepts


Object-Oriented Programming Concepts

If you've never used an object-oriented programming language before, you'll need to learn a few basic concepts before you can begin writing any code. This lesson will introduce you to objects, classes, inheritance, interfaces, and packages. Each discussion focuses on how these concepts relate to the real world, while simultaneously providing an introduction to the syntax of the Java programming language.

An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. This lesson explains how state and behavior are represented within an object, introduces the concept of data encapsulation, and explains the benefits of designing your software in this manner.

A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.

Inheritance provides a powerful and natural mechanism for organizing and structuring your software. This section explains how classes inherit state and behavior from their superclasses, and explains how to derive one class from another using the simple syntax provided by the Java programming language.

An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface. This section defines a simple interface and explains the necessary changes for any class that implements it.

A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. This section explains why this is useful, and introduces you to the Application Programming Interface (API) provided by the Java platform.


Kriptografi Sederhana Dengan Algoritma Blowfish Menggunakan Java

Pada postingan sebelumnya saya memposting tentang enkripsi dan dekripsi sederhana dengan cara menggantikan kata yang di input dengan m...