Delupara
Delupara

Reputation: 359

cant make constructor even if using same name method

I made two classes to try to understand class extension. here are the two classes I have

first one is main

public class main extends Ok {
    public static void main (String arg[]) {
        new Ok();
    }
}

And then I have Ok class

public class Ok {
    public void Ok(){
        System.out.println("k");
    }
}

I get the warning "Ok is never used". Why?

Upvotes: 0

Views: 81

Answers (2)

Frakcool
Frakcool

Reputation: 11153

You're creating a new void method not a constructor... Remove void. Also don't name your classes as main it may cause problems because of main method.

public class Ok {
    public Ok(){
        System.out.println("k");
    }
}

Like that, so it's a constructor not a void method :)

Class extension is called inheritance too.

Imagine we're talking about Mamals (this is our class name) but we know all Mamals are (important word to remember) an Animal.

We know all Animals can eat(), pee() and appeal().

When you write class Animal:

public class Animal {
    public void eat() {...};
    public void pee() {...};
    public void appeal() {...};
}

And then write the class Mamal:

public class Mamal extends Animal {
}

It will have all the methods from Animal class except if they're private

For more information about inheritance you can read this link provided by @JorgeCampos on the comments below (Thanks!)

Upvotes: 4

RockAndRoll
RockAndRoll

Reputation: 2277

Constructors have no return type, are not inherited, and cannot be hidden or overridden by subclasses.

Remove void and you will be good.

Upvotes: 0

Related Questions