Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts

Real Life Examples of OOPs Based Topics



Real life example of "Abstraction"

In General words, Abstraction is Just Hiding the complex things behind a particular Procedure to make the procedure look simple.
Example: Monitor ON/OFF::--The user doesn't need to know much about all the chips functioning that happens when Monitor is switched ON or OFF..All he needs to know is On Function ON-Monitor is On and on function OFF-Monitor is off...

Or Better Look for a car--Everyone Knows that There's a special Gear machine Which changes the gear, nobody bother to know what all functionality undergoes for a gear to change.. So, That's abstraction(avoiding unwanted implementations to prevent Complexity).
So,If a developer provides a good abstraction, users won't be tempted to peek at the object's internal mechanisms.
Abstraction is achieved by making class abstract having one or more methods abstract. Which is nothing but essential characteristic which should be implemented by the class extending it. e.g.when you inventing/designing a car you define a characteristics like car should have 4 doors, break, steering wheel etc… so anyone uses this design should include this characteristics. Implementation is not the head each of abstraction. It will just define characteristics which should be included.


Real life example of "Encapsulation"

Encapsulation is restricting a user to follow a particular procedure to access control of a particular process. It Just provides safety and ensures system robustness.
Example:We can consider The HR in a company as a person that works on the principle of Encapsulation.i.e. we cannot talk to other departments directly we need to communicate them through HR. This ensures security and better maintenance of company's records.

Together we can take example of a UNDER CONSTRUCTION BUILDING..where we can say that things like 'no. of managers' required,Types of Materials,No of workers etc as abstraction as they need to there in every Building Construction.
But, at the same time, Inclusion of every such field into a CONTRACTOR which acts as a mediator between the workers and the Building-Investor can be looked upon as Encapsulation. As, It hides all the above properties into one Entity.
Hence If you would have understood till now you can say that abstraction is just a subset of ENCAPSULATION.i.e.Every entity that performs abstraction is encapsulated internally but every thing that shows encapsulation need not be abstraction always.
e.g. .ToString() Method defined in almost every class is implementation of Abstraction because We don't the functionaltiy Within,all we care is that it changes almost everything to string.And as it assembles a s a unit,it is encapsulated too..But,The private members that we hide and access through Properties is an example of encapsulation only as it is done basically keeping data security in mindd..!!
Hope This answers your Question..!!

Real life example of "Polymorphism"

Polymorphism is one in many forms. That’s it. We can see lots of examples in real time. If you think about a Dog, A Dog is an Animal. A Dog can be a pet. So a Dog can be in
many forms. The Dog is Animal type and it can be another type of Pet. 
Apart from types, behaviors can also take part of Polymorphism. For Example, Animals can swim. A Dog can swim, A Monkey also can swim. Dog and Monkey has their own way of swimming. Here, the swimming behavior is in many forms. A monkey can walk with two legs and also with four legs. Here, walking behaviour is in many forms. These are the examples for polymorphism in real world.

Let’s see how Polymorphism works in Java. Polymorphism allows you define a Super type and have multiple subtype implementations. There Are Two Types of Polymorphism in Java. One is compile time Polymorphism and it is sometimes referred as static Polymorphism and the other one is Runtime Polymorphism and it is sometimes referred as dynamic Polymorphism
 
Real life example of "Class and Object"

Object Oriented programming is about creating programs using as building blocks, "things" that exists in the real world, these real world things are called objects, hence object oriented
For instance, if you're creating a Address Book program, you may define the following objects:
person, address, phone
Among many, many others. Those would be real life objects, and you describe your program in terms of these abstractions.
With that in mind you can start describing some concepts.
Class is used to define the characteristics an objects will have. A class is used only as a template, or a blueprint. For instance, for all the persons in your address book, you may say they all will have:
Person:
   - name
   - last name
   - phone number
   - address
Etc.
An address may have:
 Address:
    - street
    - number
    - city
    - zip code
    - country
And so on. As you can notice, a class me be defined in terms of other classes, for instance, in this context, a person has one address.
An Object is a particular instance of a given class. When you add an entry to your address book, you create an object and fill in the attributes.
 onePerson  ofType Person is ( 
     - name = "Oscar"
     - last name = "Reyes"
     - phone number = "56 58 11 11"
     - address = anAddress ofType Address (
                     - street = "Tecolotes"
                     - number = 32
                     - city   = "D.F."
                     - zip code = 23423
                     - country = "Mexico"
                 )
  )
So, this object a class instance with data. Other entry in the address book are other objects with different data.
That shows the difference between them.



class and interface Based Interview Questions



1. What's the difference between an interface and an abstract class? 
An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.

2. Can an inner class declared inside of a method access local variables of this method? 
Yes, it is possible if the variables are declared as final.

3. You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces? 
Sometimes. But your class may be a descendent of another class and in this case the interface is your only option because Java does not support multiple inheritance.

4. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it? 
You do not need to specify any access level, and Java will use a default package access level. A class with default access will be accessible only to other classes that are declared in the same directory/package.

5. When you declare a method as abstract method ? 
We declare a method as abstract, When we want child class to implement the behavior of the method.

6. Can I call a abstract method from a non abstract method ? 
Yes, We can call a abstract method from a Non abstract method in a Java abstract class

7. What is the difference between an Abstract class and Interface in Java ? or can you explain when you use Abstract classes ? 
Abstract classes let you define some behavior while forcing your subclasses to provide the rest. These abstract classes will provide the basic funcationality of your application, child class which inherit this class will provide the funtionality of the abstract methods in abstract class.

Whereas, An Interface can only declare constants and instance methods, but cannot implement any default behavior.

If you want your class to extend some other class but at the same time re-use some features outlined in a parent class/interface - Interfaces are your only option because Java does not allow multiple inheritance and once you extend an abstract class, you cannot extend any other class. But, if you implement an interface, you are free to extend any other concrete class as per your wish.

Also, Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast.

8. What are different types of inner classes ? 
Inner classes nest within other classes. A normal class is a direct member of a package. Inner classes are of four types

1. Static member classes
2. Member classes
3. Local classes
4. Anonymous classes

9. What are the field/method access levels (specifiers) and class access levels ? 
Each field and method has an access level corresponding to it:

private: accessible only in this class
package: accessible only in this package
protected: accessible only in this package and in all subclasses of this class
public: accessible everywhere this class is available

Similarly, each class has one of two possible access levels:

package: class objects can only be declared and manipulated by code in this package
public: class objects can be declared and manipulated by code in any package

10. What modifiers may be used with an inner class that is a member of an outer class? 
A non-local inner class may be declared as public, protected, private, static, final, or abstract.

11. Can an anonymous class be declared as implementing an interface and extending a class? 
An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.

12. What must a class do to implement an interface? 
It must provide implementation to all of the methods in the interface and identify the interface in its implements clause in the class declaration line of code.

13. What is the difference between a static and a non-static inner class? 
A non-static inner class may have object instances that are associated with instances of the class's outer class. A static inner class does not have any object instances.

14. When can an object reference be cast to an interface reference? 
An object reference be cast to an interface reference when the object implements the referenced interface.

15. If a class is declared without any access modifiers, where may the class be accessed?
A class that is declared without any access modifiers is said to have default or package level access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.

16. Which class should you use to obtain design information about an object? 
The Class class is used to obtain information about an object's design.

17. What modifiers may be used with an interface declaration? 
An interface may be declared as public or abstract.

18. Is a class a subclass of itself? 
Yes, a class is a subclass of itself.

19. What modifiers can be used with a local inner class?
A local inner class may be final or abstract.

20. Can an abstract class be final? 
An abstract class may not be declared as final. Abstract and Final are two keywords that carry totally opposite meanings and they cannot be used together.

21. What is the difference between a public and a non-public class?
A public class may be accessed outside of its package. A non-public class may not be accessed outside of its package.

22. What modifiers may be used with a top-level class? 
A top-level class may be public, abstract, or final.

23. What are the Object and Class classes used for? 
The Object class is the highest-level class in the Java class hierarchy. The Class class is used to represent the classes and interfaces that are loaded by a Java program.

24. Can you make an instance of abstract class 
No you cannot create an instance of abstract class. If you use new keyword to instantiate an abstract class, you will get a compilation error.

25. Describe what happens when an object is created in Java 
Several things happen in a particular order to ensure the object is created properly:

1. Memory is allocated from heap to hold all instance variables and implementation-specific data of the
object and its superclasses. Implemenation-specific data includes pointers to class and method data.

2. The instance variables of the objects are initialized to their default values.

3. The constructor for the most derived class is invoked. The first thing a constructor does is call the
consctructor for its superclasses. This process continues until the constrcutor for java.lang.Object is called,
as java.lang.Object is the base class for all objects in java.

4. Before the body of the constructor is executed, all instance variable initializers and initialization blocks are executed. Then the body of the constructor is executed. Thus, the constructor for the base class completes first and constructor for the most derived class completes last.

26. What is the purpose of System Class 
The purpose of the system class is to provide the access to the System reources

27. What is instanceOf operator used for 
It is used to check if an object can be cast into a specific type without throwing Class cast exception

28. Why we should not have instance variable in an interface? 
Since all data fields and methods in an Interface are public by default, when we implement that interface in our class, we have public members in our class and this class will expose these data members and this is violation of encapsulation as now the data is not secure

29. What is a singleton class 
A singleton is an object that cannot be instantiated more than once. The restriction on the singleton is that there can be only one instance of a singleton created by the Java Virtual Machine (JVM) - by prevent direct instantiation we can ensure that developers don't create a second copy. We accomplish this by declaring the constructor private and having a public static instance variable of the class's type that can be accessed using a getInstance() method in the class.

30. Can an abstract class have final method 
Yes, you can have a final method in an Abstract class.

31. Can a final class have an abstract method 
No, a Final class cannot have an Abstract method.

32. When does the compiler insist that the class must be abstract 
The compiler insists that your class be made abstract under the following circumstances:

1. If one or more methods of the class are abstract.
2. If class inherits one or more abstract methods from the parent abstract class and no implementation is provided for that method
3. If class implements an interface and provides no implementation for some methods

33. How is abstract class different from final class 
Abstract class must be subclassed and an implementation has to be provided by the child class whereas final class cannot be subclassed

34. What is an inner class 
An inner class is same as any other class, just that, is declared inside some other class

35. How will you reference the inner class 
To reference an inner class you will have to use the following syntax: OuterClass$InnerClass

36. Can objects that are instances of inner class access the members of the outer class 
Yes they can access the members of the outer class

37. Can inner classes be static 
Yes inner classes can be static, but they cannot access the non static data of the outer classes, though they can access the static data.

38. Can an inner class be defined inside a method 
Yes it can be defined inside a method and it can access data of the enclosing methods or a formal parameter if it is final

39. What is an anonymous class 
Some classes defined inside a method do not need a name, such classes are called anonymous classes.

40. What are access modifiers 
These public, protected and private, these can be applied to class, variables, constructors and methods. But if you don't specify an access modifier then it is considered as Friendly. They determine the accessibility or visibility of the entities to which they are applied.

41. Can protected or friendly features be accessed from different packages 
No when features are friendly or protected they can be accessed from all the classes in that package but not from classes in another package.

42. How can you access protected features from another package 
You can access protected features from other classes by subclassing the that class in another package, but this cannot be done for friendly features.