inheritance and protected class java - java

error: mul(int,int) has protected access in Multiplication
Thoughts on what I'm doing wrong?
public class Multiplication{
protected int mul(int a,int b){
return (a*b);
}
}
class ImplimentPkg extends Multiplication {
public static void main(String args[]){
Multiplication obj=new ImplimentPkg();
//Here's the error
System.out.println(obj.mul(2,4));
}
protected int mul(int a,int b){
System.out.println("");
return 0;
}
}

Java tutorial says:
The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
You may think you have matched the second case(inheritence).
Multiplication obj = new ImplimentPkg();
System.out.println(obj.mul(2, 4));
This means you are using an instance of parent class Multiplication. Although the code is inside a method of subclass of Multiplication. It doesn't mean the instance obj has something to do with inheritance. And of course the mul(...) method is invisible. What you can do is: use keyword super.
public void bar() {
Multiplication mul = new Multiplication();
mul.mul(1, 2); // <- error
super.mul(1, 2); // correct
Multiplication.foo(); // correct
}
Note: if you have protected static method in parent class, like:
public class Multiplication {
private String name = "some";
protected int mul(int a, int b) {
return (a * b);
}
protected static void foo() {
System.out.println("foo");
}
}
Here the foo() method can be accessed everywhere in subclass, but not other classes. By the way, you shouldn't use protected static method, see reasons here.
Another related topic may be interested to you, see here.

Create each class in its own Java file
ImplimentPkg.java
package my;
class ImplimentPkg extends Multiplication {
public static void main(String args[]) {
Multiplication obj = new ImplimentPkg();
System.out.println(obj.mul(2, 4));
}
protected int mul(int a, int b) {
System.out.println("");
return 0;
}
}
Multiplication.java
package my;
public class Multiplication {
protected int mul(int a, int b) {
return (a * b);
}
}

Related

How to make a method with a return statement that is inside an abstract class which implements an interface get inherited by a subclass?

I will post the part of the code that I have trouble with. I scoured the internet for hours and cannot find a solution to how to properly solve this problem with a method that has a return and isn't just a sentence. I would seriously appreciate any help that I can get! Essentially what I need is for my subclass to inherit the same method in its abstract father without any changes, so that I can apply that in my main code.
public class Main {
interface Dodela
{
public int PovecajKS();
public int SmanjiS();
}
public static abstract class Ekspanzija implements Dodela
{
final public int PovecajKS(int a, int b)
{
a = a + b;
return a;
}
final public int SmanjiS(int a, int b)
{
a = a - b;
return a;
}
}
public static class EkspanzijaP extends Ekspanzija implements Dodela
{
public int PovecajKS()
{
}
public int SmanjiS() {
int a = 0, b = 0;
a = a - b;
return a;
}
}
It already inherits all methods of its father, to use the method inside the subclass, it can be called like this
super.yourFunctionName()

Static and non-static method in one class with the same name JAVA

I know it is impossible to override a method in one class. But is there a way to use a non-static method as static? For example I have a method which is adding numbers. I want this method to be usefull with an object and also without it. Is it possible to do something like that without creating another method?
EDIT:
What I mean is, if I make a method static I will need it to take arguments, and if I create an object with variables already set it will be very uncomfortable to call function on my object with same arguments again.
public class Test {
private int a;
private int b;
private int c;
public Test(int a,int b,int c)
{
this.a = a;
this.b = b;
this.c = c;
}
public static String count(int a1,int b1, int c1)
{
String solution;
solution = Integer.toString(a1+b1+c1);
return solution;
}
public static void main(String[] args) {
System.out.println(Test.count(1,2,3));
Test t1 = new Test(1,2,3);
t1.count();
}
}
I know the code is incorrect but i wanted to show what I want to do.
I want this method to be usefull with an object and also without it.
Is it possible to do something like that without creating another
method?
You will have to create another method, but you can make the non-static method call the static method, so that you do not duplicate the code and if you want to change the logic in the future you only need to do it in one place.
public class Test {
private int a;
private int b;
private int c;
public Test(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public String count() {
return count(a, b, c);
}
public static String count(int a1, int b1, int c1) {
String solution;
solution = Integer.toString(a1 + b1 + c1);
return solution;
}
public static void main(String[] args) {
System.out.println(Test.count(1, 2, 3));
Test t1 = new Test(1, 2, 3);
System.out.println(t1.count());
}
}
But is there a way to use a non-static method as static?
No, it's not possible.
If you need this method to be used in static and non-static context, then make it static. The opposite configuration, however, is not possible.
Make it static, then it can be used with object and without it.
public class MyTest() {
public static int add() {
System.out.println("hello");
}
}
MyTest.add(); //prints hello
MyTest myobject = new MyTest();
myobject.add(); //prints hello

How do I use the "this" keyword in java?

I've been using multiple methods, but my "java the complete reference" book doesn't do a good job of explaining how to use the "this" keyword.
this in java
It is used to refer to the data members of the object in the envoked method or constructor in case there is a name conflict between fields and local variables
public class Test {
String s;
int i;
public Test(String s, int i){
this.s = s;
this.i = i;
} }
It is used to invoke one constructor from another constructor of the same class or you can say constructor chaining.
public class ConstructorChainingEg{
String s;
int i;
public ConstructorChainingEg(String s, int i){
this.s = s;
this.i = i;
System.out.println(s+" "+i);
}
public ConstructorChainingEg(){
this("abc",3); // from here call goes to parameterized constructor
}
public static void main(String[] args) {
ConstructorChainingEg m = new ConstructorChainingEg();
// call goes to default constructor
}
}
It also facilitates method chaining
class Swapper{
int a,b;
public Swapper(int a,int b){
this.a=a;
this.b=b;
}
public Swapper swap() {
int c=this.a;
this.a=this.b;
this.b=c;
return this;
}
public static void main(String aa[]){
new Swapper(4,5).swap(); //method chaining
}
}
Here's a couple:
public class Example {
private int a;
private int b;
// use it to differentiate between local and class variables
public Example(int a, int b) {
this.a = a;
this.b = b;
}
// use it to chain constructors
public Example() {
this(0, 0);
}
// revised answer:
public int getA() {
return this.a;
}
public int getB() {
return this.b
}
public int setA(int a) {
this.a = a
}
public void setB(int b) {
this.b = b;
}
}
this refers to the attributes that belong to the object in which this is used in. For example:
Example ex1 = new Example(3,4);
Example ex2 = new Example(8,1);
In these cases, ex1.getA() will return 3, because this is referring to the a that belongs to the object named ex1, and not ex2 or anything else. The same goes for ex2.getB().
If you look at the setA() and setB() methods, using this distinguishes the attributes a and b belonging to the object from the parameter names as they're the same.

Error in return statement in generic methods

My problem is with the return statement in each method,the error in netbeans says:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - bad operand types for binary operator '+'
first type: T
second type: T
at GenericMath.add(GenericMath.java:8)
at GenericMath.main(GenericMath.java:20)
public class GenericMath<T> {
public T a,b;
public T add() {
return a+b;
}
public T multiply() {
return (a*b);
}
public static <T> void main(String[] args) {
GenericMath<Integer> x=new GenericMath<Integer>();
x.a=5;
x.b=10;
int z=x.add();
GenericMath<Double> y = new GenericMath<Double>();
y.a = 5.5;
y.b = 10.2;
double g=y.multiply();
}
}
The compiler doesn't know that you can multiply and add T values - it's not the return part which the problem, it's the expression itself. You'll see the same effect if you split the two parts:
T result = a + b;
return result;
It will be the first line that fails - and there's no particularly clean answer to this. Generics just aren't built for this sort of work in Java. What you could do is have:
public abstract class GenericMath<T extends Number> {
public abstract T add(T a, T b);
public abstract T multiply(T a, T b);
// etc
}
public final class IntegerMath extends GenericMath<Integer> {
public Integer add(Integer a, Integer b) {
return a + b;
}
// etc
}
... and similar classes for DoubleMath etc.
Then:
// Alternatively, have a static factory method in GenericMath...
GenericMath<Integer> math = new IntegerMath();
int x = math.add(5, 2);
You need to do these things:
Bound the generic type to Number
Make the add() etc method abstract and return T
Provide implementations for each type you want to support
Like this:
public abstract class GenericMath<T extends Number> {
public T a,b;
public abstract T add();
public abstract T multiply();
}
public class IntegerGenericMath extends GenericMath<Integer> {
public Integer add() {
return a + b;
}
public Integer multiply() {
return a * b;
}
}
// similar for Double
public static void main(String[] args) {
IntegerGenericMath x=new IntegerGenericMath();
x.a=5;
x.b=10;
int z=x.add();
DoubleGenericMath y = new DoubleGenericMath();
y.a = 5.5;
y.b = 10.2;
double g=y.multiply();
}
There's a lot of auto boxing going on here, which won't work genericly, which is why you need separate classes for each type.

get super class value in java

I have 2 classes:
public class A
{
int n = 10;
public int getN()
{
return n;
}
}
public class B extends A
{
int n = 20;
public int getN()
{
return n;
}
}
public class Test
{
public static void main(String[] args)
{
B b = new B();
System.out.println(b.getN()); //--> return 20
System.out.println(((A)b).getN()); //--> still return 20.
//How can I make it return 10?
}
}
All methods in Java are always virtual. That is, there is no way of invoking the "super"-version of the method from the outside. Casting to A won't help as it doesn't change the runtime type of the object.
This is probably your best alternative / workaround:
class A {
int n = 10;
public int getN() {
return n;
}
public final int getSuperN() { // "final" to make sure it's not overridden
return n;
}
}
class B extends A {
int n = 20;
public int getN() {
return n;
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
System.out.println(b.getN()); // --> return 20
System.out.println(((A)b).getN()); // --> still return 20.
System.out.println(b.getSuperN()); // --> prints 10
}
}
you can't make the value be "10" because the instance of the object was for class B, and when you do the cast the only thing that are you doing is changing the define class not setting values for the object B, in other words if you need to get 10 its' something like this
b = new A();
That thing won't work due to polymorphism. Class B is still class B even if you cast it into its super class.
I think you'll need something like this:
public class B extends A
{
int n = 20;
/**
* #return the super n
*/
public int getSuperN()
{
return super.n;
}
}
What you see is polymorphism in action. Since b is a B, that method (which returns 20) is always called (regardless if you cast it to an A).

Categories