OOPS What Oops! (1)
ANUBHAV KUMAR
Full Stack Java Developer [ Java | Spring Boot | Microservices | Angular | Type Script | MySQL ] 550+ Problem Solved On DSA
Disclaimer: I am Preparing for my interview round so for the revision propose I write this article. May be it is helpful for someone and I welcome your suggestion as I am a learner and a learner learns from his mistake.
Let's begin with the first question.
Q1>What is Object oriented programming system(oops) ?
Solution ): Object oriented programming is type of programming that's allow us to break our problem into small unit of problem that is represented via objects and their function.
There are main 4 pillars of OOPs.
First one is Abstraction , Encapsulation , Inheritance and the forth one is Polymorphism. (Mnemonics is IPAE(IP Address is Elephant))
Q2>How OOPs is related to real world?
Solution ): We are study object oriented programming because to develop real world project that makes our life more easy and confirmable. object oriented programming help to deal with real world problem using Class, Object, Abstraction, Encapsulation, Inheritance and Polymorphism.
Example like: There is concept of "DATA HIDING" in Encapsulation. With the help of encapsulation we are able to hide sensitive data from user and we have control which data we want to show them and which one not using access specifiers.
Q3>Why to study OOPs?
Solution ): We study oops because many reason like
Q4>What is the limitation of OOPs??
Solution ): With lot of use case of OOPs there are also some drawbacks of OOPs like
that is few of the point i remember right now.
Q5>When we say that "X" language is object oriented programming language, then what does we mean by that?
Solution ): When any programming language emphasize working with objects and classes also show the characteristics of Inheritance, polymorphism, abstraction and encapsulation then we consider the language as a object oriented programming language.
领英推荐
____________________________________________________________________________________________________________________________________________________
Q6> What is Class and Object ? Gives Real world analogy of Class and Objects.
Solution ): Class is logical representation of data which is user defined data type also it is blueprint of object that contain variable and methods for example if we relate to real world example like Human being is a class. The body parts of a human being are its properties, and the actions performed by the body parts are known as functions/method.
Object is run time and physical entity also the instance of class. object allocate the memory when its created and it can be created many times as per requirement.
//example of class(we create class onces)
class Student {
???String name; //properties
???int age;
??
//method
???public void getInfo() {
???????System.out.println("The name of this Student is " + this.name);
???????System.out.println("The age of this Student is " + this.age);
???}
public class OOPS {
???public static void main(String args[]) {
???????Student s1 = new Student(); //Object 1
???????s1.name = "Anubhav";
???????s1.age = 22;
???????s1.getInfo();
?
???????Student s2 = new Student(); //Objects 2
s2.name = "Ankita";
???????s2.age = 21;
???????s2.getInfo();
???}
"this" keyword used to pass current object as parameter to another method.
Real world Analogy: If animal is the class then dog is the object, if human is the class then man is the object. A dog has legs and eyes, then eyes is the variable in the technical concept,?this is the property and the dog may run or may walk, these are methods, and the same concept we used in OOPS concept.
Q7>What is Access modifiers explain its type with example?
Solution ): As the name suggest access modifiers Java specifies the accessibility or scope of a field, method, constructor, or class. we can modify the access the of method as per our need and user requirements.
In java there are four type of access modifiers Private, Default, Protected, & Public .
class Hit{
private int tstudent= 3000;
private void slogan(){
System.out.println("we are Hitians");
}
}
public?class?Simple{??
?public?static?void?main(String?args[]){??
???Hit s1 =new?Hit();??
???System.out.println(s1.tstudent); //Compile?Time?Error??
???s1.slogan(); //Compile?Time?Error??
???}??
}
2. Default: When we don't use any modifier than its treated as default by default and the scope of default modifier is within the package.
class BaseClass
{
????void display()????? //no access modifier indicates default modifier
???????{
???????????System.out.println("BaseClass::Display with 'dafault' scope");
???????}
}
?
class Main
{
????public static void main(String args[])
???????{??
??????????//access class with default scope
??????????BaseClass obj = new BaseClass();
???
??????????obj.display();??? //access class method with default scope
???????}
}
Output: BaseClass::Display with 'dafault' scope
3.Protected: The?protected access modifier?is accessible within package and outside the package but through inheritance only.
//save?by?A.java?
package?pack;??
public?class?A{??
protected?void?msg(){System.out.println("Hello");}??
}??
//save?by?B.java??
package?mypack;??
import?pack.*;??
??
class?B?extends?A{??
??public?static?void?main(String?args[]){??
???B?obj?=?new?B();??
???obj.msg();??
??}??
}?
?
Output:Hello?
4.Public: The?public access modifier?is accessible everywhere. It has the widest scope among all other modifiers.
//save?by?A.java?
??
package?pack;??
public?class?A{??
public?void?msg(){System.out.println("Hello");}??
}?
?
//save?by?B.java??
??
package?mypack;??
import?pack.*;??
??
class?B{??
??public?static?void?main(String?args[]){??
???A?obj?=?new?A();??
???obj.msg();??
??}??
}???