Search code examples
javainheritancemethodssubclasssuperclass

Inheritence in Java


I am just learning how to use inheritance in Java. The purpose of this program is to write classes that contain information about certain log entries, and provide methods to set and get appropriate information. Please let me know if there any corrections needed.

ArithmeLog.java

//super class ArithmeLog with three functions to capture three instances

import java.util.*;

import java.text.*;

public class ArithmeLog {

 public ArithmeLog() {

 }



 public void captid1(String a) {

      System.out.println("");

 }



 public void captid2(String SongName) {

      System.out.println("");

 }

 public void Date() {

      Date capttim = new Date( );

      SimpleDateFormat formt = new SimpleDateFormat

 ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

      System.out.println("Time is : " +

formt.format(capttim));

   }

    }

SongLog.java

//class SongLog which inheriting ArithmeLog class

import java.util.*;

import java.text.*;

public class SongLog extends ArithmeLog {

 public SongLog() {

      super();

      System.out.println("");

 }



 @Override

 public void captid1(String SongID) {

      System.out.println("SongID is: "+SongID);

 }



 @Override

 public void captid2(String SongName) {

      System.out.println("Song name is: "+SongName);

 }

 @Override

 public void Date() {

      Date capttim = new Date( );

      SimpleDateFormat formt = new SimpleDateFormat

  ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

      System.out.println(" Date and time is : " +

  formt.format(capttim));

 }

 }

 AdLog.java

// AdLog class which inheriting ArithmeLog class

import java.util.*;

import java.text.*;

 public class AdLog extends ArithmeLog {

 public AdLog() {

      super();

      System.out.println("");

 }



 @Override

 public void captid1(String AdvertisementID) {

 System.out.println("AdvertisementID is:"+AdvertisementID);

 }



 @Override

 public void captid2(String AdvertiserID) {

      System.out.println("AdvertiserID is:"+AdvertiserID);

 }

 @Override

 public void Date() {

      Date capttim = new Date( );

      SimpleDateFormat formt = new SimpleDateFormat

 ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

      System.out.println(" Date and time is : " +

 formt.format(capttim));

 }

 }

  Mainpgm.java

// main class creating objects of classes and calling methods

public class Mainpgm {

 public static void main(String[] args) {

      //creating first object of classes

      ArithmeLog animal = new ArithmeLog();

      AdLog ao1 = new AdLog();

      SongLog so1 = new SongLog();

      System.out.println();

      ao1.captid1("1");

      ao1.captid2("7");

      ao1.Date();

      so1.captid1("1");

      so1.captid2("songg1");

      so1.Date();

      //creating second object of classes

      AdLog ao2 = new AdLog();

      SongLog so2 = new SongLog();

      ao2.captid1("2");

      ao2.captid2("ad2");

      ao2.Date();

      so2.captid1("2");

      so2.captid2("songg2");

      so2.Date();

      //creating third object of classes

      AdLog ao3 = new AdLog();

      SongLog so3 = new SongLog();

      ao3.captid1("3");

      ao3.captid2("ad3");

      ao3.Date();

      so3.captid1("6");

      so3.captid2("songg3");

      so3.Date();



    }

  }

Solution

  • Based on your comment, do you have all of this in 1 file?

    Each 'section' should be in it's own file. So a file named ArithmeLog.java with everything underneath that line up to SongLog.java, and then a file named SongLog.java which then has everything underneath that, etc.