title: scala class members causing error "Class 'class' must either be declared abstract or implement member 'member' "
body: <p>I am trying to learn <strong>Scala</strong> <a href="https://www.oreilly.com/library/view/learning-scala/9781449368814/ch09.html#objects_section" rel="nofollow noreferrer">Object</a>s and <a href="https://www.scala-lang.org/old/node/132.html" rel="nofollow noreferrer">Class</a>. I come from <strong>Java</strong> which is quite similar to Scala, but not in syntax.</p>
  
  <p>In <strong>Java</strong>, I would create a class like:</p>
  
  <pre><code>public class Wallet {
      private PublicKey publicKey;
      private PrivateKey privateKey;
      private int balance = 0;
  
      public Wallet() throws NoSuchAlgorithmException {
          KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
          KeyPair keyPair = keyPairGenerator.generateKeyPair();
          privateKey = keyPair.getPrivate();
          publicKey = keyPair.getPublic();
      }
  
      public int getBalance(){
          return balance;
      }
  }
  </code></pre>
  
  <p>This defines the template of a <code>Wallet</code> object, which I can instantiate in <strong>Java</strong> as: </p>
  
  <pre><code>Wallet wallet = new Wallet();
  </code></pre>
  
  <p>This will also create a <code>PrivateKey</code> and <code>PublicKey</code> in the <code>Wallet</code> constructor and assign it to the corresponding class members. </p>
  
  <p>I understand that <strong>Scala</strong> uses a slightly difference approach, where classes are simply templates of instantiatable objects, and <code>Object</code>s are <code>Singleton</code> type objects that store information persistently. </p>
  
  <p>Also, I found that an <code>Object</code> is frequently used in a <code>Class</code> file as a <em>Companion Object</em> according to <a href="https://stackoverflow.com/questions/1888716/what-replaces-class-variables-in-scala">this post</a>.</p>
  
  <p>Using the above <code>Wallet</code> example, I am attempting to recreate this in <strong>Scala</strong>. This is what I have so far:</p>
  
  <pre><code>class Wallet {
  
    var publicKey: PublicKey
    var privateKey: PrivateKey
    var balance: Int = 0
  
    def this() {
      this
      val keyPair =  KeyPairGenerator.getInstance("SHA-256").generateKeyPair()
      privateKey = keyPair.getPrivate
      publicKey = keyPair.getPublic
    }
  
    def getBalance(): Int = {
      balance
    }
  }
  </code></pre>
  
  <p><strong>The problem:</strong></p>
  
  <blockquote>
    <p>Class 'Wallet' must either be declared abstract or implement abstract
    member 'publicKey: PublicKey' in 'com.simple.blockchain.Wallet</p>
  </blockquote>
  
  <p><em>Now this is confusing to me.</em>  </p>
  
  <p>In <strong>Java</strong>, this would normally to be a problem to have declared but uninstantiated objects as class members, so if I try adding a <em>Companion Object</em> as suggested in the earlier SO post and following their suggestion with:</p>
  
  <pre><code>object Wallet {
    var privateKey: PrivateKey
    var publicKey: PublicKey
  }
  </code></pre>
  
  <p>I get an error:</p>
  
  <blockquote>
    <p>Only classes can have declared but undefined members</p>
  </blockquote>
  
  <p><strong>TL;DR</strong></p>
  
  <p>I would like to have the <code>Wallet</code> <strong>Java</strong> code to <strong>Scala</strong> code, how could I achieve this?</p>
  
  <hr>
  
  <p><strong>UPDATE (to clarify my original question)</strong></p>
  
  <p>So, the answer do not reflect my actual question (what I had in mind). I will attempt to clarify.</p>
  
  <p>There are various constructors supported by Scala.</p>
  
  <p>Primary constructors you will find as mentioned by @Josh <a href="https://stackoverflow.com/a/54856681/4628115">here</a></p>
  
  <pre><code>class Person(name: String) {
      var personName: String = name
  }
  </code></pre>
  
  <p>and secondary constructors will be (by extending @Josh's example) such as:</p>
  
  <pre><code>def this() {
      this("Unknown Person Name")
      // additional code
  }
  </code></pre>
  
  <p>These constructors are discussed <a href="https://alvinalexander.com/scala/how-to-create-multiple-class-constructors-in-scala-alternate-constructors" rel="nofollow noreferrer">here in some detail</a>, it makes for a good read.</p>
  
  <p><strong>What I am interested in is this.</strong> </p>
  
  <p>As I have declared a member <code>privateKey</code> and <code>publicKey</code>, so I wish to declare more members in my class. Not all these members will be instantiated when the class is initialized, but will be later during the application's execution.</p>
  
  <p>This below will give more context to what I described above. I will instantiate the <code>Wallet</code>, and at a later stage call <code>wallet.generateKeys()</code>. </p>
  
  <pre><code>class Wallet {
  
    var publicKey: PublicKey
    var privateKey: PrivateKey
    var balance: Int = 0
  
    def generateKeys(): Unit = {
      val keyPair =  KeyPairGenerator.getInstance("SHA-256").generateKeyPair()
      privateKey = keyPair.getPrivate
      publicKey = keyPair.getPublic
    }
  
    def getBalance(): Int = {
      balance
    }
  }
  </code></pre>
  
  <p>This will require a variable declared, but uninstantiated during runtime, up until the point where it is needed.</p>
  
  <p>How will <strong>this</strong> be defined in Scala code.</p>