Java print method won't work in class - java

I am trying to get the print method in my actor class to print the String that was
built in the toString() method. However I keep getting an error. (invalid method declaration, return type required)
public class actor {
private String name;
private String address;
private int age;
public actor(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
public void setName (String name) {
this.name = name;
}
public void setAddress (String address) {
this.address = address;
}
public void setAge (int age) {
this.age = age;
}
public void setFilm () {
}
public String getName () {
return name;
}
public String getAddress () {
return address;
}
public String toString (String name, int age, String address){
return name+" who's "+age+" and lives in "+address;
}
public void print (){
String a = toString();
System.out.println(a);
}
print();
}
I have been trying to get this working for quite a while to no avail.

Here's the two parts to the trouble you're having:
In order to run your program, you have to have a main method.
You have to understand what static and non-static mean.
First, as mentioned by others, you can't just have a method run because it's declared and defined in your class. You actually need to have it called, directly or indirectly, by the main method. The main method is written like this:
public static void main(String[] args) {
// Do Stuff
}
Secondly, you have to understand what static and non-static mean. Which means, you have to understand the difference between classes and objects.
Classes are blue prints. They describe how to build a particular type of object, what properties (or fields) it has, and what methods can be called off of it.
Objects are the actual instances of objects declared by a class. Think of it like this: A class is like the blueprint for a smart car. The objects are the smart cars themselves.
So now, static vs non-static.
Static means that it belongs to the class (the blueprint), rather than to the actual object. The main method, you will notice, is static. It belongs to the class it's declared in, rather than to any instance objects. This means, outside of itself, the main method knows only about the class that it's in, and any other static methods or objects in that class. Things that are not static belong to the actual objects created from the class -- and the main method will know nothing about these actual objects, unless they are created inside of the main method itself.
So, something like this won't work:
public class StuffDoer {
public void doStuff {
System.out.println("Doing Stuff");
}
public static void main(String[] args) {
doStuff(); // Won't work!
// You can't call a non-static, instance method in a static method!
}
}
Instead, you can first create a new instance object of your class inside of the main method, and then call the non-static instance method off of your instance object:
public class StuffDoer {
public void doStuff {
System.out.println("Doing Stuff");
}
public static void main(String[] args) {
new StuffDoer().doStuff(); // This will work,
// because now you have an instance to call the instance method off of.
}
}
This is usually not as good of a choice, but will also work:
public class StuffDoer {
public static void doStuff { //Now, we make this method static
System.out.println("Doing Stuff");
}
public static void main(String[] args) {
doStuff(); // This will work now, because this method is static.
}
}

You're trying to call print() from your class body. Instead, write a main method and print from there:
public static void main(String[] args) {
Actor a = new Actor(...);
a.print();
}

You should have a main function to let the program run, like:
remove the last line print() then create a new file call Main.java, write
package yourPackage // put them into the same package,
main class can call actor class
public class Main{
public static void main(String[] args) {
actor a = new actor();
a.print();
}
}

Why do you need print() method ? You can just use -
Actor a = new Actor(...);
System.out.println(a);
This will implicitly execute toString() method

Ideally you should do this way. Since purpose of toString() method is to give meaningful String representation of an object.
actor actorObj = new actor();
System.out.println(actorObj );

Calling print(); on class body is invalid. Remove following method call.
print();

First off, you should be calling the print() method from somewhere else (main for example). Even with that, you have an error: You are calling the toString() method (with no arguments, which is taken from the Object class). Just remove the arguments from your toString method to override that one. It can see the fields of its own class anyways. With this, you can do something like the following, and take advantage of Java's default toString call:
public static void main(String[] args) {
System.out.println(new Actor("Bob", "410 Main Street", 42);
}

Java does not allow you to call a method in the body of a class as you are attempting to do with the line of code that is print(); You must put the call to print inside another method. For example
public static void main(String[] args) {
actor a = new actor();
a.print();
}

By adding a main method to your class and using the constructor for Actor in that method you create an Author object. On this Author object call print().
TJamesBoone has given a really good answer to give you an understanding of what is really happening. Follow his answer and it will do as you want.
https://stackoverflow.com/a/19981973/1785341

here is your code.. compile and run..
public class actor {
private String name;
private String address;
private int age;
public actor(String name, String address, int age) {
this.name = name;
this.address = address;
this.age = age;
}
public void setName (String name) {
this.name = name;
}
public void setAddress (String address) {
this.address = address;
}
public void setAge (int age) {
this.age = age;
}
public void setFilm () {
}
public String getName () {
return name;
}
public String getAddress () {
return address;
}
#Override
public String toString (){
return name+" who's "+age+" and lives in "+address;
}
public void print (){
//String a = toString();
System.out.println(this);
}
public static void main( String[] args )
{
actor a = new actor( "xyz","abc",20 );
a.print();
}
}

Simple as it is....
Once you write toString() method in a class then do another method called print() and call inside the print() method toString() method.
public void print()
{
System.out.println(toString());
}

Related

Get the property from a private variable

I created 2 methods. Method1 assigns value to a variable via setter, method2 gets the value of the variable via a getter.
However, when I call method2 the program returns null or null. Only private variables can be used, do not use public or static.
Is there any solution or code example for me? Sorry, I'm a newbie. Thanks very much!
class Info {
private String name;
private int age;
public Info() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.method1();
test.method2();
}
public void method1() {
Info a = new Info();
a.setName("John");
a.setAge(21);
}
public void method2() {
Info a = new Info();
//I want to print Name and Age values from class Info after method1 set value
System.out.println("Name: " + a.getName() + " Age: " + a.getAge());
}
}
The scope of Info a = new Info(); is limited to the method that you have declared it in. Consider making it a field.
public class Test {
private Info a = new Info();
Test test = new Test();
test.method1();
test.method2();
}
public void method1() {
a.setName("John");
a.setAge(21);
}
public void method2() {
System.out.println("Name: "+ a.getName() + " Age: " + a.getAge());
}
}
Alternatively you could create local variables, but pass them between the methods:
In main
Info a = new Info();
Test test = new Test();
test.method1(a);
test.method2(a);
Of course this would require you change you method signatures to
public void method1(Info a) {...}
public void method2(Info a) {...}
A private global variable can only be set by a method that is private.
A method that is private can only be called inside the class it is coded(implemented).
However, inside the class you can mix security declarations levels of variables in code, but to set or get the method used must be as the variable it sets or gets with or without a method return statement.
To set and obtain(get) anything private from calls located outside the class you must use a public method inside the same class to call the private methods or if the calling class is in the same (class package hierarchy) packages hierarchy level and package tree you can use a protected method to call from such an external class but that in the external class must be called by a protected method (potentially)returning to a protected variable.
If there is only the default level "no access security declaration of the class" given (e.g. public protected - nothing stated but "class NamedAClass"), of such a class with the get set then it is similar but more lax security than private using default security level, with default security level , you can only call from a class located in the exact same package as the class with the code to call.

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.

No suitable Constructor Found

This is my code and i am trying to pass the parameter from main to cat class but its saying no constructor cant figure out what to do a little help would be appreciated.
public class Cat extends Animal implements Pet {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cat(String name, int legs) {
super(4);
this.name = name;
}
public Cat() {
this("Fluppy"); //ERROR OVER HERE
}
#Override
public void play() { //THIS METHOD IS OVERRIDDEN FROM PET INTERFACE
System.out.println(name+"Likes to play with string");
}
#Override
public void eat() { /*THIS METHOD IS OVERRIDDEN FROM ANIMAL ABSTRACT METHOD.*/
System.out.println("Cats likes to eat spiders and fish");
}
}
and the main class
public class PetMain {
public static void main(String[] args) {
Animal a;
Pet p;
Cat c= new Cat("Tom"); //IM GETTING THE ERROR OVER HERE.
c.eat();
c.walk();
c.play();
}
}
try using the correct constructor which takes two parameters
Cat c= new Cat("Tom", 4);
and
this("Fluppy", 4);
or make a new constructor for one parameter like
public Cat(String name) {
this (name, 4);
}
Take a look at your constructors in Cat
public Cat(String name, int legs) { // accept String and int constructor
super(4);
this.name = name;
}
public Cat() { // no argument constructor
this("Fluppy");
}
There is no matching for new Cat("String")
You can add new constructor
public Cat(String anyThing) {
}
First thing when you call this
Cat c= new Cat("Tom");
It expects that you Cat class have a single argument constructor which your class doesnot contain so create a single argument constructor in your Cat class like this
public Cat(String str) {
// your logic
}
Secondly this("Fluppy"); //ERROR OVER HERE
If you know about constructor chaining then you would not have done this. this() is usually used when you want to call another constructor of the same class from within one constructor in your case you are calling one-parameterized constructor from you default constructor since one-parameterized constructor doesnot exist it is giving you compilation error
You are trying to overload the constructor at:
public Cat() {
this("Fluppy"); //ERROR OVER HERE
}
but the call made is for the constructor with one String argument. You do not have a constructor with one String argument , So you have an error try to add.
public Cat(String catty) {
// initialise
}

Static method vs non static method java better understanding

I still cannot fully understand this static and non static.
public static void main(String args[]){
staticClass.setString("hey there");
System.out.println(staticClass.getString2());
//expecting to be blank
NonStaticCalling nonStaticCalling = new NonStaticCalling();
}
static String aw = "";
public static void setString(String a){
aw =a;
}
public String getString(){
return aw;
}
public static String getString2(){
return aw;
}
public class NonStaticCalling {
staticClass staticClass = new staticClass();
public NonStaticCalling(){
staticClass.getString();
System.out.println(staticClass.getString());
}
}
If i understand correctly. I declare a new object nonstaticcalling. So i assume that the value of the output from that class is "" (blank)
Can someone give me a better exmaple? thanks
When a static variable is set, it is the same for all instances of the class. Static variables are also known as "class variables". I think your confusion is actually about the variable more so than the methods. Take this example with no static variables as a simple example. "name" is the same for all instances of the class "myName" (sorry should've made it capital since it's a class name).
public class myName {
public static String name;
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
public static void main(Strings args[]) {
myName first = new myName();
myName second = new myName();
first.setName("hello");
System.out.println(second.getName()); //prints hello
}
}
Static variables are created only one for all the objects of that StaticClass so you're return the same static variable from newly created object.
For one, you can call
NonStaticCalling.getString2()
but not
NonStaticCalling.getString()
A static method can be called without instantiating the class.
SomeName.setString("hey there");
System.out.println(SomeName.getString2());
//expecting to be blank
SomeName object = new SomeName();
object.setString2("hey there");
System.out.println(object.getString());
public class SomeName
{
static String aw = "";
String aw2 = "";
public SomeName()
{
}
public static void setString(String a){
aw =a;
}
public void setString2(String a){
aw2 =a;
}
public String getString(){
return aw;
}
public static String getString2(){
return aw;
}
}
This will print what you got! so the difference is that in one you are using a static property of the class, this means that if you change it, it changes for every other object using it in the future!
In the second one you are using an "object" or an instance of the class, this means that all variables are only set to that object while it lives! If you create a new one you will have to set up aw2 again for it!

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