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
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge