What is the difference between IS -A relationship and HAS-A relationship in Java? - Stack Overflow
In Java, a Has-A relationship is also known as composition. reference to an instance of another class or an other instance of the same class. Aggregation is also called a “Has-a” relationship. It need not be the same vice- versa. When one class .. Could you please share with java code examples. . 1 )Is association class same as ordinary classs? Explain with. Although the compiler and Java virtual machine (JVM) will do a lot of work for The UML diagram showing the composition relationship has a darkened . For example, you can't add to a subclass a method with the same.
Use inheritance only if there is a clear hierarchical relationship between classes. It comes from Greek word "poly" means many and "morphos" means form.
Java - Inheritance
For examples, in chemistry, carbon exhibits polymorphism because it can be found in more than one form: But, each of the form has it own distinct properties and price. Substitutability A subclass possesses all the attributes and operations of its superclass because a subclass inherited all attributes and operations from its superclass. This means that a subclass object can do whatever its superclass can do. As a result, we can substitute a subclass instance when a superclass instance is expected, and everything shall work fine.
This is called substitutability. In our earlier example of Circle and Cylinder: Cylinder is a subclass of Circle. We can say that Cylinder "is-a" Circle actually, it "is-more-than-a" Circle. Subclass-superclass exhibits a so called "is-a" relationship. The reference c1, however, retains its internal identity.
In our example, the subclass Cylinder overrides methods getArea and toString. This is because c1 is in fact holding a Cylinder object internally. Summary A subclass instance can be assigned substituted to a superclass' reference.
Once substituted, we can invoke methods defined in the superclass; we cannot invoke methods defined in the subclass. However, if the subclass overrides inherited methods from the superclass, the subclass overridden versions will be invoked.
Consider the following example. Suppose that our program uses many kinds of shapes, such as triangle, rectangle and so on.
We should design a superclass called Shape, which defines the public interfaces or behaviors of all the shapes.
For example, we would like all the shapes to have a method called getAreawhich returns the area of that particular shape. The Shape class can be written as follow. We shall print an error message for the time being.
In the later section, I shall show you how to resolve this problem. We can then derive subclasses, such as Triangle and Rectangle, from the superclass Shape. A Test Driver TestShape. You could instantiate different subclass instance, and the code still works.
You could extend your program easily by adding in more subclasses, such as Circle, Square, etc, with ease. Nonetheless, the above definition of Shape class poses a problem, if someone instantiate a Shape object and invoke the getArea from the Shape object, the program breaks. We do not want anyone to instantiate a Shape instance.
This problem can be resolved by using the so-called abstract class. For example, in our game app, we have many types of monsters that can attack. We shall design a superclass called Monster and define the method attack in the superclass.
The subclasses shall then provides their actual implementation. In the main program, we declare instances of superclass, substituted with actual subclass; and invoke method defined in the superclass. This is because, in a UML class diagram, subclass is often drawn below its superclass. Upcasting is always safe because a subclass instance possesses all the properties of its superclass and can do whatever its superclass can do.
The compiler checks for valid upcasting and issues error "incompatible types" otherwise. This is called "downcasting". Downcasting is not always safe, and throws a runtime ClassCastException if the instance to be downcasted does not belong to the correct subclass. A subclass object can be substituted for its superclass, but the reverse is not true. Point cannot be casted to Circle The "instanceof" Operator Java provides a binary operator called instanceof which returns true if an object is an instance of a particular class.
The syntax is as follows: When a superclass instance is expected, it can be substituted by a subclass instance. In other words, a reference to a class may hold an instance of that class or an instance of one of its subclasses - it is called substitutability. If a subclass instance is assign to a superclass reference, you can invoke the methods defined in the superclass only. You cannot invoke methods defined in the subclass. However, the substituted instance retains its own identity in terms of overridden methods and hiding variables.
If the subclass overrides methods in the superclass, the subclass's version will be executed, instead of the superclass's version.
IS-A and HAS-A relationship in Java
This can be resolved via abstract method and abstract class. An abstract method is a method with only signature i. You use the keyword abstract to declare an abstract method. For example, in the Shape class, we can declare abstract methods getAreadrawetc, as follows: How to compute the area if the shape is not known? Implementation of these abstract methods will be provided later once the actual shape is known. These abstract methods cannot be invoked because they have no implementation.
A class containing one or more abstract methods is called an abstract class. An abstract class must be declared with a class-modifier abstract. Shape and its Subclasses Let us rewrite our Shape class as an abstract class, containing an abstract method getArea as follows: The abstract Superclass Shape.
UML Association vs Aggregation vs Composition
Therefore, an abstract class cannot be instantiated. In other words, you cannot create instances from an abstract class otherwise, you will have an incomplete instance with missing method's body. To use an abstract class, you have to derive a subclass from the abstract class.
In the derived subclass, you have to override the abstract methods and provide implementation to all the abstract methods. The subclass derived is now complete, and can be instantiated.
If a subclass does not provide implementation to all the abstract methods of the superclass, the subclass remains abstract. This property of the abstract class solves our earlier problem.
In other words, you can create instances of the subclasses such as Triangle and Rectangle, and upcast them to Shape so as to program and operate at the interface levelbut you cannot create instance of Shape, which avoid the pitfall that we have faced. The purpose of an abstract class is to provide a common interface or protocol, or contract, or understanding, or naming convention to all its subclasses. For example, in the abstract class Shape, you can define abstract methods such as getArea and draw.
No implementation is possible because the actual shape is not known. However, by specifying the signature of the abstract methods, all the subclasses are forced to use these methods' signature. The subclasses could provide the proper implementations. Coupled with polymorphism, you can upcast subclass instances to Shape, and program at the Shape level, i,e. The separation of interface and implementation enables better software design, and ease in expansion.
For example, Shape defines a method called getAreawhich all the subclasses must provide the correct implementation. You can ask for a getArea from any subclasses of Shape, the correct area will be computed. Furthermore, you application can be extended easily to accommodate new shapes such as Circle or Square by deriving more subclasses.
Program at the interface, not at the implementation. That is, make references at the superclass; substitute with subclass instances; and invoke methods defined in the superclass only. An abstract method cannot be declared final, as final method cannot be overridden. An abstract method, on the other hand, must be overridden in a descendant before it can be used.
An abstract method cannot be private which generates a compilation error. This is because private method are not visible to the subclass and thus cannot be overridden. Monster We shall define the superclass Monster as an abstract class, containing an abstract method attack. The abstract class cannot be instantiated i. An interface contains only public abstract methods methods with signature and no implementation and possibly constants public static final variables.
You have to use the keyword "interface" to define an interface instead of keyword "class" for normal classes. The keyword public and abstract are not needed for its abstract methods as they are mandatory. JDK 8 introduces default and static methods in the interface. JDK 9 introduces private methods in the interface. These will not be covered in this article. Similar to an abstract superclass, an interface cannot be instantiated. You have to create a "subclass" that implements an interface, and provide the actual implementation of all the abstract methods.
Unlike a normal class, where you use the keyword "extends" to derive a subclass. For interface, we use the keyword "implements" to derive a subclass. An interface is a contract for what the classes can do. It, however, does not specify how the classes should do it.
An interface provides a form, a protocol, a standard, a contract, a specification, a set of rules, an interface, for all objects that implement it. It is a specification and rules that any object implementing it agrees to follow. In Java, abstract class and interface are used to separate the public interface of a class from its implementation so as to allow the programmer to program at the interface instead of the various implementation. Use an adjective typically ends with "able" consisting of one or more words.
Each word shall be initial capitalized camel-case. Shape Interface and its Implementations We can re-write the abstract superclass Shape into an interface, containing only abstract methods, as follows: Abstract classes, Interfaces and abstract methods are shown in italics. Implementation of interface is marked by a dash-arrow leading from the subclasses to the interface. Movable Interface and its Implementations Suppose that our application involves many objects that can move.
We could define an interface called movable, containing the signatures of the various movement methods. To use an interface, again, you must derive subclasses and provide implementation to all the abstract methods declared in the interface. The subclasses are now complete and can be instantiated. Subtraction a, b ; demo. That is why, using the object of the subclass you can access the members of a superclass. The Superclass reference variable can hold the subclass object, but using that variable you can access only the members of the superclass, so to access the members of both classes it is recommended to always create reference variable to the subclass.
If you consider the above program, you can instantiate the class as given below.
Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. The super keyword The super keyword is similar to this keyword.
Following are the scenarios where the super keyword is used. It is used to differentiate the members of superclass from the members of subclass, if they have same names. It is used to invoke the superclass constructor from subclass. Differentiating the Members If a class is inheriting the properties of another class. And if the members of the superclass have the names same as the sub class, to differentiate these variables we use super keyword as shown below.
We are invoking display method of both classes and printing the value of the variable num of both classes. Here you can observe that we have used super keyword to differentiate the members of superclass from subclass. But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below.
This program contains a superclass and a subclass, where the superclass contains a parameterized constructor which accepts a integer value, and we used the super keyword to invoke the parameterized constructor of the superclass.
Copy and paste the following program in a file with the name Subclass. This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance. Animal is the superclass of Reptile class.