Question

In: Computer Science

Explain an Is-a relationship between classes in Java

Explain an Is-a relationship between classes in Java

Solutions

Expert Solution

Is-a relationship between classes:

Inheritance is Java can be done by implementing either Class Inheritance or Interface Inheritance. The notion of Is-a of classes is based on the Inheritance in Java. In Java, we can implement Is-a relationship between classes using the extends keyword or implements keyword in a class declaration.

It signifies that a child class will inherit all attributes and methods of the parent class, and this relationship is unidirectional in nature, for example, A car is a vehicle. But the reverse is not true, ie All vehicles are not a car.

Example:

package transport;

//Declare a parent class Vehicle
class Vehicle{    
    private double price; 
    public void setPrice(double  price) {
        this.price= price;
    }
    public double getPrice() {
        return price;
    }
}

//Car has Is-a relationship with Vehicle
//Car inherits price from parent class Vehicle

Class Car extends Vehicle{
   private String colour;
   public void setColour(String colour) {
        this.colour= colour;
    }
   public void getColour() {
        return colour;
    }
}

Related Solutions

In JAVA, Explain how classes properly handle data hiding and implementation hiding
In JAVA, Explain how classes properly handle data hiding and implementation hiding
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate...
java Write our Test Driver program that tests our Classes and Class Relationship How we calculate Net Pay after calculating taxes and deductions taxes: regNetPay = regPay - (regPay * STATE_TAX) - (regPay * FED_TAX) + (dependents * .03 * regPay ) overtimeNetPay = overtimePay - (overtimePay * STATE_TAX) - (overtimePay * FED_TAX) + (dependents * .02 * overtimePay ) Example printPayStub() output: Employee: Ochoa Employee ID: 1234 Hourly Pay: $25.00 Shift: Days Dependents: 2 Hours Worked: 50 RegGrossPay: $1,000.00...
Explain the relationship between GAST and UT1? What is the relationship between UT1 and UTC?
Explain the relationship between GAST and UT1? What is the relationship between UT1 and UTC?
Which statement is false regarding risk and return? a. For broad asset classes, the relationship between...
Which statement is false regarding risk and return? a. For broad asset classes, the relationship between risk and return is nearly linear. b. Adding multiple shares to a portfolio can reduce non-systematic risk. C. There is a nearly linear relationship between risk and return for individual shares. d. Because investors can easily eliminate risk through diversification, investors should only be rewarded for non-diversifiable risk.
Explain the relationship between ovary and fruit
Explain the relationship between ovary and fruit
Explain the relationship between vasodilation and inflammation.
Explain the relationship between vasodilation and inflammation.
Explain the relationship between important exports give an example of this relationship
Explain the relationship between important exports give an example of this relationship
Relationship between GPA and frequency of attending classes Most students think that GPA is very important....
Relationship between GPA and frequency of attending classes Most students think that GPA is very important. Also, it is generally agreed that there is a positive association between GPA and frequency of attending classes. How could you provide evidence for the positive association?
Explain the difference between a positive linear relationship, a negative linear relationship, and a nonlinear relationship...
Explain the difference between a positive linear relationship, a negative linear relationship, and a nonlinear relationship and give an example of each.
Explain marginal cost. Elaborate on the relationship between the two. Elaborate the relationship between marginal cost...
Explain marginal cost. Elaborate on the relationship between the two. Elaborate the relationship between marginal cost and average cost.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT