Tuesday 25 August 2015

Java Tutorial : Inheritance Has A Relationship (Person)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=p_4U_LVMbSk&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Inheritance Has A Relationship (Person)
Java Tutorial : Inheritance Has A Relationship (Person)
Java Tutorial : Inheritance Has A Relationship (Person)
Java Tutorial : Inheritance Has A Relationship (Person)
Java Tutorial : Inheritance Has A Relationship (Person)
Person.java
public class Person
{

    private String name;
    private int age;

    public Person(String name, int age)
    {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    @Override
    public String toString()
    {
        return "Person [name=" + name + ", age=" + age + "]";
    }

    
}
Address.java
public class Address
{
    private String street;
    private String city;
    private String state;
    private String zip;

    public Address(String street, String city, String state, String zip)
    {
        super();
        this.street = street;
        this.city = city;
        this.state = state;
        this.zip = zip;
    }

    public String getStreet()
    {
        return street;
    }

    public void setStreet(String street)
    {
        this.street = street;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getState()
    {
        return state;
    }

    public void setState(String state)
    {
        this.state = state;
    }

    public String getZip()
    {
        return zip;
    }

    public void setZip(String zip)
    {
        this.zip = zip;
    }

    @Override
    public String toString()
    {
        return "Address [street=" + street + ", city=" + city + ", state="
                + state + ", zip=" + zip + "]";
    }

}
Employee.java
/**
 * 
 * Employee class extends Person class means Employee IS-A Person.
 * 
 * Employee extends Person and thus inherits all methods and properties from
 * Person (except final and static).
 * 
 * Employee can also define all its specific functionality.
 */
public class Employee extends Person
{
    private String departmentName;
    private int employeeId;

    /*
     * Employee HAS-A Address
     */
    private Address address;

    public Employee(String name, int age, String departmentName,
            int employeeId, Address address)
    {
        super(name, age);
        this.departmentName = departmentName;
        this.employeeId = employeeId;
        this.address = address;
    }

    public String getDepartmentName()
    {
        return departmentName;
    }

    public void setDepartmentName(String departmentName)
    {
        this.departmentName = departmentName;
    }

    public int getEmployeeId()
    {
        return employeeId;
    }

    public void setEmployeeId(int employeeId)
    {
        this.employeeId = employeeId;
    }

    public Address getAddress()
    {
        return address;
    }

    public void setAddress(Address address)
    {
        this.address = address;
    }

    @Override
    public String toString()
    {
        return "Employee [departmentName=" + departmentName + ", employeeId="
                + employeeId + ", address=" + address + ", getName()="
                + getName() + ", getAge()=" + getAge() + "]";
    }

}
Student.java
/**
 * 
 * Student class extends Person class means Student IS-A Person.
 * 
 * Student extends Person and thus inherits all methods and properties from
 * Person (except final and static).
 * 
 * Student can also define all its specific functionality.
 */
public class Student extends Person
{

    private double grade;
    private int rollNo;

    /*
     * Student HAS-A Address
     */
    private Address address;

    public Student(String name, int age, double grade, int rollNo,
            Address address)
    {
        super(name, age);
        this.grade = grade;
        this.rollNo = rollNo;
        this.address = address;
    }

    public double getGrade()
    {
        return grade;
    }

    public void setGrade(double grade)
    {
        this.grade = grade;
    }

    public int getRollNo()
    {
        return rollNo;
    }

    public void setRollNo(int rollNo)
    {
        this.rollNo = rollNo;
    }

    public Address getAddress()
    {
        return address;
    }

    public void setAddress(Address address)
    {
        this.address = address;
    }

    @Override
    public String toString()
    {
        return "Student [grade=" + grade + ", rollNo=" + rollNo + ", address="
                + address + ", getName()=" + getName() + ", getAge()="
                + getAge() + "]";
    }

}
RelationshipDemo.java
public class RelationshipDemo
{

    public static void main(String[] args)
    {

        Address peterAddress = new Address("13th Cross", "Bangalore",
                "Karnataka", "560001");

        Employee peter = new Employee("Peter", 33, "Income Tax Department",
                350, peterAddress);
        
        System.out.println("Employee Peter Details : ");
        System.out.println(peter);
        
        System.out.println("----------------------------------------------");
        
        Address johnAddress = new Address("17th Cross", "Chennai",
                "TamilNadu", "600100");

        Student john = new Student("John", 33, 3.5,
                2, johnAddress);
        
        System.out.println("Student john Details : ");
        System.out.println(john);


    }
}
Output
Employee Peter Details : 
Employee [departmentName=Income Tax Department, employeeId=350, 
address=Address [street=13th Cross, city=Bangalore, 
state=Karnataka, zip=560001], getName()=Peter, getAge()=33]
----------------------------------------------
Student john Details : 
Student [grade=3.5, rollNo=2, address=Address [street=17th Cross,
city=Chennai, state=TamilNadu, zip=600100], getName()=John, 
getAge()=33]
To Download InheritanceDemo-HAS-A-PersonApp Project Click the below link
https://sites.google.com/site/javaee4321/java/InheritanceDemo-HAS-A-PersonApp.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • 29 comments:

    1. I appreciate you sharing this wonderful content. It's exactly what I was hoping to see, and I sincerely hope you'll keep publishing great posts like this in the future.
      Sports Injury Rehabilitation in Surrey

      ReplyDelete
    2. I appreciate you sharing this wonderful content. It's exactly what I was hoping to see, and I sincerely hope you'll keep publishing great posts like this in the future.
      Sports Injury Rehabilitation in Surrey

      ReplyDelete
    3. Great blog post! Your insights are truly valuable and well-expressed. I appreciate the depth of information and the engaging writing style. Looking forward to more enlightening content from your blog in the future. If you are looking for professional Job oriented course like
      Python Training in noida
      Data Science Training in noida
      full stack development training in noida
      Software Testing Training in Noida
      Digital Marketing Training in Noida

      ReplyDelete
    4. Great Post! I found your insights really interesting. Looking forward to more from you.If you are looking for job oriented courses then visit Appwars Technologies provides these courses:-
      Best Django training

      ReplyDelete
    5. Great Post! I found your insights really interesting. Looking forward to more from you.If you are looking for job oriented courses then visit Appwars Technologies provides these courses:-
      Best Django training

      ReplyDelete
    6. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
      https://coursejet.com/digital-marketing-courses-in-chennai/

      ReplyDelete
    7. Fantastic post! Your writing style is informative yet brief, and I value the useful ideas you have shared. Anticipating more interesting posts from you. Keep going!
      wall decoration items for pooja

      ReplyDelete
    8. Fantastic post! Your writing style is informative yet brief, and I value the useful ideas you have shared. Anticipating more interesting posts from you. Keep going!
      wall decoration items for pooja

      ReplyDelete
    9. Such a interesting content and information about it . Thanks for Posting

      Data Analytics Training in Noida

      ReplyDelete
    10. Such a interesting content and information about it . Thanks for Posting

      Data Science Training in Noida

      ReplyDelete
    11. What a fascinating blog post! Concise but thorough, the information offers insightful information. I like how easily understood and interesting the author's writing is. Anticipating more
      buy antique table decor

      ReplyDelete
    12. What a fascinating blog post! Concise but thorough, the information offers insightful information. I like how easily understood and interesting the author's writing is. Anticipating more
      buy antique table decor

      ReplyDelete