Composition has-a Relationship
One of the goals of OOP languages is reusability. Java gives good importance for reusability. A method in Java can be called in two ways.
- With the objects of the same class (through composition)
- With the objects of other classes (through inheritance)
Here we discuss the composition.
class Bird
{
public void eat()
{
System.out.println("All birds eat for their metabolic activities");
}
}
public class Parrot
{
public static void main(String args[])
{
Bird b1 = new Bird();
b1.eat();
}
}
Before going to composition, let us know how to write the above code and on which name the Java file should be opened. The rule says, in a single source code, if a number of classes exist, only one class can be public. The file name must be of that class which is public. In the code, two classes exist – Bird and Parrot. As Parrot is public, the file opened must be on the name of "Parrot.java". So, open "Parrot.java" file and write the source code and compile as usual as "javac Parrot.java" and execute as "java Parrot".
The Parrot class, in its main() method, created an object of Bird and called Bird's method eat(). Here, Bird and Parrot are no way connected or related (I mean, no inheritance). Just they happened to be in the same folder. This feature we call as "composition". Creating another class object in our class and calling other class methods is known as composition.
Composition has-a Relationship
Composition uses the concept called "has-a" relationship. We say, our class has a object of other class. Parrot "has a " object of Bird. Composition is meant for reusability.
When compiled Parrot.java, we get two .class files – Bird.class and Parrot.class. These classes work independently of each other even though they are coming from a single source file. Any other third class can make use of the methods of either both or any one these classes by composition.
There is one more concept called aggregation which differs slightly from composition. To know their narrow difference see Aggregation and Composition.
Appreciate you work Mr.Nageswara Rao.
In this website having servlet,JSP,spring and struct meterials also.
class bike {
public static void petrol() {
System.out.println(“bikes run with petrol”);
}
}
public class Vehicle {
public static void main(String args[]) {
bike b = new bike();
petrol();
}
}
Hi sir we have a rule from static keyword that we can call static methods with out object but in this program it is giving a compilation error by calling petrol method with out object can u plzz guide me where am wrong??
Write as bike.petrol(); it works. If you write petrol(), the compiler checks in the current class of Vehicle. As it is not there in Vehicle, it gives error.
i found an amazing stuff in this site great job sir..
you are “spreading smiles to the world”……..
please keep it continue
Thank you. Pl tell to your friends also to share the knowledge. This is the happiness I want from you. As a lecturer, trainer and author, I can do only this much to the world.
thank………..u
This is one of the website for the java programming ,Please provide the some code example example.
sir new keyword is used to create memory for objects but for instance variable does this new create memory or not
No. When an object is created all the instance variables are linked to the object and memory given with default values, if instance variables are not initialized.
the materials in this web site very use full to me.thank u sir
Done a great job Sir.
sir,this site is a boon to programmers specially new one’s learning java. great work..god bless you
Oh, Thank you. In fact, this web site is developed for my students’ children spread all over the globe. My students requested me this.
It is very useful for me and easy to understand. Thank you.