Sunday, 12 July 2015

Inheritance in Swift

Inheritance in Swift

Learn Objective C from here:Click Here

Learn Swift from Here:Click Here

Learn Initializers in Objective C:Click Here

Learn IOS Application Development From here:Click Here

Learn Accessory Type in IOS:Click Here

I already describe you about the inheritance in objective c if you have not studied yet Click Here.

When use of methods,properties of one class required in another class without writing it again you can inherit the another class with One class called base class in general.
Think in your mind that when reusability of code occurs then you can use inheritance just that is the inheritance it is same in all programming languages but you need to remember the syntax.

In Inheritance you are most probably familiar with two types of  classes Base Class,Derived Class or Super and Sub Class Respectivelly.


What is Super and Sub Class?

Sub Class: Sub class is also called derived class which inherit the properties,methods of super class.Sub Class Access methods from super class and override it accordingly.Overrirding comes in polymorphism i will describe it lator.

Super Class: Super Class is also called Base class from which sub class is inherited their methods,properties. 


In Objective C you need to write @interface Student:Teacher in header file for subclassing.

In Swift you need to write Class Student:Teacher

I have used the getter and setter in below project you can read it from here:Click Here
Below is Example of Inheritance in Swift:


class Teacher {
    var TeacherName:String
    var TeacherSubject:String
    init(teachername:String,teachersubject:String)
    {
        self.TeacherName=teachername
        self.TeacherSubject=teachersubject
    }
    func printTeacherName()->Void
    {
        println("The Teacher:\(TeacherName) teaches \(TeacherSubject)")
    }
}
var teacher1=Teacher(teachername: "Suraj", teachersubject: "IphoneDevelopement")
teacher1.printTeacherName()

class Student: Teacher {
    var StudentName:String
    var Physics:Float
    var Chemistry:Float
    var Math:Float
    var TotalMarks:Float
    var Percentage:Float
        {
        get
            {
                return TotalMarks/300*100
            }
        set
        {
            self.Percentage=0
        }
    }
    init(studentname:String,physics:Float,chemistry:Float,math:Float)
    {
        self.StudentName=studentname
        self.Physics=physics
        self.Chemistry=chemistry
        self.Math=math
        TotalMarks=self.Physics+self.Chemistry+self.Math
      super.init(teachername: "Suraj", teachersubject: "IphoneDevlopment")
    }
    func printStudentNameTeacher()->Void
    {
        println("The Student:\(StudentName) is Teaches by Teacher:\(TeacherName) and got Percentage is:\(Percentage)")
    }
}
var student1=Student(studentname: "John", physics: 90, chemistry: 90, math: 85)
student1.printStudentNameTeacher()




You think why i use super.init() in above Example due to the chaining in Swift Designated and Convenience .

I hope you like my tutorials. Any query can mail me or comment here.

My next tutorials will be Chaining of initialisation in swift.

1 comment:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in IOS development, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on IOS development . We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete