Java is class abstract? - java

I'm trying to get object from another class from another package
package processManager;
public class PCB {
public int vruntime;
public int nice_value=0;
}
in the same package
package processManager;
public class Process {
public Process(PCB pcb) {
this.pcb = pcb;
}
public Process() {
}
public PCB pcb;
int a;
}
usage of object
package processManager.newpackage;
import processManager.Process.*;
public class NewClass {
public static void main(String[] args) {
Process proc=new Process();
}
}
and I don't know why but then I've got "Process is abstract; cannot be instantiated"

Please look closer at your code:
A) You have defined a class processManager.Process which is not abstract.
B) Next in the next file you are importing
import processManager.Process.*;
Which actually defines an import of all sub-classes of processManager.Process class (you have none) but the class itself is not considered an import.
C) This means that in the next piece of code
Process proc = new Process();
You are trying to create an instance of java.lang.Process class which is abstract.
This is a source of your error.

Related

using a static method in a different class

Let us suppose we have a class:
package package1;
public class Car {
public static int brake()
{
int x;
//some functionalities
}
//other functionalities
}
I want to ask for using this method brake in classes of different package, do we need to include package name also? -- int xyz=package1.Car.brake(); or simply Car.brake(); will work
You can import package or use full path of method:
First solution:
public class App {
public static void main(String[] args) {
package1.Car.brake();
}
}
Second solution:
import package1.Car;
public class App {
public static void main(String[] args) {
Car.brake();
}
}
Add import statement like import package1.Car; and then you can use Car.brake(); to call the function. Read more about imports here

runtime instantiated class cannot access the interface it implements

I have two classes interfaceTestA and interfaceTestB. They have
an arraylist called 'myList' and a method, 'makeString()' in commom.
MyList is defined in an interface named hasList and makeString is
implemented in an abstract class getTheData. So both of them extend
getTheData and implement hasList. Compiling and running, for example,
interfaceTestA works just fine. In particular it has access to myList.
But now, in a class called ifTest, I want to instantiate one of the
two known only at run time. My problem is that when instantiated
this way, it does not know about myList. See the listings.
import java.util.*;
interface hasList { ArrayList myList = new ArrayList(); }
//===
abstract class getTheData {
public String makeString() { return "Hi Mom"; }
}
//===
public class
interfaceTestA extends getTheData implements hasList
{
public interfaceTest() {
setString();
System.out.println("size= "+myList.size());
}
void setString() { myList.add(makeString()); }
public static void main(String[] args) {
interfaceTestA it = new interfaceTestA();
}
}
import java.util.*;
import java.lang.reflect.*;
public class
ifTest
{
public static void
main(String[] args)
{
Class prog = Class.forName("interfaceTestA");
getTheData gtd = (interfaceTestA) prog.newInstance();
System.out.println("myList size= "+gtd.myList.size());
}
}
interfaceTestA is child of hasList but getTheData is not. In your code, type of gtd is getTheData so that it can not see the myList.
I found a solution. Create a new abstract class that combines both, e.g. abstract class combined extends getTheData implements hasList. Now
declare gtd to be of type combined, e.g. combined gtd = (interfaceTestA)prog.newInstance().
And it now works. Thanks for the help.

Static Block not running, no final variables

I have this code here using my API:
package org.midnightas.os.game.dots;
import java.awt.Graphics2D;
import org.midnightas.os2.Key;
import org.midnightas.os2.MidnightasOS;
import org.midnightas.os2.gameapi.Game;
public class Dots extends Game {
public Dots(MidnightasOS midnightasos) {
super(midnightasos);
}
#Override
public void init() {
}
#Override
public void keyPressed(Key arg0) {
}
#Override
public void render(Graphics2D arg0) {
}
#Override
public void tick() {
}
static {
System.out.println("MOS Dots crashed.");
MidnightasOS.setGame(Dots.class);
}
}
The static block is supposed to be ran calling MidnightasOS.setGame(Class);
However that is not happening.
I have also debugged using System.out to no avail.
Is the problem within MidnightasOS? I will post it's code if necessary.
I'm doing this because I'm trying to create an artificial operating system with Linux and the Raspberry PI.
This shall be a game console like the Game Boy.
I'm trying to load all Game classes so at least one of them would use MidnightasOS.setGame(Class);
Thanks for reading.
When is Dots class loaded by classloader. It will be loaded on the first reference of this class. See if you ever refer to this class
You can even dynamically load the class
And to find all the subtypes of a class and load them all you can use this library
public class MainClass {
public static void main(String[] args){
ClassLoader classLoader = MainClass.class.getClassLoader();
Reflections reflections = new Reflections("org.midnightas");
Set<Class<? extends Game>> subTypes = reflections.getSubTypesOf(Game.class);
for(Class<? extends Game> subType : subTypes){
try {
Class aClass = classLoader.loadClass(subType);
System.out.println("subType.getName() = " + subType.getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
The static blocks in a class are executed as soon as the classloader loads the class for the first time. There are several possibilities to achieve this. Consider the following class:
public class SomeClass {
static {
System.out.println("static block in SomeClass");
}
static void someMethod() {
System.out.println("some static method");
}
}
Loading it by creating an object:
SomeClass foo = new SomeClass();
Loading it by calling a static method:
SomeClass.someMethod();
Loading it directly:
Class.forName("SomeClass");
These are only some of the possibilities you have! Please remark that you'll have to include the package structure into the third approach (if the class is in the package some.package it'll be: Class.forName("some.package.SomeClass");

Java - inheritance, class with methods

I have my subclass:
public class Actions extends Main{
public void getFireTarget() {
GameObject target = getGameObjects().closest("Target");
do{
log("Shooting at the target");
getMouse().click(target);
} while(target != null && !getDialogues().inDialogue() && getLocalPlayer().getTile().equals(rangeTile));
}
}
I want to write similar methods, so I can call them in my Main class, so I don't have to write over and over.
My main class looks like this (won't fully paste it as it's long):
public class Main extends AbstractScript{
...code here
Actions actions = new Actions();
}
So I am trying to implement the methods in Actions by doing actions.getFireTarget(), which seems to work. But when I compile, I am getting two compile errors:
1) In the Main class, in the line: Actions actions = new Actions();
2) In the Actions class, in the line where I am extending the superclass.
Am I missing something in the sub class in order to store methods and then call them in the main method? Please advise! Thanks
The syntax is wrong. () are not allowed here: public class SomeName(). Remove the brackets.
I am having trouble understanding your details.
Here is a short info about inheritance:
public class A{
protected int t;
public void methodA(){}
}
public class B extends A{
#Override
public void methodA(){}
public void methodB(){}
}
If you override the methodA in class B, any call from a instance of class B to the method will use the method defined in class B. (If you dont write the method in class B, it will use the method from class A)
Objects of class A cannot use methodB() defined in class B.
Also you can access the field t in class B, because of the protected modified.
I think its better for you your problem to instantiate other class to your main class if you have same function name or same variable name. try to compile and run this code
public class First{
Second sec = new Second();
String s = "This is first";
public First(){
System.out.println(this.s);
System.out.println(getSecondString());
System.out.println(sec.getSecondString());
}
public String getSecondString(){
return "This is first";
}
public static void main(String args[]){
new First();
}
}
public class Second {
String s = "This is second";
public String getSecondString(){
return s;
}
}

Access public static class' state from a separate class file

I have a public static class within another public class as follows:
public class Foo<A> {
public static class Bar<A>{
A firstBar;
Bar(A setBar){
this.firstBar=setBar;
}
}
public final Bar<A> instanceBar;
public Foo(A actualValue) {
instanceBar = new Bar<A>(actualValue);
}
public Bar<A> getBar() {
return instanceBar;
}
My objective is to access instanceBar's state from a separate class file without a get method and without changing the visibility of firstBar. How do I accomplish this?
For example, the following says not visible.
public class RetrieveFirstBar {
public static void main(String[] args) {
Foo z = new Foo(5l);
Foo.Bar<Long> z2 = z.getBar();
long k = z2.firstBar; //not visible!
}
}
I guess you mean
class Foo<A>
Since you write "A firstBar;" you give package access to the variable:
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
If you have the RetrieveFirstBar in the same package you will not have visibility problems. But, if you want to access it from everywhere you should write
public A firstBar;

Categories