I am trying to figure out how I can most easily make it easier to make new types of units in my game. I have buildings and ships, and would like to know how I could make it easy to add new units. I have been recently told about interfaces, and have worked with inheritance a little bit.
What I would like to able to do is have it so that all of the variables and methods common to all ships could be stored in a superclass or interface, and same with the buildings. I would also like to be able to assign behaviours to the buildings and ships, maybe as interfaces, which could contain all of the methods and variables required for the functions of that ship or building.
For example, creating a new type of building that can shoot, build ships, and can regenerate nearby ships. So it would possible inherit all of the variables and methods common to all buildings, such as health, image, x, y, getX(), getY() etc. But it would then also gain the variables and methods essential for its functionality, such as shootRange, shoot(), regenRate, etc.
I 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?
As what i understand, Generalization is also know as Inheritance and Aggregation. They may or may not co-exist in order for a class to work. The subclass and the super class
And on the other hand, multiple inheritance is not allowed in Java, how these two differs.
I understand that interface methods are abstract. I don't understand what the methods in the API do if the method bodies are empty. For example, say there are two interfaces, both with one method with no parameters. What would make these two interfaces different from each other. In the API, the AudioClip interface has the methods play(), stop(), and loop(). If abstract methods have no method bodies, and these methods take no parameters, what makes them different from each other.
why interfaces are needed in Java,Now you saw what a class must do to avail itself of the s... - justpaste.it (if I paste the quote here, I get the "Page not found" error after posting -.^)
the first fragment reads that the compiler must be sure that a method exits at a compile time, whereas the second fragment denies it - if a[i] doesn't have the specified compareTo method, a JVM simply throws an exception.
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 ?
I'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)...
Interfaces are 100 % abstract classes.They cannot be instantiated.Their sole purpose is to be implemented.So why does the following code works just fine while it is attempting to instantiate an interface.
interface TestA { String toString(); } public class Test { public static void main(String[] args) { System.out.println(new TestA() { public String toString() { return "test"; }}); } }
This is the link [URL] and it says One significant difference between classes and interfaces is that classes can have fields whereas interfaces cannot.How can this be possible?
In Interview many times Interviewer ask a simple question "Hibernate core Interfaces ?".The five core interfaces exposed by Hibernate. But he not satisfy, Why?...
interface: methods - abstract, default, static ONLY(abstract methods have no body, while static and defaults do, right?) fields - public, static, final ONLY abstract class: a normal class, but has at least one abstract method methods - all i.e., static, non-static, abstract (can it have a default method?) fields - all i.e., public, protected, private / final, non-final / static, non-static
I have three classes of object, most of which must implement two out of three interfaces. The interfaces look like this:
public interface Source { public void startSending(); } public interface Sender { public void setReceiver();
[Code] .....
That works fine, but I am wondering if pairing the interfaces into subinterfaces is a defensible methodology. For example, all classes that act like Producer must implement both the Source and Sender interfaces. And all classes that act like Relayer must implement the Sender and BlackHole interfaces. I could define two subinterfaces like this:
public interface Factory extends Source, Sender { } public interface Modifier extends BlackHole, Sender { }
I could then define my classes like this:
public class Producer implements Factory { } public class Relayer implements Modifier { } public class Consumer implements BlackHole { }
Within the class definitions, it makes no difference, as I will have to implement the same methods either way. But it seems more self-documentary to create the subinterfaces from their parent interfaces and name them in ways that reflect what the classes that implement them must actually do.
I am reading about interface and i see that classes are allowed inside interfaces which are implicitly static. Here is sample of code i created and i am able to access the static method and fields as well. Here is the code snippet.
public class TestInnerClass { public static void main(String[] args){ Test.NestedClass.printMe(); } } interface Test{ static class NestedClass{ static int x = 100 ; public static void printMe(){ System.out.println(x); } } }
My question is what is the use of such static classes inside interface? If i don't have access to Foo, i can't ever invoke NestedClass. Whats the design usage?
import java.util.*; public class CommonElements { private int comparisons; // number of comparisons made private Comparable[] arrayToSearch; // current array being traversed private Comparable[] commonElements = new Comparable[10]; private int arrayPosition = 0; //keeps track of what index to add an element to common at
[Code] ...
I have trying to get this down to the bar minimum. I am trying to cast the desired object array to a array of comparable. This is all required by the assignment.
I am getting a runtime error that I can not perform the desired cast. What do I need to provide the compiler in order to allow for this casting. I can not change the signature of the method however nothing about the class has been specified do I need to implement comparable? Also I don not now what the client is passing so how would I write a generic compareTo method to compare object of unknown types.
I have i am trying to implement tooltip through javascript, like when we click on an image link tooltip should be displayed and it should have close button/ close image to close that tooltip.like the same way i will have multiple images on page, when ever i click on the images all tooltips should be displayed on the page when ever i want to close that then only it should close through close button on tooltip.can we do it through java script or will go for jquery.