Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Objective-C Inheritance 
 

Objective-C enables programmer to inherit common methods and properties from other class, known as inheritance. Class from methods and properties are inherited known as Base Class and class that inherits known as Derived Class.

 

Objective-C Inheritance

                         


Objective-C enables programmer to inherit common methods and properties from other class, known as inheritance. Class from methods and properties are inherited known as Base Class and class that inherits known as Derived Class. derived class only specifies how it is different with base class and everything else is taken to be the same. Here in the figure given below Vehicle is the base class and both Car and Bike are derived classes so that these classes can use methods and properties of Vehicle class.


      


Example:

This is code of base class.

FirstClass.h                                    FirstClass.m
#import<Foundation/NSObject.h>
@interface FirstClass:NSObject {	
  }
  int num1;
  -(void)setNum1 :(int) x;
  -(int)getNum1;
@end

 

#import "FirstClass.h"

@implementation FirstClass
    -(void)setNum1 :(int) x {
        num1 = x;
        printf("num1 is : %d \n", num1);
    }
    -(int)getNum1 {
         return num1;
    }
@end

This is code of derived class.

SecondClass.h                                                  SecondClass.m
#import "FirstClass.m"
@interface SecondClass: FirstClass {
	int num2 ;
}
-(void)setNum2 :(int) y;
-(int)mul;
@end
#import "SecondClass.h"
#import "FirstClass.h"
@implementation SecondClass
-(id) init {
self = [super init];
return self;
}
-(void)setNum2 :(int) y {
num2 = y ;
printf("num2 is : %d \n", num2);
}
-(int)mul {
return num2*[self getNum1];
}
@end

This is code of main class.
main.m

#import "SecondClass.m"
#import <stdio.h>
int main() {
    FirstClass *obj1 = [[FirstClass alloc] init];
    SecondClass *obj2 = [[SecondClass alloc] init];
    [obj1 setNum1 : 10 ];
    [obj2 setNum2 : 15 ];
    printf("Multiplication Result : %d \n",[obj2 mul]);
    return 0;
}

Output:

num1 is : 10 
num2 is : 15 
Multiplication Result : 150


Download Source Code

                         

» View all related tutorials
Related Tags: iphone c programming mac application import object language io sed system ip port applications this app for phone program to

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.