User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







java:classes:extends

extends

extends extends an already existing class with custom methods

package myclass.mydate;
 
import java.util.Date;
 
/* 
 * extends Date = extends class java.util.Date 
 * java.util.Date must be importet 
 */
public class MyDate extends Date { 
 
    public int getFullYear() {
 
        return this.getYear() + 1900;
    }
 
 
 
// overrride the method getMonth from java.util.Date
 
    @Override
    public int getMonth() {
        return 99;
    }    
}

usage of the extended class

package xmascalc;
 
 
import myclasses.mydate.MyDate; // import the class MyDate
 
 
 
 
public class XmasCalc{
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
 
 
        MyDate my = new MyDate();
        int year = my.getFullYear();
        System.out.println(year);
 
 
        int month = my.getMonth();
        System.out.println(month);
 
    }
}

output

  2013
  99

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
java/classes/extends.txt · Last modified: 2024/02/16 01:03 (external edit)

Impressum Datenschutz