How To Simplify Classes By Using Interfaces And / Or Inheritance

Apr 6, 2014

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.

How could this best be achieved?

View Replies


ADVERTISEMENT

How Multiple Inheritance Is Possible In Java With Interfaces

Apr 11, 2014

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

View Replies View Related

Implement Multiple Inheritance In Java Using Interfaces?

Jan 12, 2014

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 Related

Abstract Classes And Interfaces

Mar 29, 2014

I did research again....

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

View Replies View Related

Nesting Interfaces Inside Classes

Jun 20, 2014

When are we allowed to nest interfaces inside a class? Would this be possible? Why or Why not?

View Replies View Related

Classes Of Object - Implement Two Out Of Three Interfaces

May 8, 2014

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.

View Replies View Related

Why Static Classes Are Allowed In Interfaces

Oct 25, 2014

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?

View Replies View Related

Why Super Classes Always Declared As Interfaces And Not Abstract Class

Dec 1, 2014

While reading the design patter book, i got one doubt ,There is a List an interface having sub classes ArrayList, LinkedList etc.,

Q1) My question is Why they declared the List as interface rather than Abstract class?

Q2) i read some site -

List l = new ArrayList(); Why it is GOOD line?
ArrayList l = new ArrayList() ; Why it is BAD line?

Answer required with detailed information for Q1 and Q2.

View Replies View Related

How Does Inheritance Work With Abstract Classes

Nov 19, 2014

I 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.

View Replies View Related

Extending Classes In Inheritance Hierarchy

Jan 4, 2015

I have a working program, except that it does not calculate the credit hours and the financial aid. When I enter an input, it works, until it should show the student name, credit hours and financial aid. the error i get from the command is "Hours invalid for false student". Here is the program i think i might have the problem.

import java.text.DecimalFormat;
public abstract class Student
{
//initialise variables
String name;
int creditHrs;

[Code] .....

View Replies View Related

Java Does Not Support Multiple Inheritance Using Classes

Aug 16, 2014

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?

Theoretically,

MyTest extends Object &
MyTest extends Test.

View Replies View Related

Simplify If Condition

May 24, 2014

I m trying to simplify an if condition as much as possible and i m curious if it can be made more short then i managed to .The problem is simple , a integer number with 3 digits is generated and the user is asked to input another 3 digits integer number ... One of the conditions that need to be verified is if all the digits in each number match but not exactly ( aka 123 and 132 ) .Using only an if statement to verify this what is the most short condition that can be checked to identify when the digits in a 3 digits number match the digits of another 3 digits integer number (not interested if they match exactly) .

The simplest condition i managed to find is :

else if (guessDigit1 + guessDigit2 + guessDigit3
== lotteryDigit1 + lotteryDigit2 + lotteryDigit3
&& (guessDigit1 == lotteryDigit1 || guessDigit1
== lotteryDigit2 || guessDigit1 == lotteryDigit3)
&& (guessDigit2 == lotteryDigit1 || guessDigit2
== lotteryDigit2 || guessDigit2 == lotteryDigit3)) {
System.out.println("Match all digits: you win $3,000");}

Is there a better(shorter ) condition that can be used ?

View Replies View Related

Simplify Two Large Methods Into One Similar Method?

Jan 4, 2015

Right now I have 12 methods, each roughly 40 lines long, and I am contemplating whether I should simplify some of the pairs (12 methods, but every other method is very similar to each other, so 6 pairs) into one method.

The main pro of simplifying them is that my class will be much smaller, right now its about 555 lines of code. However a con is that it might not be as straight forward, at first glance at least.

Here is what I am talking about. Two original methods that are very similar:

void rotateRightClockwise(int rotations){//rotates FRONTS's RIGHT! CONFIRMED
String a, b, c;//temp values
for(int i = 1; i <= rotations; i++){//confirmed
a = cube.cube[1][2].square[2];//3
b = cube.cube[1][2].square[5];//6
c = cube.cube[1][2].square[8];//9

[Code] ....

The only difference between these two methods, other than the last chunk for each method, is the I have to change the 2nd square bracket, [], values from lines 9-23 for cube[][]. That's it. So I am thinking of combining them into something like this...

private void rotateRight(int rotations, int x, int y){
String a, b, c;//temp values
for(int i = 1; i <= rotations; i++){//confirmed
a = cube.cube[1][2].square[2];//3
b = cube.cube[1][2].square[5];//6
c = cube.cube[1][2].square[8];//9

[Code] .....

And then after calling this similar method, I would finish off the last chunk from each methods, since they are too different (ie lines 26-38).

The reason why I ask this is because I am trying to build up my portfolio and I want to know if the employer would look at this and say "that was a good move by compacting your code" or "this is not that straight forward".

View Replies View Related

Can Interfaces Be Instantiated

Feb 14, 2014

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"; }});
}
}

View Replies View Related

Using Constructors In Interfaces?

Sep 1, 2014

Can i use constructors in an interface?

interface AI {
public abstract AI();
public abstract void hello();
}

Output:

I got the error as the method AI() should have return type.

View Replies View Related

Interfaces In Java API

May 14, 2014

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.

View Replies View Related

JavaDocs Says Interfaces Cannot Have Fields?

May 20, 2014

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?

View Replies View Related

Why Interfaces Are Needed In Java

Dec 5, 2014

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.

View Replies View Related

Hibernate Core Interfaces

Jan 8, 2015

In Interview many times Interviewer ask a simple question "Hibernate core Interfaces ?".The five core interfaces exposed by Hibernate. But he not satisfy, Why?...

View Replies View Related

Static Methods In Java Interfaces?

Jun 16, 2010

Why can't we have static methods in an interface?

View Replies View Related

Interfaces Using Generic Type Constructions

Jan 16, 2014

I want to make some library interfaces for a graph.Using these interfaces:

Graph<V,E>
Edge<E>
Vertex<V>

how can i constraint users of this library to use the same type <E> in the graph and edge interface and type <V> in the graph and vertex interface??

View Replies View Related

Casting And Interfaces - Comparing Object Of Unknown Types

Oct 15, 2014

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.

View Replies View Related

Ambiguity When Implementing Multiple Interfaces With Same Method / Variable Names?

Oct 14, 2014

imagine you are implementing 2 interfaces having identical method signatures:

interface A {
void doStuff();
}
interface B {
void doStuff();

[Code] ....

How can I implement both methods?

Or another example with member variables:

interface A {
public static final int i = 3;
}
interface B {
public static final int i = 33;

[Code] ....

How can I go about making clear which 'i' is meant?

View Replies View Related

Displaying Override In Inheritance

Jul 11, 2014

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]....

View Replies View Related

Difference Between Abstract And Inheritance

Dec 16, 2014

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 Related

How Inheritance And Exception Work Together

Jun 10, 2014

how inheritence and exception work together ?? what are the rules ??

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved