Wednesday, June 28, 2017

Simple Inheritance






public class HelloWorld{
    public static void main(String args[]){
        Greet msg=new Greet();
        System.out.println(msg.getMessage());
        System.out.println(msg.getThankYou());
        msg.getMyMessage();
    }
}
public class Greet extends Message{
       private String msg=new String("Computer Science - ");
       public Greet(){
           super();
        }
    public String getMessage(){
        return msg+getWord();
    }
}

public class Message extends Goodbye implements Welcome {
       private String word;
       public Message(){
           System.out.println("Did you just call me?");
           word="Hello World";
        }
    public final String getWord(){   //try to override this in message
        return word;
    }
    public void greetMe(String m){
        System.out.println("Welcome to my application");
    }
    public String getThankYou() {
        return thanks;
    }
}
public abstract class Goodbye{
    protected String thanks="Thank you for visiting us";
    public abstract String getThankYou();
    public void getMyMessage(){
        System.out.println("This is from the abstract class");
    }
}

public interface Welcome{
    void greetMe(String m);
    //only default and public class modifier can be used
    //try to copy a method with body - fail
}

No comments:

Post a Comment