Advertisement
stevennathaniel

Penerapan OOP yang Berhasil: Pembuatan Class Override

Feb 3rd, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.15 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package latihanbasic2;
  7.  
  8. /**
  9.  *
  10.  * @author steven
  11.  */
  12.  
  13.  
  14.  
  15. class Apel{
  16.            
  17.             void makan(){
  18.                
  19.                
  20.                 System.out.println("Apel bisa dimakan");
  21.             }
  22.            
  23.            
  24.         }
  25.        
  26.        
  27.         class ApelMerah extends Apel{
  28.            
  29.             void makan(){
  30.                
  31.                 System.out.println("Apel Merah bisa juga dimakan");
  32.             }
  33.         }
  34.        
  35.        
  36.         // public class Makanan{
  37.            
  38.            // public static void main(String args[]){
  39.                
  40.                
  41.               //  Apel obj1 = new Apel();
  42.                
  43.               //  Apel obj2 = new ApelMerah();
  44.  
  45.  
  46. public class BasicClass2 extends javax.swing.JFrame {
  47.  
  48.     /**
  49.      * Creates new form BasicClass2
  50.      */
  51.     public BasicClass2() {
  52.         initComponents();
  53.     }
  54.  
  55.     /**
  56.      * This method is called from within the constructor to initialize the form.
  57.      * WARNING: Do NOT modify this code. The content of this method is always
  58.      * regenerated by the Form Editor.
  59.      */
  60.     @SuppressWarnings("unchecked")
  61.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  62.     private void initComponents() {
  63.  
  64.         jButton1 = new javax.swing.JButton();
  65.  
  66.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  67.  
  68.         jButton1.setText("jButton1");
  69.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  70.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  71.                 jButton1ActionPerformed(evt);
  72.             }
  73.         });
  74.  
  75.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  76.         getContentPane().setLayout(layout);
  77.         layout.setHorizontalGroup(
  78.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  79.             .addGroup(layout.createSequentialGroup()
  80.                 .addContainerGap()
  81.                 .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
  82.                 .addContainerGap(216, Short.MAX_VALUE))
  83.         );
  84.         layout.setVerticalGroup(
  85.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  86.             .addGroup(layout.createSequentialGroup()
  87.                 .addContainerGap()
  88.                 .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
  89.                 .addContainerGap(203, Short.MAX_VALUE))
  90.         );
  91.  
  92.         pack();
  93.     }// </editor-fold>                        
  94.  
  95.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  96.         // TODO add your handling code here:
  97.        
  98.        
  99.        
  100.         Apel obj1 = new Apel();
  101.                
  102.         Apel obj2 = new ApelMerah();
  103.        
  104.        
  105.        
  106.         obj1.makan();
  107.         obj2.makan();
  108.        
  109.        
  110.     }                                        
  111.  
  112.     /**
  113.      * @param args the command line arguments
  114.      */
  115.     public static void main(String args[]) {
  116.         /* Set the Nimbus look and feel */
  117.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  118.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  119.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  120.          */
  121.         try {
  122.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  123.                 if ("Nimbus".equals(info.getName())) {
  124.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  125.                     break;
  126.                 }
  127.             }
  128.         } catch (ClassNotFoundException ex) {
  129.             java.util.logging.Logger.getLogger(BasicClass2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  130.         } catch (InstantiationException ex) {
  131.             java.util.logging.Logger.getLogger(BasicClass2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  132.         } catch (IllegalAccessException ex) {
  133.             java.util.logging.Logger.getLogger(BasicClass2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  134.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  135.             java.util.logging.Logger.getLogger(BasicClass2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  136.         }
  137.         //</editor-fold>
  138.  
  139.         /* Create and display the form */
  140.         java.awt.EventQueue.invokeLater(new Runnable() {
  141.             public void run() {
  142.                 new BasicClass2().setVisible(true);
  143.             }
  144.         });
  145.     }
  146.  
  147.     // Variables declaration - do not modify                    
  148.     private javax.swing.JButton jButton1;
  149.     // End of variables declaration                  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement