Super() not working on my extends class - java

I'm pretty new in java and I'm doing a simple program but I don't know why I get an error in my program when I'm try to use super... Does anybody can explain me or what is my error, because it's not accepting super.myCoord() what should I change or add?
public class myCoord {
private double coorX, coorY;
public myCoord(){
coorX = 1;
coorY = 1;
}
public myCoord(double x,double y){
coorX = x;
coorY = y;
}
void setX(double x){
coorX = x;
}
void setY(double y){
coorY = y;
}
double getX(){
return coorX;
}
double getY(){
return coorY;
}
public String toString(){
String nuevo = "("+coorX+", "+coorY+")";
return nuevo;
}
public class Coord3D extends myCoord{
private double coorZ;
Coord3D(){
super.myCoord(); // ---> I got an error here !!
coorZ = 1;
}
Coord3D(double x, double y, double z){
super.myCoord(x,y); ---> Also here !!
coorZ = z;
}
void setZ(double z){
coorZ = z;
}
double getZ(){
return coorZ;
}
}

Calling the super's constructor in Java is done by super(), either with arguments or without. In your case:
public class Coord3D extends myCoord{
private double coorZ;
Coord3D(){
super();
coorZ = 1;
}
Coord3D(double x, double y, double z){
super(x,y);
coorZ = z;
}
// rest of the class snipped
}

You should call methods, not constructors, using the dot (.) operator. Here you are calling super class' constructor using dot (.).
That's why you are getting errors like these:
The method myCoord() is undefined for the type myCoord
and
The method myCoord(double, double) is undefined for the type myCoord
Use these to call your super constructor: super(); and super(x,y); as shown below.
public class Coord3D extends myCoord {
private double coorZ;
Coord3D() {
super(); // not super.myCoord(); its a constructor call not method call
coorZ = 1;
}
Coord3D(double x, double y, double z) {
super(x,y); // not super.myCoord(x,y); its a constructor call not method call
coorZ = z;
}
}

public class myCoord {
private double coorX, coorY;
public myCoord(){
coorX = 1;
coorY = 1;
}
public myCoord(double x,double y){
coorX = x;
coorY = y;
}
void setX(double x){
coorX = x;
}
void setY(double y){
coorY = y;
}
double getX(){
return coorX;
}
double getY(){
return coorY;
}
public String toString(){
String nuevo = "("+coorX+", "+coorY+")";
return nuevo;
}
public class Coord3D extends myCoord{
private double coorZ;
Coord3D(){
super(); // ---> I got an error here !!
coorZ = 1;
}
Coord3D(double x, double y, double z){
super(x,y); ---> Also here !!
coorZ = z;
}
void setZ(double z){
coorZ = z;
}
double getZ(){
return coorZ;
}
}

super()
super(x,y);
they should be like that, you are calling constructor

Related

How to use methods which are not in interface with interface i = new class();

I am only able to access methods/vars of m1/m2 which are in the interface Measurable how do i access other methods and vars?
How is it possible to use the getLength() AND getBreadth()/getRadius() method of m1/m2 object in the getArea() method? Thanks in Advance!
public class Main {
public static void main(String[] args) {
Circle c = new Circle(10);
Rectangle r = new Rectangle(10,5);
addArea(c, r);
Measurable Rec = new Rectangle(10, 5);
Measurable Cir = new Circle(10);
addArea(Rec, Cir);
}
public static void addArea(Measurable m1, Measurable m2){
String m1s = null,m2s = null;
if(m1 instanceof Rectangle){
m1s="Rectangle";
}
else if(m1 instanceof Circle){
m1s="Circle";
}
if(m2 instanceof Circle){
m2s="Circle";
}
else if(m2 instanceof Rectangle){
m2s="Rectangle";
}
System.out.println("Area of "+m1s+" and "+m2s+" is "+(m1.getArea()+m2.getArea())+"\n");
}
}
interface Measurable{
double PI = 3.14;
public double getPerimeter();
public double getArea();
}
class Rectangle implements Measurable{
public double breadth, length;
public Rectangle(int breadth, int length){
this.breadth = breadth;
this.length = length;
}
#Override
public double getPerimeter() {
return 2*(breadth+length);
}
#Override
public double getArea() {
return length*breadth;
}
public double getLength(){
return length;
}
public double getBreadth(){
return breadth;
}
}
class Circle implements Measurable{
public double radius;
public Circle(int radius){
this.radius = radius;
}
#Override
public double getPerimeter() {
return 2*PI*radius;
}
#Override
public double getArea() {
return PI*radius*radius;
}
public double getRadius(){
return radius;
}
}
You should cast your objects, do that in your if blocs and assign the value to a String object. The reference of the interface can not make a call to a method written in one class that implements it.
Solution
E.g for getLength() and m1
String m1Length = null;
if(m1 instanceof Rectangle){
m1s="Rectangle";
m1Length = ((Rectangle)m1).getLength());
}
else if(m1 instanceof Circle){
m1s="Circle";
m1Length = ((Circle)m1).getLength());
}
Apply this logic to the rest of the code and you'll be fine.

Generic behaviour

(sorry for the pun)
Say one wants to define a generic builder, like this:
public abstract class GenericBuilder<T extends Product> {
int x;
int y;
<K extends GenericBuilder<T>> K setX(int x) {
this.x = x;
return (K)this;
}
<K extends GenericBuilder<T>> K setY(int y) {
this.y = y;
return (K) this;
}
abstract T build();
}
abstract class Product {
int x;
int y;
}
class ConcreteProduct extends Product {
int z;
}
class ConcreteBuilder extends GenericBuilder<ConcreteProduct>{
int z;
<K extends GenericBuilder<ConcreteProduct>> K setZ(int z) {
this.z = z;
return (K) this;
}
#Override
ConcreteProduct build() {
ConcreteProduct cp = new ConcreteProduct();
cp.x = x;
cp.y = y;
cp.z = z;
return cp;
}
public static void main(String[] args) {
new ConcreteBuilder().setX(1).setY(2).setZ(3);
}
}
When calling ConcreteBuilder.setZ(), it fails during compilation.
Why is that? Is it due erasure? Or the generics, say, don't carry information about its generic parameters?
EDIT:
Any ideas how to avoid using second generic parameter in:
public class ConcreteBuilder extends GenericBuilder<ConcreteProduct, ConcreteBuilder>
i.e. <..., ConcreteBuilder>, which seems to be a little clumsy? I guess it's not possible. Are there other languages (C# maybe?) which allow to do that?
Break your code this way and you will understand that your class GenericBuilder<ConcreteProduct> doesn't have any setZ() method defined.
GenericBuilder<ConcreteProduct> setY = new ConcreteBuilder().setX(1).setY(2);
setY.setZ(3);
In your GenericBuilder your functions return a GenericBuilder when you don't specify the type argument of the function. In your main function the call to setX returns a GenericBuilder and you loose the information that you are actually using a ConcreteBuilder. To succesfully make the calls, you have to specify the generic parameters for the setters:
new ConcreteBuilder().<ConcreteBuilder>setX(1).<ConcreteBuilder>setY(2).setZ(3);
Alternative
You can add a second type parameter to GenericBuilder:
public abstract class GenericBuilder<T extends Product, K extends GenericBuilder<T, K>> {
int x;
int y;
K setX(int x) {
this.x = x;
return (K)this;
}
K setY(int y) {
this.y = y;
return (K) this;
}
abstract T build();
}
and change ConcreteBuilder to this:
public class ConcreteBuilder extends GenericBuilder<ConcreteProduct, ConcreteBuilder> {
int z;
ConcreteBuilder setZ(int z) {
this.z = z;
return this;
}
#Override
public ConcreteProduct build() {
ConcreteProduct cp = new ConcreteProduct();
cp.x = x;
cp.y = y;
cp.z = z;
return cp;
}
public static void main(String[] args) {
new ConcreteBuilder().setX(1).setY(2).setZ(3);
}
}

Accessing values of a given object

I am wondering why i have to deal with two types of arguments;that of a constructor and that of a method.For instance i have this simple class that adds two numbers
class Calc{
private int x = 6;
private int y;
private char z = 'z';
public int getx(){
return x;
}
public char selfrecur(){
return this.z;
}
public int add(int one,int two){
return one + two;
}
public static void main(String[] args) {
Calc gx = new Calc();
System.out.println(gx.x);
System.out.println(gx.add(44,3));
System.out.println(gx.selfrecur());
}
}
That works,and wow,wasn't that great.Now,i have this idea of having the constructor provide the arguments and the function's work will be to do the heavy computations.For instance in my class Kalc
class Kalc{
//** This example won't work **
private int x;
private int y;
private int z;
public Kalc(int v1,int v2,int v3){
this.x = v1;
this.y = v2;
this.z = v3;
}
public int add(){
return newObject.x + newObject.y + newObject.z;
//Gets the values of a new object and add them up
}
public int multiply(){
return newObject.x * newObject.y * newObject.z;
//Gets the values of a new object and multiply them
}
public static void main(String[] args) {
Kalc k = new Kalc(4,5,6);
System.out.println(k.add());
System.out.println(k.multiply());
}
}
I have been looking here http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html for clues but so far nothing.Is this even possible?.
Edit
class Kalc{
private int x;
private int y;
private int z;
public Kalc(int v1,int v2,int v3){
this.x = v1;
this.y = v2;
this.z = v3;
}
public int add(){
return this.x + this.y + this.z;
}
public static void main(String[] args) {
Kalc k = new Kalc(4,5,6);
System.out.println(k.add);
}
}
Error
C:\ja>javac Kalc.java
Kalc.java:17: error: cannot find symbol
System.out.println(k.add);
^
symbol: variable add
location: variable k of type Kalc
1 error
C:\ja>
Use this key word:
public int add(){
return this.x + this.y + this.z;
}
You can use this key word inside non-static methods too.
About your edit:
add is a function (and not a member) of class Kalc so you can call it as a function only:
System.out.println(k.add());
You can do the below
class Kalc{
private int x;
private int y;
private int z;
public Kalc(int v1,int v2,int v3)
{
this.x = v1;
this.y = v2;
this.z = v3;
}
public int add(){
return x+y+z;
}
public int multiply(){
return x*y*z;
}
public static void main(String[] args) {
Kalc k = new Kalc(4,5,6);
System.out.println(k.add());
System.out.println(k.multiply());
}
}
What is newObject?
You have instantiated an object with prescribed values. If you want to add them with an instance method, try this
return this.x + this.y + this.z;
I think you need to print :
System.out.println(k.add());
Instead of :
System.out.println(k.add);
as in the second case the compiler show k.add as add variable
but in the first case add() the compiler show add() as a function which you define in Kalc Class

How can I use a 'next generation' java data object style together with interfaces?

I'm writing most of my immutable data objects in the following style, which is somtimes described as 'next generation' or 'functional':
public class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
I would like to use the same style for data objects specified by interfaces:
public interface Point {
public final int x;
public final int y;
}
public class MyPoint {
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Origin {
public Origin() {
this.x = 0;
this.y = 0;
}
}
But this is not allowed by java, which gives an error in the interface code as well as the implementations.
I can change my code to
public interface Point {
public int x();
public int y();
}
public class MyPoint {
private int mx, my;
pulic MyPoint(int x, int y) {
mx = x;
my = y;
}
public int x() {return mx;}
public int y() {return my;}
}
public class Origin {
public int x() {return 0;}
public int y() {return 0;}
}
But it is more code, and I don't think it gives nearly the same feeling of simplicity in the API.
Can you see a path out of my dilemma? Or do you personally use a third, even simpler style?
(I'm not really interested in a discussion of mutable/immutable, getterSetter/new-style or private/public fields.)
I would rather switch to use inheritance or delegation
public class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Inheritance
public class MyPoint extends Point {
public MyPoint (int x, int y) {
super (x, y);
}
....
}
public class Origin extends Point {
public Origin () {
super (0, 0);
}
}

Defining an enum in Java

I have a class in Java describing a parameter (name: Param) and another class in which I declare and initialize around 100 of such parameters as:
private static final Param param_name_1 = new Param(x, y, z);
I would like to put all these objects/instances in an enum and initialize them there. What is the best method to do that?
===UPDATE===
I asked for the syntax of the enum but not like that.
I my case Param is another java class which has its own parameters, getters and setters and a constructor with the 3 parameters between the paranthesis:
public Param(intx, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
I my other class I declare and initialize 100 instances of the class Param as written above. Each x, y and z for each instance are different.
And the enum should contain the declaration of the instances and possibly also initialize them.
Assuming that you're asking about the enum syntax then you can do something like this (assuming that x, y and z are compile time constants).
public enum Param {
param_name_1(1,2,3),
param_name_2(3,4,5);
private int x;
private int y;
private int z;
private Param(int x, int y, int z) {
this.x=x;
this.y=y;
this.z=z;
}
}
An enum is a special type of class, so you can declare constructors, fields and methods as well as implement interfaces. However, they can not extend other classes.
In this case, I'd suggest final fields and getters, like this:
public enum Param {
PARAM_A(1, 2, 3),
PARAM_B(4, 5, 6),
PARAM_C(1, 3, 5);
private final int x;
private final int y;
private final int z;
private Param(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
}
I think this is what you want
class Param
{
private int x, y, z;
public Param(int x, int y, int z)
{
this.x = x; this.y = y; this.z = z;
}
}
public class t
{
private enum ParamVals
{
VAL1(new Param(0,0,0)),
VAL2(new Param(1,1,1));
private Param paramVal;
private ParamVals(Param paramVal)
{
this.paramVal = paramVal;
}
public Param getVal()
{
return paramVal;
}
}
}

Categories

Resources