Pages

Saturday, September 17, 2016

Java inheritance

 



Inheritance means to access other classes members...

how to inherit a class.


In java to inherit a class a word extends is used to creat a sub class for a super class..


Super class


Also called parent class.or base class..

A class from which other class/classes is derived..

Or

A class which members are used by another classes.is know as super class.


Sub class


Also called child class or derived class.

A class which use the members of a parent class is called derived class
to creare a derived class the following syntax are used.


class class_name extends
base_class {
};


Where

Class: is a keyword to identify this is a class


class_name: this is the name of the derived class
Or inother words the name of the class.

extends: is a keyword used to identify this class is derived..and an inherited class.

base_class: represent the parent.which members will be used / inherited in this class.


Practical example of extends...


A class student_print  is inherited from a parent class student

class student {

public int student_id;
public String name;

};

class student_print extends student{
student_id=30;// student  id is inherited
name="Umair ahmad";  /* name is inherited */

};



class main {
//main method
public static void main (String []args){

student std = new student();

System.out.println (std.student_id);/*student id is printed*/
System.out.println (std.name);/*student name is printed*/

}
};

No comments:

Post a Comment