简体   繁体   中英

Confusion about extended classes in Java

I am having some confusion about extended classes. I think the best way to explain what I want to do is with some skeleton code:

abstract class Player
{
  public int solve()
  { // One method of solving problem
  }
}

class otherPlayer extends Player
{ 
  public char solve(int num)
  { //Different method of solving same problem
  }
}

// I suspect this is a wrong way to create Player
Player first = new otherPlayer;
// Because this gives me an error...
first.solve(5)'
// And this uses the super class's method of solving problem
first.solve();
// I don't want to do this though...
otherPlayer first = new otherPlayer;

// Because I actually define the Players in a method
// that returns a type Player():
private Player genPlayer()
{ // Take input from keyboard
  Player newPlayer;
  if (specific set of answers)
    newPlayer = new otherPlayer();
  else newPlayer = new Player();

  return newPlayer;
}

My understanding of extended classes is limited. If I say "TypeA var = new TypeB", and TypeB extends TypeA, it seems that var only has access to methods in the TypeA class. What does it do, then to say it is a new TypeB? Why is that even a legal way to instantiate a variable? And how would you recommend I restructure this program so I can make it work (I'd prefer to keep the method, so I don't have to make a mess every time I create a Player)?

I really just want to know how to create an extended class as if I were a person who knew what he was doing.

The point of TypeA a = new TypeB() is programming to contract. It means you can change TypeB into any other type that extends TypeA and be guaranteed not to have to change any other line of your code.

You need to do something like:

abstract class Player {
    abstract void solve(int num);
}

class OtherPlayer extends Player {
    void solve(int num) {
        //...
    }
}

That is, to call a method through variables of type Player , at least its signature must be declared on that class - and must be implemented in all subclasses. There is no way to call a method that may or may not exist in a subclass using a variable of the superclass type.

Imagine you could do this:

class Player {
    // ...
}

class FooPlayer extends Player {
    void solveInt(int num) { 
        // ... 
    }
}

class BarPlayer extend Player {
    void solveString(String s) {
        // ...
    }
}

Player[] players = new[] {new FooPlayer(), new BarPlayer()};
// this is the sort of code you want to work
for (Player p : players) {
    p.solveInt(123);
}

what's supposed to happen when the method is called on BarPlayer ? As you can see, it doesn't make much sense to allow this.

While it's not very useful to do Player player = new FooPlayer() explicitly, it does let you not have to know which exact subtype of Player the value is. It's missing the point to look at the methods that are different between the subtypes - the point of polymorphism is that the same method (ie with the same signature) is implemented in a different way between the subclasses:

class Player {
    abstract String greet();
}

class EnglishPlayer extends Player {
    String greet() {
        return "Hello";
    }
}

class JapanesePlayer extends Player {
    String greet() {
        return "Konnichi wa";
    }
}

first, you are getting an error in the instantiation syntax. It should be new otherPlayer() . Also classes should be capitalized in java. I assume the code is within a method, otherwise it will not compile. Finally, to answer your question why you extend a class, if otherPlayer had its own implementation solve() (no arguments) then which method would be called would depend on the instantiation you used, not on the variable type. In other words, first.solve() would call the method in otherPlayer, not in player.

extension is about superclasses and subclasses.

if typeA is a subclass of typeB, then you could say

typeA extends typeB, and typeA would inherit all of the methods of typeB

it only works one way, kind of like a child can inherit traits from their parents, but the parents don't inherit anything from the child

if you want a method in the subclass to behave differently than in the parent class, simply write a new method with the same name/parameters. subclass methods automatically override superclass methods. then, if you want to use the superclass version of a method, you can use the super keyword

Inheritance is something that happens when you extend a class, you need to figure out the common methods you want in your class and subclass and define any alternates in your subclass.

Check this out for more info: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

you declared 'first' as a Player reference that is referencing an otherPlayer object. This is legal, but if you want otherPlayer behavior, you will have to typecat 'first': ((otherPlayer)first).solve(5)

First of all, you can't instantiate abstract classes, so you can't code newPlayer = new Player(); .

Then, it's perfectly legal to write something like ClassA a = new ClassB(); as long as ClassB is a subclass of ClassA and ClassB is not an abstract class.

In the case you have a method in ClassB that overrides a method of ClassA, which one is called depends on the dynamic type of the object that is actually the type you used for instantiate that object, this is called Polymorphism. There is lot of material about this (Object Oriented Programming and Java) on the web, but I think a great place to start at, is the book Thinking in Java by Bruce Eckel, he offers the third edition for free at his site ( http://www.mindview.net/Books/TIJ/ ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM