Set methods in a subclass - java

I need to set values from the subclass by invoking the set method from the super class. I need to set monster name and health from the subclass
Alien class
public class Alien {
//Instance variables
public String monsterName;
public int HP;
//A method that sets monster properties
public void setValues(String monsterN, int health) {
monsterName = monsterN;
HP = health;
}
//A method that returns the monster name
public String getName() {
return monsterName;
}
//A method that returns monster's health
public int getHP() {
return HP;
}
//ToString method that prints out the info
public String toString() {
return("Monster name: " + getName() + "Monster's current health point: " + getHP());
}
}
Snake class
public class SnakeAlien extends Alien {
//Instance variable
//Set the snake values
Alien aObject = new Alien();
aObject.setvalues("Snake\n" 55));
}
The main class
public class Main {
public static void main(String[] args) {
Alien object = new Alien();
object.toString();
System.out.println(object);
}
}
I know how would I set these values through the main class, but I need to do it from the sub class SnakeAlien which inherets from the Alien class.

Because everything is public (which is terrible BTW) you can simply call the superclass' methods frimbthe subclasses. Not even super is needed.
Though, you should change members you don't want to reach from the outside to at least protected, and if you never want to make a pure Alien object, you should make that an avstract class.

Related

Effecting the instance fields of one class with the instance fields of another

I want to clarify beforehand that I am only interested in programming from a hobbyist perspective and have no education regarding programming nor do I have any professional experience.
I am trying to figure out how to affect the instance fields of one class with the instance fields of another. Here is an example of what I'm trying to figure out:
//Main Class
class Main
{
public static void main(String[] args)
{
Class1 instance1 = new Class1(1);
Class2 instance2 = new Class2(1);
}
public void method1()
{
// code to add the class1 instance field value class1Num1 to Class2 instance field value class2Num1
// at least cobine the two values in one variable that is usable in Main
}
}
//Class1
class Class1
{
int class1Num1;
public Class1(int num1)
{
class1Num1 = num1;
}
}
//Class2
class Class2
{
int class2Num1;
public Class2(int num1)
{
class2Num1 = num1;
}
}
Ultimately I want to know how I could get class2Num1 and add it to class1Num1 in the Main class. I mean want to change the value of class1Num1 via a method so that later in my program when class1Num1 is referenced, the value returned is 2 (class1Num1 +class2Num1). How would I write a method to do that and what class or classes would I have to write this in? This may be an incredibly simple task I realize but I can't seem to figure this out.
Based on responses it look as if getter and setter methods are the way to go however, based on the examples I have seen so far it looks as though instance1 and instance2 are having to be passed as arguments in the Main class method that combines the two numbers. Is there a way that I could write a method so that it targets the instance that calls the method? Here is an example:
//this method is being called in Main
instance1.method(int class2){
int a = getClass1Num1();
// ^ how could I target the instance calling this method here?
//
int b = class2.getClass2num1();
int c = a +b;
}
It's better to use private fields and getter and setter methods
Class1 is as follows
public class Class1 {
//fields
private int class1Num1;
//getter
public int getClass1Num1() {
return class1Num1;
}
//setter
public void setClass1Num1(int class1Num1) {
this.class1Num1 = class1Num1;
}
}
Class2 is as follows
public class Class2 {
//fields
private int class2Num1;
//getter
public int getClass2Num1() {
return class2Num1;
}
//setter
public void setClass2Num1(int class2Num1) {
this.class2Num1 = class2Num1;
}
}
Main Class is as follows
public class Main {
public static void main(String[] args) {
//Set class1Num1
Class1 instance1 = new Class1();
instance1.setClass1Num1(1);
//Set class2Num1
Class2 instance2 = new Class2();
instance2.setClass2Num1(1);
//add class1Num1 + class2Num1
int sum = instance1.getClass1Num1() + instance2.getClass2Num1();
//Set sum into class1Num1 of instance1
instance1.setClass1Num1(sum);
//Printing class1Num1 (2)
System.out.println(instance1.getClass1Num1());
}
}
a good object oriented way to do that would be to include a getter method to return the instance variable of your classes. Eg you should add a method like this to your Class1 and Class2 :
public int getInstanceVar() {
return class1Num1; // Or class2Num1 in Class2
}
Then in your main class you can call this method from your newly created Class1 // Class2 objects, eg :
int result = instance1.getInstanceVar() + instance2.getInstanceVar();

Java Inheritance: Calling a subclass method in a superclass

I'm very new to java and would like to know whether calling a subclass method in a superclass is possible. And if doing inheritance, where is the proper place to set public static void main.
Superclass
public class User {
private String name;
private int age;
public User() {
//Constructor
}
//Overloaded constructor
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public static void main(String []args) {
User user1 = new Admin("Bill", 18, 2);
System.out.println("Hello "+user1.getName());
user1.getLevel();
}
}
Subclass
public class Admin extends User {
private int permissionLevel;
public Admin() {
//Constructor
}
//Overloading constructor
public Admin(String name, int age, int permissionLevel) {
super(name, age);
this.permissionLevel = permissionLevel;
}
public void getLevel() {
System.out.println("Hello "+permissionLevel);
}
}
Short answer: No.
Medium answer: Yes, but you have to declare the method in the superclass. Then override it in the subclass. The method body from the subclass will be in invoked when the superclass calls it. In your example, you could just put an empty getLevel method on User.
You could also consider declaring User as an abstract class and declaring the getLevel method as abstract on the User class. That means you don't put any method body in getLevel of the User class but every subclass would have to include one. Meanwhile, User can reference getLevel and use the implementation of its subclass. I think that's the behavior you're going for here.
I'm very new to java and would like to know whether calling a subclass
method in a superclass is possible.
A superclass doesn't know anything about their subclasses, therefore, you cannot call a subclass instance method in a super class.
where is the proper place to set public static void main.
I wouldn't recommend putting the main method in the Admin class nor the User class for many factors. Rather create a separate class to encapsulate the main method.
Example:
public class Main{
public static void main(String []args) {
User user1 = new Admin("Bill", 18, 2);
System.out.println("Hello "+user1.getName());
user1.getLevel();
}
}
No, it is not possible to call sub class method inside super class.
Though it is possible to call different implementations of the same method in a client code while you have a variable with a super class type and instantiate it with either super class or sub class objects. It is called polymorphism.
Please, consider the following example:
public class User {
private String name;
private int age;
protected int permissionLevel;
public User() {
//Constructor
}
//Overloaded constructor
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return this.name;
}
public void getLevel() {
System.out.println("Hello "+ permissionLevel);
}
}
public class Admin extends User {
public Admin() {
//Constructor
}
//Overloading constructor
public Admin(String name, int age, int permissionLevel) {
super(name, age);
this.permissionLevel = permissionLevel;
}
#Override
public void getLevel() {
System.out.println("Hello "+permissionLevel);
}
public static void main(String []args) {
User user1 = new Admin("Bill", 18, 2);
System.out.println("Hello "+user1.getName());
user1.getLevel(); //call to subclass method
user1 = new User("John", 22); //the variable is the same but we assign different object to it
user1.getLevel(); //call to superclass method
}
}
Answering your second question, no, it does not matter where you place your main method as long as it is of right method signature. As you can see in my example I moved the method to Admin.java - it is still acceptable.
Calling subclass method in a superclass is possible but calling a subclass method on a superclass variable/instance is not possible.
In java all static variable and methods are considered to be outside the class i.e they do have access to any instance variable or methods. In your example above it will be wise to create a new class called Main and put public static void main in there but this is just a hygiene issue and what you have above will work except for the line.
user1.getLevel()
Use case: If employee eats, then automatically should sleep:-)
Declare two methods eat and sleep from class person.
Invoke the sleep method from eat.
Extend person in the employee class and override only the sleep method:
Person emp=new Employee();
emp.eat();
Explanation: As eat method is not in subclass, it will invoke the super class eat. From there, sub class's sleep will be invoked.

Confusing java code - related to static binding

something about this code confuses me. The first print line is 1600. I understand it has something to do with the static class being Car and not Sportscar. But we created the object as a Sportscar, so why isn't the volume field 3500? Thanks.
public class Car {
public int volume;
public Car() { this(1600); }
public Car(int volume) { this.volume = volume; }
public String toString() { return "Car:" + volume; }
}
public class SportsCar extends Car {
public int volume;
SportsCar() { this(3000); }
SportsCar(int volume) { this.volume = volume; }
public String toString() {return "SportsCar:"+volume;}
}
public class CarApplication {
public static void main(String [] args) {
Car car = new SportsCar(3500);
System.out.println(car.volume);
System.out.println(car.toString());
System.out.println(car);
}
}
First off, note that when you call the constructor for SportsCar Java will automatically call the default constructor for its parent class, Car. This will set the volume field of the object's parent Car class to 1600.
In Java, there is no polymorphism for fields. So whereas the toString() method inside your SportsCar class always overrides that of its parent (Car) class, the rules for how identically named instance variables are accessed is a little different.
If you are accessing volume from within the SportsCar class then the volume of SportsCar will be used. From outside the class itself (so when you're calling from CarApplication) which instance variable is accessed depends upon the compile-time type of the object in question. Because you declare the type of car to be a Car, the volume value of the parent Car class is used--hence 1600 is printed. If you had instead declared car to be a SportsCar, 3500 would be printed.
Another option would be this:
System.out.println(((SportsCar)car).toString());
This will output 3500, since the type of car has been cast to SportsCar.
So the point here is that when you are using car.volume, car variable is of type Car and object reference is of type SportsCar.
As both the classes have the same element named volume and you are trying to refer it through parent object variable, it is returning 1600.
If you will typecast and then check the volume, it will return 3500 as per below code:
System.out.println(((SportsCar)car).volume);
When you declare something in this way:
ParentClass variable = new ChildClass(Args);
The methods and fields in ParentClass are the only ones available to you. It is restricted because you declared the type as a ParentClass. Because Car's volume is 1600 and the object is restricted to the ParentClass's methods and fields, it prints 1600.
An example would be as follows:
Consider I had an Apple Class and a Fruit class:
public class Fruit {
private String type;
public Fruit(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
And the Apple Class:
public class Apple extends Fruit {
private String variant;
public Apple(String variant) {
System.out.println("I like " + variant + apples too!");
super("Apple");
}
public String getVariant() {
return variant;
}
}
And now I instantiate like this:
Fruit ap = new Apple("Fiji");
I don't have access to getVariant() or any methods in the Apple class because the type is of the parent class, not of Apple. I would be able to do getType() but that's it.
In your case:
public class SportsCar extends Car {
public int volume;
SportsCar() {
this(3000);
}
SportsCar(int volume) {
this.volume = volume;
}
public String toString() {
return "SportsCar:"+volume;
}
}
Although SportsCar has it's own constructor and takes it's own volume, the parent class Car is the actual type, causing the Car constructor to be called, hence setting volume to 1600. To specify a SportsCar object, do this:
SportsCar sc = new SportsCar(3500);
System.out.print(sc.toString());
This will print out:
SportsCar:3500
You can also typecast it like so:
Car c = new SportsCar(1600);
System.out.print(((SportsCar) c).toString());
Aman Chhabra answer is right. You created a SportCar object which is from the Car "family". And what you are printing is the volume of Car, not the volume of SportCar.
One other way is to create a SportsCar object instead of Car.
P.s: you should always set your class attributes as private :)

Abstract Class and toString() method

I am a little confused on how to set up the TestHomework method so that it prints properly when using the toString() method. Right now when I run the main method it prints "null - 0" but what I would like it to say is "Math - 6". This program is supposed to extend an abstract class. It is supposed to say how many pages there are for homework and for what subject.
public abstract class Homework {
private int pagesToRead;
private String typeHomework;
{
// initialise instance variables
pagesToRead = 0;
typeHomework = "none";
}
public Homework(int pages, String hw) {
this.pagesToRead = pages;
this.typeHomework = hw;
}
public abstract void createAssignment(int p);
public int getPages() {
return pagesToRead;
}
public void setPagesToRead(int p) {
pagesToRead = p;
}
public String getTypeHomework() {
return typeHomework;
}
public void setTypeHomework(String hw) {
typeHomework = hw;
}
}
public class MyMath extends Homework {
private int pagesRead;
private String typeHomework;
public MyMath(int pages, String hw) {
super(pages,hw);
}
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
public String toString() {
return typeHomework + " - " + pagesRead;
}
}
public class TestHomework {
public static void main(String[] args) {
MyMath one = new MyMath(6, "Math");
one.createAssignment(6);
System.out.println(one);
}
}
That's because you are defining the 2 properties (that one of them happen to have the same name as one of the abstract class's) but you are not initializing them, you are initializing those of the abstract class. (So their values is always set to their type's default)
You need to drop those from the MyMath class, & define the toString method in your abstract class: it's the one to be used by default by its inheriting classes.
public abstract class Homework {
private int pagesToRead;
private String typeHomework;
// Same code
// Define the toString here
#Override
public String toString() {
return typeHomework + " - " + pagesToRead;
}
}
public class MyMath extends Homework {
// You don't need to define any extra attributes
public MyMath(int pages, String hw) {
super(pages,hw);
}
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
}
public static void main(String[] args) {
// Calls the constructor of the MyMath class, which in turn
// invokes the constructor of its superclass, the 'Homework' class
MyMath one = new MyMath(6, "Math");
one.createAssignment(6);
// Invokes the toString of the MyMath class. Since it does not have one,
// The toString method of its superclass (Homework) is called.
System.out.println(one);
}
Your derived class has its own typeHomework and pagesRead fields, which are never set (even though the base class happens to have fields with the same names). Therefore, they stay null and 0.
You should delete those fields and use the data from the base class, via the public getter methods.
Why it doesn't work:
Be careful you redeclared the attribute typeHomework of you parent class. Attributes are automatically added to your extending class so you don't have to write them again.
By redeclaring it you confused the compiler, viewing your code in debug shows, that your one object contains your typeHomework twice:
typeHomework = null // The one from the super class
typeHomework = "Math" // The one from your child class
Your method now uses the typeHomework from your super-class therefor the output is null!
pagesRead is 0 because you are setting the pagesToRead of your super-class to 6(not pagesRead!) when calling setPagesToRead(p);.
Some style tips
Use the #Override annotation when overriding methods like this:
#Override
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
It's not really needed but it's good practice (readers of your code know that it overrides something).
When referring to attributes of your class it's also good practice to use the this statement so it's clear, that you're referring to an attribute and not a local variable:
#Override
public String toString() {
return this.typeHomework + " - " + this.pagesRead;
}

Is there a way to override class variables in Java?

class Dad
{
protected static String me = "dad";
public void printMe()
{
System.out.println(me);
}
}
class Son extends Dad
{
protected static String me = "son";
}
public void doIt()
{
new Son().printMe();
}
The function doIt will print "dad". Is there a way to make it print "son"?
In short, no, there is no way to override a class variable.
You do not override class variables in Java you hide them. Overriding is for instance methods. Hiding is different from overriding.
In the example you've given, by declaring the class variable with the name 'me' in class Son you hide the class variable it would have inherited from its superclass Dad with the same name 'me'. Hiding a variable in this way does not affect the value of the class variable 'me' in the superclass Dad.
For the second part of your question, of how to make it print "son", I'd set the value via the constructor. Although the code below departs from your original question quite a lot, I would write it something like this;
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void printName() {
System.out.println(name);
}
}
The JLS gives a lot more detail on hiding in section 8.3 - Field Declarations
Yes. But as the variable is concerned it is overwrite (Giving new value to variable. Giving new definition to the function is Override). Just don't declare the variable but initialize (change) in the constructor or static block.
The value will get reflected when using in the blocks of parent class
if the variable is static then change the value during initialization itself with static block,
class Son extends Dad {
static {
me = "son";
}
}
or else change in constructor.
You can also change the value later in any blocks. It will get reflected in super class
Yes, just override the printMe() method:
class Son extends Dad {
public static final String me = "son";
#Override
public void printMe() {
System.out.println(me);
}
}
You can create a getter and then override that getter. It's particularly useful if the variable you are overriding is a sub-class of itself. Imagine your super class has an Object member but in your sub-class this is now more defined to be an Integer.
class Dad
{
private static final String me = "dad";
protected String getMe() {
return me;
}
public void printMe()
{
System.out.println(getMe());
}
}
class Son extends Dad
{
private static final String me = "son";
#Override
protected String getMe() {
return me;
}
}
public void doIt()
{
new Son().printMe(); //Prints "son"
}
If you are going to override it I don't see a valid reason to keep this static. I would suggest the use of abstraction (see example code). :
public interface Person {
public abstract String getName();
//this will be different for each person, so no need to make it concrete
public abstract void setName(String name);
}
Now we can add the Dad:
public class Dad implements Person {
private String name;
public Dad(String name) {
setName(name);
}
#Override
public final String getName() {
return name;
}
#Override
public final void setName(String name) {
this.name = name;
}
}
the son:
public class Son implements Person {
private String name;
public Son(String name) {
setName(name);
}
#Override
public final String getName() {
return name;
}
#Override
public final void setName(String name) {
this.name = name;
}
}
and Dad met a nice lady:
public class StepMom implements Person {
private String name;
public StepMom(String name) {
setName(name);
}
#Override
public final String getName() {
return name;
}
#Override
public final void setName(String name) {
this.name = name;
}
}
Looks like we have a family, lets tell the world their names:
public class ConsoleGUI {
public static void main(String[] args) {
List<Person> family = new ArrayList<Person>();
family.add(new Son("Tommy"));
family.add(new StepMom("Nancy"));
family.add(new Dad("Dad"));
for (Person person : family) {
//using the getName vs printName lets the caller, in this case the
//ConsoleGUI determine versus being forced to output through the console.
System.out.print(person.getName() + " ");
System.err.print(person.getName() + " ");
JOptionPane.showMessageDialog(null, person.getName());
}
}
}
System.out Output : Tommy Nancy Dad
System.err is the same as above(just has red font)
JOption Output: Tommy then Nancy then Dad
This looks like a design flaw.
Remove the static keyword and set the variable for example in the constructor. This way Son just sets the variable to a different value in his constructor.
Though it is true that class variables may only be hidden in subclasses, and not overridden, it is still possible to do what you want without overriding printMe () in subclasses, and reflection is your friend. In the code below I omit exception handling for clarity. Please note that declaring me as protected does not seem to have much sense in this context, as it is going to be hidden in subclasses...
class Dad
{
static String me = "dad";
public void printMe ()
{
java.lang.reflect.Field field = this.getClass ().getDeclaredField ("me");
System.out.println (field.get (null));
}
}
https://docs.oracle.com/javase/tutorial/java/IandI/hidevariables.html
It's called Hiding Fields
From the link above
Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different. Within the subclass, the field in the superclass cannot be referenced by its simple name. Instead, the field must be accessed through super, which is covered in the next section. Generally speaking, we don't recommend hiding fields as it makes code difficult to read.
class Dad
{
protected static String me = "dad";
public void printMe()
{
System.out.println(me);
}
}
class Son extends Dad
{
protected static String _me = me = "son";
}
public void doIt()
{
new Son().printMe();
}
... will print "son".
It indeed prints 'dad', since the field is not overridden but hidden. There are three approaches to make it print 'son':
Approach 1: override printMe
class Dad
{
protected static String me = "dad";
public void printMe()
{
System.out.println(me);
}
}
class Son extends Dad
{
protected static String me = "son";
#override
public void printMe()
{
System.out.println(me);
}
}
public void doIt()
{
new Son().printMe();
}
Approach 2: don't hide the field and initialize it in the constructor
class Dad
{
protected static String me = "dad";
public void printMe()
{
System.out.println(me);
}
}
class Son extends Dad
{
public Son()
{
me = "son";
}
}
public void doIt()
{
new Son().printMe();
}
Approach 3: use the static value to initialize a field in the constructor
class Dad
{
private static String meInit = "Dad";
protected String me;
public Dad()
{
me = meInit;
}
public void printMe()
{
System.out.println(me);
}
}
class Son extends Dad
{
private static String meInit = "son";
public Son()
{
me = meInit;
}
}
public void doIt()
{
new Son().printMe();
}
Variables don't take part in overrinding. Only methods do. A method call is resolved at runtime, that is, the decision to call a method is taken at runtime, but the variables are decided at compile time only. Hence that variable is called whose reference is used for calling and not of the runtime object.
Take a look at following snippet:
package com.demo;
class Bike {
int max_speed = 90;
public void disp_speed() {
System.out.println("Inside bike");
}
}
public class Honda_bikes extends Bike {
int max_speed = 150;
public void disp_speed() {
System.out.println("Inside Honda");
}
public static void main(String[] args) {
Honda_bikes obj1 = new Honda_bikes();
Bike obj2 = new Honda_bikes();
Bike obj3 = new Bike();
obj1.disp_speed();
obj2.disp_speed();
obj3.disp_speed();
System.out.println("Max_Speed = " + obj1.max_speed);
System.out.println("Max_Speed = " + obj2.max_speed);
System.out.println("Max_Speed = " + obj3.max_speed);
}
}
When you run the code, console will show:
Inside Honda
Inside Honda
Inside bike
Max_Speed = 150
Max_Speed = 90
Max_Speed = 90
only by overriding printMe():
class Son extends Dad
{
public void printMe()
{
System.out.println("son");
}
}
the reference to me in the Dad.printMe method implicitly points to the static field Dad.me, so one way or another you're changing what printMe does in Son...
You cannot override variables in a class. You can override only methods. You should keep the variables private otherwise you can get a lot of problems.
No. Class variables(Also applicable to instance variables) don't exhibit overriding feature in Java as class variables are invoked on the basis of the type of calling object. Added one more class(Human) in the hierarchy to make it more clear. So now we have
Son extends Dad extends Human
In the below code, we try to iterate over an array of Human, Dad and Son objects, but it prints Human Class’s values in all cases as the type of calling object was Human.
class Human
{
static String me = "human";
public void printMe()
{
System.out.println(me);
}
}
class Dad extends Human
{
static String me = "dad";
}
class Son extends Dad
{
static String me = "son";
}
public class ClassVariables {
public static void main(String[] abc) {
Human[] humans = new Human[3];
humans[0] = new Human();
humans[1] = new Dad();
humans[2] = new Son();
for(Human human: humans) {
System.out.println(human.me); // prints human for all objects
}
}
}
Will print
human
human
human
So no overriding of Class variables.
If we want to access the class variable of actual object from a reference variable of its parent class, we need to explicitly tell this to compiler by casting parent reference (Human object) to its type.
System.out.println(((Dad)humans[1]).me); // prints dad
System.out.println(((Son)humans[2]).me); // prints son
Will print
dad
son
On how part of this question:- As already suggested override the printMe() method in Son class, then on calling
Son().printMe();
Dad's Class variable "me" will be hidden because the nearest declaration(from Son class printme() method) of the "me"(in Son class) will get the precedence.
Just Call super.variable in sub class constructor
public abstract class Beverage {
int cost;
int getCost() {
return cost;
}
}`
public class Coffee extends Beverage {
int cost = 10;
Coffee(){
super.cost = cost;
}
}`
public class Driver {
public static void main(String[] args) {
Beverage coffee = new Coffee();
System.out.println(coffee.getCost());
}
}
Output is 10.
Of course using private attributes, and getters and setters would be the recommended thing to do, but I tested the following, and it works... See the comment in the code
class Dad
{
protected static String me = "dad";
public void printMe()
{
System.out.println(me);
}
}
class Son extends Dad
{
protected static String me = "son";
/*
Adding Method printMe() to this class, outputs son
even though Attribute me from class Dad can apparently not be overridden
*/
public void printMe()
{
System.out.println(me);
}
}
class Tester
{
public static void main(String[] arg)
{
new Son().printMe();
}
}
Sooo ... did I just redefine the rules of inheritance or did I put Oracle into a tricky situation ?
To me, protected static String me is clearly overridden, as you can see when you execute this program. Also, it does not make any sense to me why attributes should not be overridable.
Why would you want to override variables when you could easily reassign them in the subClasses.
I follow this pattern to work around the language design. Assume a case where you have a weighty service class in your framework which needs be used in different flavours in multiple derived applications.In that case , the best way to configure the super class logic is by reassigning its 'defining' variables.
public interface ExtensibleService{
void init();
}
public class WeightyLogicService implements ExtensibleService{
private String directoryPath="c:\hello";
public void doLogic(){
//never forget to call init() before invocation or build safeguards
init();
//some logic goes here
}
public void init(){}
}
public class WeightyLogicService_myAdaptation extends WeightyLogicService {
#Override
public void init(){
directoryPath="c:\my_hello";
}
}

Categories