Categories
Programming & Dev

Understanding OOPs: The second step towards Software Dev

From this article onwards, all the learning in the series of “Understanding OOPs” will be carried out by implementing and learning everything through the codes.

Constructor

Whenever an object of a class is initialized, then a legal initial value has to be given to member variables which are done by the constructor. A constructor is a method/function having the same name as that of a class without any return type. It can be of the same name but different signatures leading to the constructor overloading concept. In the below code we have added comments to make you understand things more clear that how everything is adjusted as an OOP body.

import java.util.*;

 class television{

  private String name;   //made private to implement data hiding concept 
  private int channel;
  private int volume;
  
  public void changech(int ch){         //setter methods
      channel=ch;
  }

   public void changevol(int vol){
       volume=vol;
   }
   public void tvname(String n){
       name=n;
   }

    public television()    //constructor->basic one
     {

    channel=1;
    volume=30;
    name="Default-concept";
  }
  public television(int c, int v , String n) {  //parameterized constructor
    channel=c;
    volume=v;
    name=n;
}

  public void getinfo(){                  //getter method
      System.out.println("Channel:"+channel);
      System.out.println("volume:"+volume);
      System.out.println("company:"+name);
  }

 }

 class constructor{
    static Scanner in=new Scanner(System.in);

    public static void main(String[] args) {
        
        System.out.println("enter channel:");
        int ch=in.nextInt();
        System.out.println("set vol to:");
        int v=in.nextInt();
        System.out.println("enter the company name to be printed:");
        String s=in.next();
        System.out.println();
        television t = new television();
        t.getinfo();   //on using non parameter constructor
        System.out.println();
        television c=new television(100,60,"Sony");
        c.getinfo();     // on using parameterized constructor
        System.out.println();
        t.changech(ch);
        t.changevol(v);
        t.tvname(s);
        t.getinfo();  // on using methods/member-function

    }

 }
OUTPUT:
oops

Inheritance

 It is a property in which one class inherits all the functions and features from the primary class. Basically, this thing is implemented to upgrade one object from its previous one. To understand it more clear, we are taking the example of a mobile phone concept, which is at first a single phone concept was introduced in which only screen size, processor, ram and storage was first introduced. After this, a new derived category of the phone came with the name of smartphone in which some extra features such as camera quality, operating system, touchscreen and network type were added. In the below class we have implemented both the things and also explaining about constructors also.

import java.util.*;

class phone{              // base class

    private String screensize;     // properties or member variables
    private String processor;
    private int ram;
    private int storage;

    public phone(){          // basic constructor

        screensize="4 inch";
        processor="snapdragon";
        ram=2;
        storage=16;

    }

    public phone(String s, String p,int r,int st){     // parameterized constructor 
        screensize=s;
        processor=p;
        ram=r;
        storage=st;
    }

    public void getinfo(){                // method or function to get all the info

        System.out.println("screensize="+screensize);
        System.out.println("Processor="+processor);
        System.out.println("ram="+ram);
        System.out.println("storage="+storage);

    }

}

class smartphone extends phone{            // class smartphone inheriting the feature of phone 

    private String os;
    private String camera;           
    private String networkType;

    public smartphone(){
        os="shell";
        camera="5MP";
        networkType="4g";
    }
    public smartphone(String a , String b , String c, String d , String e , int f,int g){
        super(d,e,f,g);
        os=a;
        camera=b;
        networkType=c;

    }

    public void info(){
        System.out.println();
        super.getinfo();
        System.out.println("operating system="+os);
        System.out.println("camera="+camera);
        System.out.println("network type="+networkType);
    }

}

class inheritance{

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        System.out.println("enter the numbers of smartphones:");
        int n=in.nextInt();
        smartphone a[]=new smartphone[n];
        a[0]=new smartphone("ios","12MP","4g","4'8inch","A11",4,64);
        a[1]=new smartphone("android","24MP","4g","6inch","850s",6,64);
        a[2]=new smartphone("android","64MP","5g","6inch","855s",8,128);
       
        for(int i=0;i<a.length;i++)
{
                  a[i].info();

}

        
    }

}
OUTPUT:
oops

This and Super keyword

“this” is a keyword used when we are initialising value to a member variable in a constructor whose arguments have the same name. For example:

class rectangle {
int length;
int breadth;

 public rectangle (int length , int breadth){
this.length=length;
this.breadth=breadth;
}

In the above case, the newly declared variables length and breadth in the constructor arguments have the same name as the member variables. So, in order to avoid compilation error and also confusion, we use “this.member_variable” to take the member variable of the class in the account for a particular task.

Talking about the super keyword, the super keyboard is generally used to call the member variables, functions or constructor of the parent class(super-class) in child class.

some of the uses are:
super.x;          ---> calls parent class instance variable
super.function_name(); ---> calls parent class method/func 
super();       ---> calls parent class instance constructor 

Things learnt so far in OOPs

So now, we will compile all the concepts we have learnt so far and will implement them in a long program based on laptops.

oops
oops

OUTPUT:

oops

If you have any doubts you can ask the questions directly by commenting down below. Also, don’t forget to read our 1st article on the OOPs to understand it completely: https://www.meusec.com/dev/software-dev-understanding-oops-concept/

Get the latest tech news and updatesethical hacking tutorials and cybersecurity tips and tricks. Check out MeuSec for more.

Sometimes we include links to online retail stores and/or online campaigns. If you click on one and make a purchase we may receive a small commission.

Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *