-1

According to my book it is ok! It says if we write <classname> with the method then we don't need to cast the Object parameter. Why am I getting the error??

import java.util.*;

interface politics {    //interface
    void politician(Object obj);
}  

class obama implements politics <obama>  //class
{  
    String job;
    public void politician(obama p) {
        if(p.job.equals("president")) {
            System.out.print("You are right Obama is Mr. PRESIDENT");
        }
        else {
            System.out.print("So you say Obama is a "+job.toUpperCase());
        }
    }
}

class interface2 {   //class
    public static void main(String args[]) {    //main
        Scanner in=new Scanner(System.in);
        obama o=new obama();
        System.out.println("president or citizen?");
        String s=in.next();
        o.job=s;
        o.politician(o);
    }
}
admdrew
  • 3,790
  • 4
  • 27
  • 39

1 Answers1

2

Your interface politics is not generic. In your code it should look like this:

interface politics<T> {
    void politician(T obj);
}

It's just a very short answer, for sure you should read the Java tutorial on generics first, then try the examples from the tutorial and then return back to your code.

The difference between your code and your book is that Comparable is defined like this:

public interface Comparable<T>
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
  • Thanks, can you please explain the difference? – user3798851 Jul 02 '14 at 18:43
  • yes but why is it not working with the Object type parameter? – user3798851 Jul 02 '14 at 18:47
  • Please **study** the tutorial first. You would not be able to understand my answer before it. If you have still questions *after* studying the tutorial, feel free to ask. You are asking me to explain you the differentials before understanding the multiplication :) – Honza Zidek Jul 02 '14 at 18:47