hello to all!!!

i really don't have any problem this time, but there's something i wanna learn more about!
we are discussing classes and inheritance now!! my professor have given us quite examples of it....but it wasn't that clear!!

can i ask for a simple codes that implements a class you made on your main program!

thanks for any replies!!
God bless!

package testpackage;

/**
 *
 * @author stamatios.bardanis
 */
public class Person {
    private String name=null;
    
    public Person() {
        name="";
    }
    
    public Person(String na) {
        name = na;
    }

    public String getName() {
        return name;
    }

    public void setName(String na) {
        name = na;
    }

    public String toString() {
        return "My name is: "+name;
    }
    
}
package testpackage;

/**
 *
 * @author stamatios.bardanis
 */
public class Main {
    public static void main(String [] args) {
        Person p1 = new Person();
        p1.setName("Andersson");
        System.out.println("Mr "+ p1.getName()+" welcome back. We missed you");
        
        System.out.println();
        
        Person p2 = new Person("Neo");
        System.out.println(p2.toString());
    }
}

And for inheritance:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testpackage;

/**
 *
 * @author stamatios.bardanis
 */
public class Athlete extends Person {
    private int height=0;
    
    public Athlete() {
        super();
        height=0;
    }
    
    public Athlete(int he) {
        super();
        height=he;
    }
    
    public Athlete(String na) {
        super(na);
        height=0;
    }
    
    public Athlete(String na, int he) {
        super(na);
        height=he;
    }

    public void setHeight(int he) {
        height = he;
    }

    public int getHeight() {
        return height;
    }

    @Override
    public String toString() {
        return "The athlete: "+getName()+" has height: "+height+" cm";
    }
    
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testpackage;

/**
 *
 * @author stamatios.bardanis
 */
public class Main2 {
    public static void main(String [] args) {
        Athlete a1 = new Athlete();
        a1.setName("Andersson");
        a1.setHeight(180);
        System.out.println("Name: "+a1.getName());
        System.out.println("Height: "+a1.getHeight());
        
        System.out.println();
        
        Athlete a2 = new Athlete("Superman",200);
        System.out.println(a2.toString());
    }
}

Check what I call. Then check the code of what I call and what it returns
Any questions?

hello to all!!!

i really don't have any problem this time, but there's something i wanna learn more about!
we are discussing classes and inheritance now!! my professor have given us quite examples of it....but it wasn't that clear!!

can i ask for a simple codes that implements a class you made on your main program!

thanks for any replies!!
God bless!

public class User {

	protected String userName;
	protected String password;
	protected String eMail;
        private String aString = "I won't be inherited!!!";//It really won't

	public User(){ 
	}
	
	public User(User user) {
		this.id = user.id;
		this.userName = user.userName;
		this.password = user.password;
		this.eMail = user.eMail;		
	}
	
	public User( String userName, String password ){
		this.userName = userName;
		this.password = password;
	}
	public String getUserName(){
		return userName;
	}

	public String getPassword(){
		return password;
	}

	public String getEMail(){
		return eMail;
        }
	public void setUserName( String userName ){
		this.userName = userName;
	}
	
	public void setPassword( String password ){
		this.password = password;
	}

	public void setEMail( String eMail ){
		this.eMail = eMail;
	}
}
public class Administrator extends User {
	private String firstName;
        private String lastName;
	public Administrator() {
		super();
	}

	public Administrator(String username, String password) {
		super(username, password);
	}

	public Administrator(String username, String password, String firstName,
			String lastName) {
		super(username, password)
               this.firstName = firstName;
               this.lastName = lastName;
	}
	
}

The getUsername() and getPassword() methods will be available in the subclass too. The super statement (if present) is the first statement of a constructor. Protected fields will be inherited, they are invisible from outside, just like private fields (but privates won't be inherited).

Now pay REAL attention to this:

package testpackage;

/**
 *
 * @author stamatios.bardanis
 */
public class Main3 {
    public static void main(String [] args) {
        Person a = new Person("John");
        Person b = new Athlete("John", 178); 
        
        System.out.println(a.toString());
        System.out.println(b.toString());
    }
}

Both a and b are Persons:
Person a = ...
Person b = ...
And I call the same method:
a.toString()
b.toString()

BUT for object a, the Person toString() will be called.
For object b, the Athlete toString() will be called.
Check what the toString() methods of those objects do and what is called.

An Athlete object is a Person. Meaning it can do whatever a Person can do. The Athlete object has access to all the public methods of Person

But this would be wrong:

Athlete c = new Person ("John");

Because a Person is not an Athlete. The Person cannot call the Athlete's methods. How could it?

What could be the possible line of code if i were to ask the user to input names?

should i still use buffered reader on the main part?

If you want to give only once input the use the args of the main. This happens when you run the program

Use BufferedReader when you don't know how many inputs the user will give.
If you have a menu where the user selects options you don't know when the user will decide to stop selecting. So you will use BufferedReader. In that way you read input while the program is running

This question has already been answered. Start a new discussion instead.