Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Inheritance and Composition

Have Programming Question? Ask it here!
 
 
 


sassy
Inheritance and Composition
2 Answer(s)      5 years and 8 months ago
Posted in : Java Beginners



Advertisement
View Answers

January 12, 2009 at 5:11 AM


Hi friend,


As you progress in an object-oriented design, you will likely encounter objects in the problem domain that contain other objects. In this situation you will be drawn to modeling a similar arrangement in the design of your solution. In an object-oriented design of a Java program, the way in which you model objects that contain other objects is with composition, the act of composing a class out of references to other objects. With composition, references to the constituent objects become fields of the containing object.

For example, it might be useful if the coffee cup object of your program could contain coffee. Coffee itself could be a distinct class, which your program could instantiate. You would award coffee with a type if it exhibits behavior that is important to your solution. Perhaps it will swirl one way or another when stirred, keep track of a temperature that changes over time, or keep track of the proportions of coffee and any additives such as cream and sugar.

To use composition in Java, you use instance variables of one object to hold references to other objects. For the CoffeeCup example, you could create a field for coffee within the definition of class CoffeeCup,

class CoffeeCup {

private Coffee innerCoffee;

public void addCoffee(Coffee newCoffee) {
// no implementation yet
}

public Coffee releaseOneSip(int sipSize) {
// no implementation yet
// (need a return so it will compile)
return null;
}

public Coffee spillEntireContents() {
return null;
}
}

public class Coffee {

private int mlCoffee;

public void add(int amount) {
// No implementation yet
}

public int remove(int amount) {
// No implementation yet
// (return 0 so it will compile)
return 0;
}

public int removeAll() {
// No implementation yet
// (return 0 so it will compile)
return 0;
}
}



January 12, 2009 at 5:13 AM


Inheritence using in java

<HTML>
<HEAD>
<TITLE>demo</TITLE>
<SCRIPT>

function GetCookie(sName){
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++) {
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}
//cookie with the requested name does not exist
return null;
}
</SCRIPT>
<SCRIPT language="javascript" for="oBody" event="onload()">
form1.hiddenelement.value = GetCookie("us")
</SCRIPT>
</HEAD>
<BODY id="oBody">
<FORM name="form1">
<INPUT type="hidden" name="hiddenelement"/>
</FORM>
</BODY>
</HTML>




When a subclass is derived simply from it's parent class then this mechanism is known as simple inheritance. In case of simple inheritance there is only a sub class and it's parent class. It is also called single inheritance or one level inheritance

class TextDemo {
int x;
int y;


int get(int p, int q){
x=p; y=q; return(0);
}
void Show(){
System.out.println(x);
}
}

class Test extends TextDemo{
public static void main(String args[]){
TextDemo a = new TextDemo();
a.get(5,6);
a.Show();
}
void display(){
System.out.println("B");
}
}
----------------------------------------------------------

Visit for more information:

http://www.roseindia.net/java/language/inheritance.shtml

Thanks

Amardeep


Related Tutorials/Questions & Answers:
Inheritance and Composition - Java Beginners
inheritance class TextDemo { int x; int y; int get(int p, int q...Inheritance and Composition  import java.util.*; public class... is with composition, the act of composing a class out of references to other objects
Inheritance and Composition - Java Beginners
Inheritance and Composition  CAN YOU FIND THE ERROR OF MY CODE JAVA.THIS IS A TEST CLASS.I CANT FIND THE ERROR,CAN YOU FIX IT FRIEND.THANK YOU SO MUCH. import java.util.*; public class TestPet { public static void main
Advertisements
Inheritance and Composition - Java Beginners
Inheritance and Composition  import java.util.*; public class TestPet { public static void main(String[]kevs) { Scanner console=new Scanner(System.in); String type, name, home; int weight, years; char gender
Inheritance and Composition - Java Beginners
Inheritance and Composition  HELLO FRIEND THIS IS THE CODE,CAN YOU FIND WHAT IS THE ERROR AND IF ITS OK CAN YOU FIX IT .THANK YOU. public class Pet extends Animal { private String home, bite; //private Boolean bites
Inheritance and Composition - Java Beginners
Inheritance and Composition  HELLO FRIEND THIS IS THE CODE,CAN YOU FIND WHAT IS THE ERROR AND IF ITS OK CAN YOU FIX IT .THANK YOU.10000X THANK YOU.HOPE YOU HELP ME. public class Animal { private int lifeExpectancy, weight
Inheritance and Composition - Java Beginners
Inheritance and Composition  HELLO FRIEND THIS IS THE CODE,CAN YOU FIND WHAT IS THE ERROR AND IF ITS OK CAN YOU FIX IT .THANK YOU. THANK YOU.HOPE YOU HELP ME.i think theres one error can u fix the error friend .thank u public
inheritance
inheritance  how does one implement inheritance in java
What is composition? - Java Beginners
What is composition?  Hi, What is composition? Give example of composition. Thanks
inheritance
inheritance  hi.. pleaseeeeee i need a quick help(answer) in creating aprogrm in java by using inheritance for car in such away that .... Car is parent class and both of Private and Public Car extends it then both of Bus
Inheritance,Inheritance in Java,Java Inheritance
Inheritance       To know the concept of inheritance clearly you must have the idea of class and its... this, super etc. As the name suggests, inheritance means to take something
multiple inheritance.
multiple inheritance.  hello, can java support multiple inheritance???   hi, java does not support multiple inheritance
Inheritance
Inheritance       To know the concept of inheritance clearly you must have the idea of class and its... this, super etc. As the name suggests, inheritance means to take something
multiple inheritance
multiple inheritance  why java doesn't support multiple inheritance? we can achieve this by using interface but not with classes? what is the main reason that java community implemented like
Java inheritance
Java inheritance   can the child access protected content of the parent if the child is in a different project   If you have considered different project as different package then the child cannot access the content
Multiple Inheritance
Multiple Inheritance  Why Java Doesn't have Multiple Inheritance It is Advantage or Disadvantage.........   Hi Friend, When we extends... and inconsistencies Java does not support Multiple Inheritance. Thanks
Multiple Inheritance
Multiple Inheritance  All are saying java doesn't support multiple inheritance but by default Object class is super class for all the user defined... class so java any how do multiple inheritance? Can you please explain
javascript inheritance framework
javascript inheritance framework  javascript inheritance framework
Inheritance question?
Inheritance question?  Q3)Create a class hierarchy as Company extended by AccountsDepartment. AccountsDepartment extended by AccountsReceivable and AccountsPayables. The Company performs following operations. (1
Inheritance question?
Inheritance question?  Q3)Create a class hierarchy as Company extended by AccountsDepartment. AccountsDepartment extended by AccountsReceivable and AccountsPayables. The Company performs following operations. (1
Inheritance
Inheritance       To know the concept of inheritance clearly you must have the idea of class and its... this, super etc. As the name suggests, inheritance means to take something
Inheritance in Spring
Inheritance in Spring       Inheritance Demo, In the example given below we are going to tell about the inheritance in the Spring framework. By inheritance we mean a way
Inheritance in java - Java Beginners
Inheritance in java  I want a example program using the concept of (simple and multiple) inheritance
Inheritance
other features like creation and implementation of the object, Inheritance etc...; float p, q;   public Rectangle...(){   return(p * q);   
Inheritance in java - Java Beginners
Inheritance in java  I want a example program using the concept of (single and multiple) inheritance.  Hi Friend, Please visit the following link: http://www.roseindia.net/java/language/inheritance.shtml Thanks
inheritance - Java Beginners
inheritance  help me on inheritance concept in java specialy on overloading and overridding concept
Concept of Inheritance in Java
Concept of Inheritance in Java Concept of Inheritance in Java is considered one of the important features of Object Oriented Programming. Inheritance... class from parent class. Let's understand the concept of inheritance in Java
creating inheritance of cars
creating inheritance of cars   hi ... please help me in creating inheritance between some types of cars like car that is parent and both of private car and public car are extends it then bus and truck extends public car
Maven's order of inheritance
Maven's order of inheritance  hello, What is Maven's order of inheritance?   hii, 1.parent pom 2.project pom 3.settings 4.CLI parameters
PHP Inheritance Class
composition over inheritance.  In the following example we will come...Inheritance in PHP: Inheritance is a property of Object Oriented Programming... not use the inheritance. We must not use inheritance when we have only different
how can i achieve multiple inheritance in java without using inheritance ?
how can i achieve multiple inheritance in java without using inheritance ?  how can i achieve multiple inheritance in java without using inheritance
Inheritance in Java 7
This tutorial describe concept of Inheritance. It is one of OOPs concept
multiple inheritance - Java Beginners
multiple inheritance  Why java does not support Multiple Inheritance?  First of all the question is wrong the correct one is......... Q)why java classes does not support Multiple Inheritance? JAVA CLASSES ONLY
What type of inheritance that php supports?
What type of inheritance that php supports?  What type of inheritance that php supports
What are the types of inheritance models in Hibernate?
What are the types of inheritance models in Hibernate?  Hi, What are the types of inheritance models in Hibernate? thanks
Inheritance - Java Beginners
Inheritance  pls help me!!! Question: write an inheritance hierachy for classes Quadrilateral,Trapezoid,Parallelogram, Rectangle and Square. Use Quadrilateral as superclass. make hierachy as deep as possible. Specify
Java Inheritance Concept.
Java Inheritance Concept.  Yesterday i got confused with following question in an aptitude exam. Question:Class A,B,C have method named doit(). Class B extends Class A,Class C extends Class B.How will you call method doit
Inheritance java Example
Inheritance java Example  How can we use inheritance in java program?   class Animal { public void eat(String str... of inheritance. Inheritance is a way of establishing a subtype from an existing object
Inheritance in Java
Inheritance, one of the important concepts of Object Oriented Programming.... Benefit of using inheritance: A code can be used again and again Inheritance in Java enhances the properties of the class, which means
Inheritance Overloading - Java Beginners
Inheritance Overloading  The problem is: I have this program: class Base{ int i = 1; int j = 2; Base(int i, int j){ i = i; j = j; } void m(){ System.out.println(i + j); } } class Child extends
Inheritance problem - Java Beginners
Inheritance problem  hello friend can u please help me that why this program is printing show method of Base_Class b=5 even though i am assigning the Base_Class object into the Super_Class reference i.e Super_Class bc=new
doubt in inheritance program - Java Beginners
doubt in inheritance program  how will we get the result 6 2 5 in the inheritance program in the given example i got 6 &2 but i am confused about 5
Inheritance in Spring
Inheritance in Spring       Inheritance Demo, In the example given below we are going to tell about the inheritance in the Spring framework. By inheritance we mean a way
Inheritance in Spring
Inheritance in Spring       Inheritance Demo, In the example given below we are going to tell about the inheritance in the Spring framework. By inheritance we mean
Inheritance in Hibernate - Hibernate Interview Questions
Inheritance in Hibernate  Hi, Can we do everything in servlet as in jsp. Thanks Prakash
Inheritance Example In Java
Inheritance Example In Java In this section we will read about the Inheritance using a simple example. Inheritance is an OOPs feature that allows to inherit the features of parent into the child. Inheritance allows the reusability
Inheritance, abstract classes
Inheritance, abstract classes  Hi. I wish to thank you for answering my last question, but I had figured it out, but the GUI was very helpful. I could use someones help again with abstracts & a demo account. I know you
Inheritance - Java Beginners
of Inheritance within the java programming. class Points{ private double x
Advertisements
 

 

 

DMCA.com