Java: overriding methods: what it means, and rules about it

Tutoring Java programming, you might be asked to clarify about overriding. The tutor shares some thoughts.

In Java, when you override a method, you either

  1. define its performance (give it a “body”), or else
  2. redefine (change) its performance
  3. .

Case 1, above, refers to the situation of writing a subclass of an abstract class, or implementing an interface: in either case, a method with undefined functionality must (typically) be defined, aka overridden.

Case 2 often refers to the situation of writing a subclass that alters the performance of an inherited method. However, an instance of a class can have a method overridden as well.

Rules:

When overriding a method, its return type, name, and parameter list (including types) must be identical to its original ones [from the abstract class, interface, or base class].

@Override can be used at the beginning of the line where the override is declared. For example:

@Override public void method_x(String param0, int param1,….){

This may give the compiler extra opportunity to check the validity of the override, as well as improve the code’s human readability.

Source:

docs.oracle.com

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Java compile error: non-static method cannot be referenced from a static context – what causes it?

Tutoring Java programming, this error message might come up. The tutor talks about a couple of its causes.

The message

error: non-static method MethodX() cannot be referenced from a static context

has a couple of causes I know:

  1. The class that contains the static main method contains another method other_method that isn’t static. other_method can’t be called from the static one unless it, too, is declared static.
  2. An object has been created [Objectx Ox = new Objectx();], but then its method methodx() is called using the class name [Objectx.methodx();]
    as opposed to the instance name [Ox.methodx();]

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Java programming: constructors in subclasses

Tutoring Java programming, there are so many interesting facets to encounter. The tutor talks about a constructor of a subclass and the word super.

Let’s imagine you write a subclass. Furthermore, let’s imagine the parent class (the superclass) has a detailed constructor. Can you assume that the subclass will simply inherit the parent’s constructor and default to its use? Apparently not.

My situation: I wrote a subclass of an abstract class that, while abstract, has a well-defined constructor. In the subclass I just defined the abstract functions from the parent. The compiler protested, citing a mismatch between the parameters required for the constructor versus those given. I looked up the problem.

Apparently, to a subclass, the compiler provides a default constructor with no arguments.

To instill the parent’s constructor in the subclass, one can declare a constructor in the subclass with the same parameters as those of the superclass’ constructor, then call super(parameter list).

Let’s imagine a parent class PC like so:

class PC{

String name;
String color;

public PC(String name, String color){
this.name=name;
this.color=color;
}//end of constructor

}

Now, the subclass SC:

class SC extends PC{

public SC(String name, String color){
super(name,color);
}//end constructor

}

From my observation, the code in the blue rectangles above is what will make the subclass take on the parent’s constructor.

Source:

docs.oracle.com

journals.ecs.soton.ac.uk/java

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Java: the meaning of * used with import

The tutor offers an interpretation of what the asterisk (aka, wildcard) means in an import statement.

I’m not trained in Java, but do write experimental programs with it. Here are some ideas about the import statement, as I understand it:

Herein, it might be helpful to think of a package as a directory.

The Java statement

import pckg0.member0;

gives access to the definition of member0, as needed for the program. In this context, member0 is a single, specific class found in the package pckg0.

The statement

import pckg0.*;

gives access to all “single” classes contained in pckg0, but not to those of its subdirectories. If pckg0 contains the package pckg00, which contains the class mem01, then the statement

import pckg0.pckg00.mem01;

provides access to it.

The statement

import pckg0.pckg00.*;

provides access to all single classes in pckg00.

Therefore, the two import statements, one atop the other,

import pckg0.*;
import pckg0.pck00.*;

can sensibly occur together at the top of a Java program.

HTH:)

Source:

McGrath, Mike. Java in Easy Steps, 5th ed. Warwickshire: Easy Steps Limited, 2014.

docs.oracle.com

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Computer science: JavaScript: null value, undefined type and value

The tutor comments on JavaScript null and undefined.

Null, it seems, is often mentioned in computer science. Perhaps it’s used a little loosely: it might refer to the value read when there’s nothing there, or the value of a variable that doesn’t exist.

In JavaScript, in my experience:

Comp Sci: command prompt testing (Windows 7): how to kill a runaway program

The tutor brings up a couple of “emergency procedures” for stopping a program.

I don’t write many big programs; rather, I’ll often write a page or so of code (with lots of white space) to test a utility from a given language (lately it’s been Java). Such a program is usually run from the command line.

Some programs end up “hanging”; therefore, I like to know how to stop a program before I run it. If the program really gets out of hand (and assuming it’s running from the command line), there are two ways I know to stop it:

  1. The key combination Ctrl+C
  2. From the Task Manager

The Task Manager is easily started by right-clicking the taskbar, then clicking Start Task Manager. There are several tabs to choose from; the Applications one will show the Command Prompt. If the program is running from there, just click it, then End Task at the bottom.

The two methods work equally well on my system:)

Source:

techrepublic.com

docs.oracle.com

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Java: why null might be used as an input parameter (aka argument)

The tutor looks into passing null as an argument.

Sometimes, reading Java code, you encounter null being passed as an argument aka parameter:

method0(null);

Apparently, when a method expects an object parameter, but doesn’t (necessarily) use it, you can pass it null to satisfy its need for a parameter. A very simple example might be as follows:

method0(Object obj0){
System.out.println(“Hello! I’m here!”);
}

Above, the method method0 requires a parameter of type Object. Although method0 doesn’t use that parameter, the compiler will protest if method0 is called without it. Calling method0 with null as the parameter will apparently satisfy the compiler, which is why you might see

method0(null);

in Java code examples.

I’ll talk more about this in future posts.

Source:

coderanch.com

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.