(Java)Overriding

Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding.

Example :

Create Class , Class Name Father.java

public class Father {
    
    private int id;
    
    public int getID(){
        return id;
    }
    public void setId(int id){
        this.id = id;
    }
    
}
<pre> 

Create Class Child.java

public class Child extends Father implements Serializable{


    public void setId(Integer  id){
        if(id == null){
            System.out.println("nilai id tidak boleh nulll");
        }else{
            super.setId(id);
        }
    }
    
}
<pre> 

Create Class DemoOverriding.java

public class DemoOverriding {
    
    public static void main(String[] args) {
        Father ft = new Father();
        Father mc = Child();
        
        Integer id = null;
        
        ft.setId(id);
        mc.setId(id);
    }

    private static Customer MemberCustomer() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

   
    
}
<pre> 

Reference :
Ebook Java Desktop Ifnu Bima.

Advertisements

Published by

jarcode

hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s