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  




No comments:

Post a Comment