Showing posts with label final classes. Show all posts
Showing posts with label final classes. Show all posts

Thursday, 4 January 2018

Kotlin: Inheritance

Inheritance is the concept of re usability. Object of one class can get the properties and methods of object of another class by using inheritance.

By using inheritance, you can create new class by extending the existing class. The new class is called derived class and the existing class is called base class.



Example
open class BaseClass{

}

class DerivedClass : BaseClass{

}


By default, all the classes in kotlin are final. To make these class inheritable, you should use the keyword ‘open’.


Let’s see inheritance feature by an example. In every organization, there are two kinds of employees.
a.   Permanent Employees
b.   Contract Employees

Both the permanent and contract employees share properties like firstName, lastName and id. Where as contract employees has separate payroll company. We can place these common properties (firstName, lastName and id) in separate class Employee and make the classes PermanentEmployee and ContractEmployee inherit these properties from the common base class Employee.

open class Employee {

}

class PermanentEmployee : Employee {

}

class ContractEmployee : Employee {

}

Find the below working application.


Inheritance.kt
open class Employee {
 var firstName: String = ""
 var lastName: String = ""
 var id: String = ""

 constructor(firstName: String, lastName: String, id: String) {
  this.firstName = firstName
  this.lastName = lastName
  this.id = id
 }

 open fun getEmployee(): String {
  return "firstName : $firstName, lastName: $lastName, id: $id"
 }

}

class PermanentEmployee : Employee {
 var healthInsuranceNumber = ""
 var salary: Double = 0.0

 constructor(firstName: String, lastName: String, id: String, healtInsuranceNumber: String, salary: Double) : super(firstName, lastName, id) {
  this.healthInsuranceNumber = healtInsuranceNumber
  this.salary = salary
 }

 override fun getEmployee(): String {
  return super.getEmployee() + ", healthInsuranceNumber: $healthInsuranceNumber, salary: $salary"
 }

}

class ContractEmployee : Employee {
 var payRollCompany = "ABC"

 constructor(firstName: String, lastName: String, id: String, payRollCompany: String) : super(firstName, lastName, id) {
  this.payRollCompany = payRollCompany
 }

 override fun getEmployee(): String {
  return super.getEmployee() + ", payRollCompany: $payRollCompany"
 }
}


fun main(args: Array<String>) {
 var emp = PermanentEmployee("krishna", "Gurram", "123", "health3425", 56789.01)

 println(emp.getEmployee())
}


Output
firstName : krishna, lastName: Gurram, id: 123, healthInsuranceNumber: health3425, salary: 56789.01

constructor(firstName: String, lastName: String, id: String, healtInsuranceNumber: String, salary: Double) : super(firstName, lastName, id)
If super class don’t have default constructor, then all the sub class constructors must call the super class constructor explicitly.

By default, all the functions and classes are not inheritable (final), to make them inheritable, you should use the keyword ‘open’.

open class Employee {

         open fun getEmployee(): String {
                 return "firstName : $firstName, lastName: $lastName, id: $id"
         }

}

To override a function of super class, you should use override keyword.

class PermanentEmployee : Employee {

         override fun getEmployee(): String {
                 return super.getEmployee() + ", healthInsuranceNumber: $healthInsuranceNumber, salary: $salary"
         }

}

Important Points
a.   If the class has a primary constructor, the base type can (and must) be initialized right there, using the parameters of the primary constructor.


b.   If the class has no primary constructor, then each secondary constructor has to initialize the base type using the super keyword, or to delegate to another constructor which does that. Note that in this case different secondary constructors can call different constructors of the base type:


Previous                                                 Next                                                 Home

Friday, 21 February 2014

final Classes and Methods

final methods
If a method declared as final, then it can't be overridden by sub class.

Example
class SuperClass{
 final void print(){
 }
}

class SubClass extends SuperClass{
 void print(){
 }
} 

SubClass trying to override the method print() which is final. So when trying to compile the program, SubClass, then compiler throws the below error.
 SubClass.java:2: error: print() in SubClass cannot override print() in SuperClass
    void print(){
    ^
    overridden method is final
    1 error

Same is applicable to static classes also.
class SuperClass{
 final static void print(){
 }
}
 

class SubClass extends SuperClass{
 static void print(){
 }
}

When you tries to compile the SubClass.java, compiler throws the below error.
    SubClass.java:2: error: print() in SubClass cannot override print() in SuperClass
    static void print(){
    ^
    overridden method is static,final
    1 error

final classes
Any class which is declared as final, can't be extended. Try to extend a final class causes a compiler error.

Example
final class SuperClass{

}

class SubClass extends SuperClass{

}


When you tries to compile the SubClass.java, compiler throws the below error.

SubClass.java:1: error: cannot inherit from final SuperClass
    class SubClass extends SuperClass{
    ^
    1 error

Some Points to Remember

1. Both the private and final methods can't be overridden.
Example
class SuperClass{ 
 private void print(){ 
  System.out.println("I am super class"); 
 } 
}
   
class SubClass extends SuperClass{
 void print(){
  System.out.println("I am sub class");
 }

 public static void main(String args[]){
  SuperClass obj = new SubClass();
  obj.print();
 }
}


When you tries to compile the SubClass.java, compiler throws the below error.

 SubClass.java:8: error: print() has private access in SuperClass
            obj.print();
            ^
            1 error
         
2. Can I make private method as final ?
Since private already implies that a subclass may not override a method, declaring a private method to be final is redundant. It won't cause any problem to your program, Program compiles fine.


Final Variables                                                 Abstract methods and Abstract classes                                                 Home

final keyword

final key word is used to define

Java support the creation of constants using final keyword.

super keyword                                                 final variables                                                 Home