OOP Concetps Required for SQA

 

 
If you are preparing for SQA interviews then you have to revise Basic OOP concepts as well. 

Encapsulation:
It binds the data and information together into a single entity to protect the data from external sources.
The whole idea behind encapsulation is to hide the implementation details from users. If a data member is private it means it can only be accessed within the same class. No outside class can access private data member (variable) of other class.
However if we setup public getter and setter methods to update (for example void setSSN(int ssn))and read (for example  int getSSN()) the private data fields, then the outside class can access those private data fields via public methods.


Class Box
{
Private int length, height; // instance variable is kept private
Public void setData (int l, int h)
//method  named setData having local variables l and h
{
length=l;  
height=h;
// instance variables
} 
Public void showData()
{ 
System.out.println("Length is=" +length);
System.out.println("Height is=" +height);
}
Public void main (string arg[])
//this is main method, every program should contain one main method, 
//first of all main method gets executed when we run program.
{ Box hl = new Box(); // “hl” is an object of class type. And new keyword is used to allocate
//memory at run time.
//“hl” is reference variable. hl.setDate( 12,5); hl.showData(); //objectName.methodName is used to call method } }

Abstraction:
Abstraction
 is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. Consider your mobile phone, you just need to know what buttons are to be pressed to send a message or make a call, What happens when you press a button, how your messages are sent, how your calls are connected is all abstracted away from the user.

Abstract class contains at least one abstract method. The Abstract method has no body.
The implementation (body of method is defined) of abstract method is provided in child class.
If any class has one Abstract method then that class has to be declared as Abstract.
Abstract class can contain both abstract and non-abstract methods.
Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user.
We cannot make object of abstract class.

Abstract class Parent
{
 	public abstract void m1();  //abstract method without body.
 	public void m2() {
 	System.out.println("this is non-abstract method");
}
}
Class Child extends Parent
{
 	void m1() {
 	system.out.println("this is definition of abstract method m1)
}
}
 Class Test
{
 public static void main (String args[])
 {
 	Child C = new Child();
 	C.m1();
 	C.m2(); //this object will call m2 method and find it in child class,but
//m2 method is not present in child class so it will look m2 in its parent class.
} }

Constructor:
Constructor is a member function of class.
It has same name as class. When we make an object then this function is called automatically.It has no return type, Compiler called it when object is created. We do not call it.A constructor in which we pass parameters is called parameterized constructor. Parameterized constructor is used to access data members of class through object.

Class Box
{
Box() // this is constructor; a function which has no return type and name 
//of function is same as name of class.
{ System.out.println("This is called when object is made of this class") } Public static void main (string args []) { Box obj = new Box(); // this object will call constructor function automatically. } }

Example of parameterized Constructor:

Class Box
{
private int a , b; // data members of class
Box(int m , int n) // this is parameterized constructor.
{
a = m;
b = n;
}
void showData()
{
System.out.println(a);
System.out.println(b);
}
Public static void main (string args [])
{
Box obj = new Box(12, 5); // this object will call constructor function 
//automatically.
obj.showData(); this will print values of a and b i.e 12 and 5 respectively. } }

Inheritance:
The process by which one class acquires the properties (
instance variables) and functionalities of another class is called Inheritance, it is basically
Child, Parent relationhip. It provides reusability of data.

Inheritance: 
Child, Parent relation is called Inheritance. It provides reusability of data. 
Class Person 
{
 	private String name;
 	Private int age;

public void setName(string n)
{
 	name=n;
}

public void getName()
{
 	return(name);
}
Public void setAge(int a)
{
 	a=age;
}
public void getAge()
{
 	return(age);
}
}//class person brackets closed
Class Student extends Person
{
 	private int roll;
Public void setRoll(int r)
{
 	roll=r;	
}

public void getRoll()
{
 	return(roll)
}
}
Public Class Example
{
public static void main (string args[])
{
Student s = new Student();
s.setName('waqas');
string nm = s.getName();
system.out.println("Name of  student is:" +nm);
s.setAge(20);
system.out.println("Age of Student is:"+s.getAge() );
s.setRoll(15);
int rl= s.getRol();
System.out.println("Roll number is:" + rl );
}
}

 

Method Overloading:
Defining different versions of same method is called method overloading.
These methods must have same name but different arguments. Complier distinguishes methods on the basis of their arguments. Both methods can be in parent, or both in child or one in parent and one in child.

Class Hello
{
public void fun(int a)
{
 	system.out.println("1st method");
}
public void fun(int a , int b)
{
 	system.out.println("2nd method");
}//both methods have same name but different parameters, 
//this is method overloading
public static void main(string args[]) { Hello obj = new Hello(); obj.fun(3); obj.fun(3,6); } }

Method Overriding:
Defining two methods with same signatures i.e same method name and same parameters.
One version should be in parent class and the other should be in child class.

Class A
{
public void fun(int a)
{
system.out.println("this is Class A")
}
}
Class B extends A
{
public void fun(int a)
{
system.out.println("this is Class B")
}
}

Class Test
{
public static void main(String args[])
{
B obj = new B();
obj.fun(3);
}// output of this function would be “this is Class B”.
}//We use method overriding where we use same method signature 
//but different implementation.

 

Interface:
Interface looks like a class but it is not a class it is a set of properties. An interface can have methods and variables just like the class but all methods declared in interface are by default abstract (only method signature, no body) and no need to declare them abstract.

interface MyInterface
{
   /* compiler will treat them as: 
    * public abstract void method1();
    * public abstract void method2();
    */
   public void method1();
   public void method2();
}
class Demo implements MyInterface
{
   /* This class must have to implement both the abstract methods
    * else you will get compilation error
    */
   public void method1()
   {
	System.out.println("implementation of method1");
   }
   public void method2()
   {
	System.out.println("implementation of method2");
   }
   public static void main(String arg[])
   {
	MyInterface obj = new Demo();//object of class Demo reference to Interface 
	obj.method1();
   }
}//output: implementation of mehtod1


Polymorphism:
Polymorphism is one of the OOP's features that allows us to perform a single action in different ways. For example, let’s say we have a class Animal that has a method sound(). Since this is a generic class so we can’t give it a implementation like: Roar, Meow, Oink etc. We had to give a generic message.

Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementations. As we will see in the below example that we have defined the method sound() and have the multiple implementations of it in the different-2 sub classes.
Which sound() method will be called is determined at runtime so the example given below is a runtime polymorphism example.

public class Animal{
   public void sound(){
      System.out.println("Animal is making a sound");   
   }
}
public class Cat extends Animal{
    //Override
    public void sound(){
        System.out.println("Meow");
    }
    }

class Horse extends Animal{
    //Override
    public void sound(){
        System.out.println("Neigh");
    }
    public static void main(String args[]){
    	Animal obj = new Horse();
    	obj.sound(); // output=Neigh

Animal obj1 = new Cat();
    	Obj1.sound(); //output=Meow

    }
}

Post a Comment

0 Comments