public class Human {
	private String name;
	
	public Human(String name){
		this.name = name;
	}
	
	public String getName(){
		return name;
	}
	public String toString(){ 
		return "Human " + getName(); 
		}

	
	
}

interface Electrician{
	public String changeBulb();
	
}

interface Plumber{
	public String unplugDrain();
}

class HandyPerson extends Human implements Electrician, Plumber{
	public HandyPerson(String name){
		super(name);
	}
	
	public String changeBulb(){
		return "HP: Unscrew old one replace new 1";
		
	}
	
	public String unplugDrain(){
		return "HP: Use plunger";
	}
	
	public String toString(){ 
		return "HandyPerson " + getName(); 
		}

	
	

}

class NobodySpecial extends Human{
	public NobodySpecial(String name){
		super(name);
	}
	
	public void changeBulb(){
		System.out.print("NS: Unscrew old, screw in new");
	}
	
	public void unplugDrain(){
		System.out.println("NS: Use plunger");
	}
	
	public String toString(){ 
		return "Nobody Speacial " + getName(); 
		}
	
}

class IndustrialPlumber extends Human implements Plumber{
	public IndustrialPlumber(String name){
		super(name);
	}
	
	public String unplugDrain(){
		return "IP: We'll add in to out list";
		
	}
}

i got this on a tutorial website..but a error comes. I know that i should insert a main function but where should i insert? @.@

First of all NobodySpecial doesn't need to have the unplugDrain, changeBulb methods because it doesn't implement the interfaces. It is not wrong, but the methods are not mandatory.

As far as your question. You can put the main method in whatever class you want.
You can put it in the Human, NobodySpecial, wherever. It will not affect the compilation, nor what your classes do.
The main is used only for you to "tell" java what commands to run. If you put it in the Human, it will not go and run the Human class code.

You create a main method, put code inside it, and when you run that class, ONLY what is inside the main will execute.

public class Human {
	private String name;
	
	public Human(String name){
		this.name = name;
	}
	
	public String getName(){
		return name;
	}
	public String toString(){ 
		return "Human " + getName(); 
		}

	public static void main(String [] args) {
             System.out.println("This is a program");
        }
	
}
class IndustrialPlumber extends Human implements Plumber{
	public IndustrialPlumber(String name){
		super(name);
	}
	
	public String unplugDrain(){
		return "IP: We'll add in to out list";
		
	}

         public static void main(String [] args) {
             System.out.println("This is another program");
        }
}

You can run both classes because they have a main method. When each of them runs, it will execute only the code you put inside the main.

For better design and maintenance we put the main method in a separate class. We don't have to, but it is better to keep things separately. Not to mix code. You have the Human class; for better design keep only code that has to do with humans inside it.

So you can try this:

public class MainMethodClass {

  public static void main(String [] args) {

     Human h1 = new Human("George");
     HandyPerson h2 = new HandyPerson("Mike");

    System.out.println("h1: "+h1);
    System.out.println("h2: "+h2);

    // NOW CHECK THIS OUT
    Human h3 = new Human("A");
    Human h4 = new NobodySpecial("B");

    System.out.println("h3: "+h3);
    System.out.println("h4: "+h4); // check out what this prints


//------------------------------------

    Plumber p1 = new HandyPerson("C");
    Plumber p2 = new IndustrialPlumber("C");
    
   // check out what those print
    System.out.println("p1: "+p1.unplugDrain());
    System.out.println("p2: "+p2.unplugDrain());
 
  }
}

i got it..thanks dude!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.