Java Serialization With Inheritance And Aggregation
Feb 20, 2015What is difference between Java Serialization with Inheritance (IS-A Relationship) and Java Serialization with Aggregation (HAS-A Relationship) ....
View RepliesWhat is difference between Java Serialization with Inheritance (IS-A Relationship) and Java Serialization with Aggregation (HAS-A Relationship) ....
View Replieswhat is seriazable.But I am not able to come that why it is used and when should I declare my class(Object) as serialzable and when not?
View Replies View RelatedI am trying to save a state of a big GUI Swing app. I am trying to save the containers (ArrayLists and JPanels too ). But I am recieving an exceptions:
Java Code:
ilian.Quiz.MainApp$5
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1377)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1173)
at java.io.ObjectOutputStream.access$300(ObjectOutputStream.java:162)
[code]...
What is causing it? I guess anonymous classes like new Thread(new Runnable(...) will cause errors.
i was leaning inheritance and tried to implement it in Java.This is my base class vehicl.java
public class vehicle{
private int topSpeed;
private int cubicCap;
private String modelName;
private boolean hasAlloy;
[code]...
I also have a derived class called car.java.What i wanted to do in the derived class was that i wanted to override the constructor as well as the getInfo() method from the base class.I wanted to add an additional attribute to the car "numberSeats" and display tat too when the object to car class calls the getInfo() method .It showed an error "identifier required" when i tried to compile car.java program.
import java.util.Scanner;
public class car extends vehicle{
//int numberSeats;
//System.out.println("Enter the number of Seats");
Scanner numberSeats=new Scanner(System.in);
numberSeats=numberSeats.nextInt();
//System.out.println(numberSeats.nextInt());
[code]....
explain the errors that i get when i tried to compile car.java without using super keyword or without defining the constructor from the Car class ?
This is my assignment.
Identify how multiple inheritance is possible in Java with interfaces.
Write a java programme with appropriate classes to demonstrate the above.
Hint: both inheritance and interface concepts are necessary.
For the project name in netbeans, use your id and the name "assignment" separated by underscore,
E.g. 9876543_assignment
how can i implement multiple inheritance in java using interfaces. if interfaces have some methods having same name then how to distinguish that ?
View Replies View RelatedI'm making a vending machine program to practice inheritance and have a few questions. My superclass is a Soda class, and I'm making subclasses like "Orange soda", "Coke", etc. My first question is, what is the point of the subclass inheriting the instance variables of the superclass? If you have to define them again is there any point in the super class having them? Here is an example of this:
My superclass:
public abstract class Soda {
public double price;
public int numAvailable;
public String name;
public String machineCode;
[code]...
Besides not having to write the vendSoda() method again, what is the benefit of inheritance in a situation like this if you have to define all variables again? My second question is, how could I store all of the code strings from all of the different subclasses in one place? (so when the user enters a code it can search for the code entered to give the desired soda)...
how to do multiple inheritance without sing interface in java?
View Replies View RelatedI studied that java does not support multiple inheritance using classes. It is fine practically, but I have still a question in mind:Lets see the below code.
class Test{
------
------
}
class MyTest extends Test{
---------
---------
}
Here, as we know that that Object is Super class for every class in java. and I have already extends a Test class in MyTest.My question is: How is it possible to extend features of two class at the same time?
Theoretically,
MyTest extends Object &
MyTest extends Test.
What is serialization in Java ?how to use serialization in Singleton?
View Replies View RelatedI am studying Serialization from the SCJP 6 Kathy Sierra book. I came across this code snippet.
public class Cat implements Serializable {
public static void main(String[] args) {
Cat c = new Cat();
try {
FileOutputStream fs = new FileOutputStream("testSer.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(c);
[code]....
The output is as follows.
files.Cat@4d43691d
files.Cat@7f39ebdb
1) Why are the two hashcodes different?
2) Serialization is supposed to make and identical copy of any object and all its instance variables. So, if the hashcodes are different, are these objects located in different locations in heap?
Difference between Serialization Vs Externalization
View Replies View RelatedI'm building GUI and whole app is going to read and rewirte on xml file..
First I make function for read and write and they are working, but now I decide to make one class where I'm going to have get and set.. This is my class wehre I need to read everything from xm class CurrentData {
private String USERNAME = "username";
private String PASSWORD = "password";
private String STATUS = "status";
public String getusername() {
return username;
[Code] ....
Now my idea is when I start app that everything from xml is going to read from xml and trough get is going to show on my interface, when I edit I'm going to save and with function create I'm going to make same xml file with different parameters... how to use serialization...
Can you tell me or give a complete resource of file I/O, serialization!
View Replies View RelatedDoes child class gets a copy of the methods and variables of parent class?
public class test1 {
public static void main(String a[]) {
Child c = new Child();
c.print();
[Code] ....
why is the output 1?
I'm learning about inheritance and part of my problem is to create an Order with methods, then an UpdateOrder where the total price is changed by adding four dollars to it, and then a main method displaying a few orders. I've copied all three below in order. My question is when I run the program it will display the totalprice() first for the second order followed by name, number, etc.what you override always displayed first regardless of the order you put them in? (The issue is at line 31 on the third code.)
import javax.swing.JOptionPane;
public class Order { //superclass
private String customerName;
private int customerNumber;
protected int quantityOrdered;
protected double unitPrice;
protected double totalPrice;
[code]....
I am new to java i dont understand the difference between the abstract and inheritance i mean we use the abstract class with extends with other class name ,even we do that same in the inheritance pls tell me main difference between abstract and inheritance...
View Replies View Relatedhow inheritence and exception work together ?? what are the rules ??
View Replies View RelatedIf I define a class which contains a few static fields, and then have a few classes who inherit this class, then all these classes would have the static field as well. Now my question is the following: would all those sub classes (and the base class itself) share the same object, or would each class have one object for all it's instances?
View Replies View RelatedI've tried to write a package and two classes this way:
<path>/pack/Aclass.java
Java Code: package pack;
public class Aclass<T> {
private T t;
public void set(T t) {
this.t = t;
[code]....
I am in an intro programming class and we got assigned a problem for creating a super class with about a dozen sub classes for generating a random word(via WordGetter class) and then comparing that word to a variety of different patterns(like: does the word contain "re"). We were given the super class which looks like this...
public class Pattern {
public boolean matches(String text) {
return true;
}
public String toString() {
return "(TRUE)";
[code]...
and from this class, we have to write subclasses that override those three methods. I am struggling to understand inheritance and I am not really sure where to even start. Here is the instructions for the first sub class we need to write...
"CONTAINS" SUBCLASS
Constructor: The constructor accepts a String named ‘letters’.
Matches: This pattern matches any text that contains at least one occurrence of each ‘letter’.
toString: produces the text “(CONTAINS <LETTERS>)” where <LETTERS> is the ‘letters’ string.
getLetters(): this method must return letters.
equals(Object): careful on this one. Two Contains are equal if they have the same letters (order is not relevant).
(Example):
Pattern p = new Contains(“re”);
boolean f1 = p.matches(“renew”); // f1 is true
boolean f2 = p.matches(“zoo”); // f2 is false
String s = p.toString(); // s is “(CONTAINS re)”
boolean f3 = p.equals(new Contains(“er”)); // f3 is true.. really..
The first is clear , new Person().printPerson(); displays Person but for the second : new Student().printPerson(); it accesses the Student constructor that points to the Person class => object. It builds the Person instance then goes back to the Student constuctor .Both methods are private and to my knowledge invisible one to the other , except that you cant run the the Person one because it's private so the only one in the Student class is the Student one . Guess it 's incorrect , but why ? (is because private methods cant be overriden and somehow the super class one always has priority ? , even if it's private?)
public class Test {
public static void main(String[] args) {
new Person().printPerson();
new Student().printPerson();
[code]....
If i have 2 classes, Top and ClassB which extends Top
public class Top {
String variable;
public Top(){
setVariable();
}
void setVariable(){
variable = "variable is initialized in Main Class";
[code]....
So what is happening when ClassB inherits from Top?I know that the B constructor is calling super, so does that mean its calling setVariable (in Top?) but as its overridden in ClassB, then that is whats being called and setting the String variable?
Here is my abstract Boat class.
public abstract class Boat{
private int height;
private int length;
private int width;
private double boatValue;
private double chargeRate;
private Owner owner;
public Owner getOwner() {
return owner;
[Code] ....
Design Patterns are one form of reuse. so is inheritance. what are the similarities and difference between them?
View Replies View RelatedI am writing small pieces of code to make sure I understand Java basics and I have the following.
package teams1;
public abstract class Team1{
private String sport = new String();
public abstract String getSport();
public abstract void setSport();
}
import teams1.*;
[Code] .....
It doesn't compile because sport is private in the super class, but I thought FootballTeam1 would inherit it's own copy of sport because it is extending Team1.