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

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.
Create the following classes and show a “Hierarchical” relationship between them:           Company, Products and Employee...
Create the following classes and show a “Hierarchical” relationship between them:           Company, Products and Employee    Define at least 2 instance variables of these classes. company: company_code, company_name Employee: employee_ name, number_of_employees. Choose the appropriate members for Products by your own understanding. Write default and overloaded constructors for each class Include a method in all the classes by the same name that overrides the direct or indirect base classes.
In JAVA, Explain how classes properly handle data hiding and implementation hiding
In JAVA, Explain how classes properly handle data hiding and implementation hiding
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
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...
in java language. There are three classes for the project, and two inner classes defined in...
in java language. There are three classes for the project, and two inner classes defined in the Cuboid class. Create the following classes: •   Shape. It’s an abstract class with two abstract methods, area and perimeter. •   Rectangle. It’s a concrete class that extends Shape. Implement area() and perimeter(). Implement the compareTo(object) method to sort rectangles by area in ascending order. •   Cuboid. It’s a concrete class that extends Rectangle. A cuboid is a 3D rectangle. The shape has a...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT