Pages

Thursday, October 20, 2016

OOP , inheritance , abstract , encapsulation and polymorphism

OOP , inheritance , abstract , encapsulation and polymorphism




What does Object oriented progarmming language mean ?


All the languages which include abstract , encapsulation , 
inheritance and polimorphism is called Object oriented progrmming language



abstract class


abstract means to epitmise or to take from so In oop when you want to make a class abstract this cannot be instantiate in any of the class. 
While you can use it and access it using the subclasses of it (In other words all the classes which is inhereted from the super abstract class can be instantiate with
the reference variable of that super class ). 

abstract methods 

when  an abstract method is created  in a super class all the inherited class (subclass) of that class must use  this method. 

In java the rules for abstract method

1. Abstract should be declare in abstract class.
2. must use the keyword abstract 
3. no body could be define with abstract method ie
    
  abstract void student() ; 

4. Inhered class must have similar specifier as it has in
 the super class 
 ie (private , public , protected)

5. All the sub classes have to have this method but being defined
  ie

 public void student(){ ... }


implementation of abstract class in java



suppose we have a class student and we want that 

"to every student have their name put for admission "

 this implementation could be done easily with abstraction 

ie 


abstract class student{


public abstract void student_name(); 

}

class student1 extends student{


//this is abstract method of student classs
public void student_name(){

System.out.println("yasir") ; 

}
}


//second student 

class student2 extends student{



//this is abstract method of student classs

public void student_name(){

System.out.println("Rafiq") ; 

}
}


//third student
class student3 extends student{



//this is abstract method of student classs

public void student_name(){

System.out.println("Rezwaan") ; 
}
}



//main class 

class main{

public static void main(String[]args){

student std1  = new student1() ; 
student std2  = new student2() ; 
student std3  = new student3() ; 


}

}


out put 


yasir
Rafiq
Rezwaan










Encapsulation 




encapsulation means to get or set private member of another class is called encapsulation

 Ie

 suppose we have a student class which include some data about a student


class student{

private String name ; 

public void init(){

name ="Mr abrar Khan" ; 

}

//method to access name variable out side this class
public String getName(){

return name ; 

}

//method to change the value of name from outside classes
public void setName(String name){

this.name= name ; 

}

}

and we have another class From where we want to change the student name or to access the name of the student 

class accessStudent{


public static void main(String[] args){
  
//create object for the student class 

  student std = new student() ; 

String accessName = std.getName() ; 


System.out.println(accessName); 

String change_Name = std.setName("Kamal Cr") ;

System.out.println(change_Name) ; 

}


}


Output :

Mr abrar khan
Kamal Cr



Inheritance 




Inheritance means to get public members of super a class ...

 ie 

suppose we have character ( good / bad )  common in all the student so to implement this using inheritance

class student { 

void character(){

 System.out.println("Good");
}

}


And now if you want to be the character method use for all student so instead of creating this method in all the student classes you just have to inheret this class
in all the student classes 


In order to inherit a class you have to put keyword ( extends ) in java 

ie 




class student1 extends student{



}
class student2 extends student{



}

class student3 extends student{



}


class main{

public static void main(String[]args){

 student1 std1 = new student1(); 
 std1.character() ; 
  student1 std2 = new student2();  
   std2.character() ; 

   student1 std3 = new student3(); 
 std3.character() ; 


}

}







So the out put will be 



good 
good 
good





polymorphism 




In polymorphism we can assign  all the subclass to one super class object (also called Reference variable) 


ie suppose we have some classes inhereted from a super class student 

class student1 extends student{

 public void student(){
    
System.out.println("Jamil Khan") ; 
 }

}

class student2 extends student{

 public void student(){
    
System.out.println("Salman") ; 
 }

}

class student3 extends student{

 public void student(){
    
System.out.println("Iqbal Hussain") ; 
 }

}


class student{


public void student(){

System.out.println("Kamal hassan") ; 
}


}

class main{

public static void main(String[]args){
// here is the point you can assign any subclass to super class object

student std[] = new std[3] ; 

std[1] = new student1(); 
std[2] = new student2() ; 
std[3] = new student3() ; 
for(int i = 0 ; i<std.length ; i++){
  std[i].student(); 
}


}

}




So long story short in polymorphism you don't have to create many object for sub classes  




Monday, October 17, 2016

Speech (my parent)

Honorable Principle ,
 Respected Teachers and my dear School fellows!

 Assalm-O -Allaikum (w.wabaraktuhu)


   I am supposed to be stand here today and 
   talk to you about parents.

   I am unsure of how i am supposed to start talking
   about one of the most important things in my life.

   But i will do my best. I thought to myself
   
    what is parent ? 

    They are more than any one will ever be able to speak.
    I can't began to term what a parent is or what they are
    suppose to be. 
   
    But i can tell you that all of them have shown us love
    and caring to bring us forth in this world .

     Whether young , old , dying, famous , rich or poor.
     Our love to you as parents is unconditional. 




    Respected Audience ! 


   I realized that from the past, Unfortunately , friends come 
   and friends go, Friends change , interests change or ideas
    change . 

   And unfortunately some friends move far away. 

   Our relationship and friendships change with life.

   But the love our parents have for us does not change. 
  No matter what we do , where we go , or what we think our        parents posses an undying love for their children.

  They have brought us up in a world full of obstacles . They passed   us from these obstacles. 

  



  Dear fellows ! 

   No words could  ever repay them for all of the love they have    shown us.

  But I would like to let every parent know that what they do for us is really so appreciated.

   Mom and Dad I love you . 


 Wassalam.


 Thank you !

Linked List , Types of linked list and why we use linked list

Linked List


 Definition : It is that type of lenear data structure where    nodes/ item are connected with one another through pointer. Diagrammatically the connection betwwen two nodes is represented by arrows.

 For example 

 the first nodes of the linked list is called head and the last node of the linked list is called 'tail' .

 Types of Linked List :

  There are three types of Linked List.
  
  1. one way linked list: 
  
  It is a linked list where two adjacent nodes are connected by   one pointer ( Represented by single headed arrow).
  
   
   For example 
  

  in one way linked list each nodes is divided into two parts 
  
  a .(info part) 
  b.(Address part)
  
  The info parts contain the actual data/information 
  and the address part of a node contain the address of the next node .
  
  Note :  the address part of the last node does not contain      any address it contains null address
  
  

2 Circular linked list :

 It is a linked list where the last node is connected back to the first node.

The address part of the last node contain the address of the first node.

For example ....




Multiple Linked List : 

   It is that type of linked list where nodes are connected by more than one pointer /Where two adjacent nodes  is connected by two pointer.
 
For example:



 
Here each nodes has three part 
 
 
1 info part : This part contain the actual data/information 

2 left part :this part contain the address of next node

3 Right part :   This part contain the address                  of succeeding (previous) node. 
 
 
 Why we use Linked List
 
 The most important application of linked list is the creation 
  of file on secondary storage device.




Writter/publisher : Shafiq from university of Abdul wali khan Mardan timergara campus