Concise Discussion on Objects & Classes (OOP)

OOP (Object Oriented Programming) is superseding fundamental concepts of Procedural Programming. Both Procedural and Object Oriented Programming are falling under Imperative Programming paradigm and the key noticeable difference in between two is that Procedural Programming is building methods that perform operations on data and in contrast OOP is building objects that could carry data as well as methods.

 


Known OOP languages like Java, Python, Ruby, Visual Basic, PHP, C++ and Delphi consists features of OOP.

 

There are 6 concepts under OOP  to learn, after which we can a get brief understanding of what actually OOP is. To begin with, sound understanding of the Objects and Classes is important.



Objects

Objects are instances/variance of Classes. These are real world objects like Dog, Cycle and Even Smartphone is an Object.

 

Class

Class is a blueprint, a prototype that contains (attributes) variables and methods (behaviors).

 

Objects & Classes

Looking at below example, Smartphone can be considered a Class. Smartphone class has attributes (Variables) like Model, Year & OS_Version and also behaviors (Methods) like charge, on and off. 

Samsung, Huawei and Apple are objects (Variants) of Smartphone class and therefore gets all the attributes and behaviors of Smart phone Class.

Shown below is a Java code sample which further explains this. Smartphone class has attributes like model, year and OS version. Also it has methods like charge, on and off.  'Samsung' is an instantiation of the Smartphone class (an object of Smartphone class) and by default obtains the attributes and behaviors of Smartphone class.

 
public class Smartphone {
    
    String model;
    int year;
    String os_version;
    
    public String charge(){
        return "is Charging \n";
    } ;
    
    public String on(){
        return "is On \n";
    } ;   
    
    public String off(){
        return "is Off \n";
    };   
    
    public static void main(String [] args) {
       
        Smartphone samsung = new Smartphone(); // object of smartphone class
       
        String messsage =
               
                "Main method is running and we have created samsung object from Smartphone Class."
               
                + "Samsung" + samsung.on()
                + "Samsung" + samsung.off()
                + "Samsung" + samsung.charge()
                
                ;
       
        System.out.println(messsage);
       
    }
    
}

 

Output is as below.

Signing off ~ Will be back with next OOP lesson > OOP Inheritance.

No comments: