-1

Lets say we have this:

class A
  {
      func printTheValueA()
         {
            print("A")
         }

  }
  class B:A
  {
    func printTheValueB()
      {
        print("")
       }
   }
class C {
var a:A = B()
a.printTheValueB()
}

As in above code is it possible to use the method of class B from the reference of class A?

Dheeraj
  • 1
  • 2

1 Answers1

0

Don't what you are trying but the code should be

open class A
  {
       fun printTheValueA()
         {
            print("A")
         }         
  }
  class B:A()
  {
       fun printTheValueB()
      {
        print("B")
       }
    }


fun main() {
    println("Hello, world!!!")
    var a:B = B()
    if(a is A)
    a.printTheValueB()

}

you can try it here

Doc
  • 10,831
  • 3
  • 39
  • 63
  • thanks for answering @Doc but i wanted to know if child can have instance of parent and access to the method. But after checking I found out that there will be compile time error. – Dheeraj May 09 '19 at 10:25