Friday, March 4, 2016

Day 2 Object Oriented

OOPs concepts in Java

OOPs Basics

1. Method overloading2. Method overriding3. Exception handling in method overriding
4. Method overloading vs Overriding5. Constructors 6. Constructor overloading
7. private constructor8. Constructors in interfaces9. Constructor chaining
10. Aggregation11. Association12. OOPs basics
13. Polymorphism in java14. Types of polymorphism15. Inheritance
16. Types of inheritance17. Hybrid inheritance18. Hierarchical inheritance
19. Multilevel inheritance20. Multiple inheritance21. Encapsulation
22. Static and dynamic binding in java23. Static class24. Static methods
25. Static variables26. Static import27. Static constructor
28. Inner classes29. Abstract class and methods30. Interfaces in java
31. Abstract class vs interface32. Access modifiers33. Packages in java
34. Final Keyword35. Super keyword
Note: Above are the links of separate detailed tutorials on each topic. However if want to brush up the things for interview, below is a brief of each of the above topic which will help you re-call the things.
Object Oriented Programming is a programming method that combines:
a) Data
b) Instructions for processing that data
into a self-sufficient ‘object’ that can be used within a program or in other programs.

Advantage of Object Oriented Programming

a) Objects are modeled on real world entities.
b) This enables modeling complex systems of real world into manageable software solutions.

Programming techniques

a) Unstructured Programming (Assembly language programming)
b) Procedural Programming (Assembly language, C programming)
c) Object Oriented Programming (C++, Java, Smalltalk, C#, Objective C)
Unstructured Programming
This consists of just writing the sequence of commands or statements in the main program, which modifies the state maintained in Global Data. Example: Assembly Language programs.
Limitations of Unstructured Programming
a) The data is global and code operates on it
b) As the size of code increases, maintenance is a problem
c) Does not have independent data for processing
d) The concept of local variables did not exist
e) Reusability of code was not supported
Assembly Language: Similar to machine language, but provides names for numeric instructions present in the machine language, making it easy for the programmer.
Machine language is the language which a Central Processing Unit (CPU) of a computer understands and consists only of numbers.

2. Variables

Global Variables
a) The variables that are declared outside any function body.
b) These variables exist for the entire life-cycle of the program.
c) Global variables can be accessed from anywhere within the program.
Local Variables
a) The variables that are declared within a function body.
b) Their scope is limited to within the function body.
c) Local variables cannot be accessed outside the function body.

Object Oriented Programming

OOPs concepts, Object Class and instance
Object
Object:  is a bundle of related variables and functions (also known methods).
Objects share two characteristics: They have State and Behavior.
State: State is a well defined condition of an item. A state captures the relevant aspects of an object
Behavior: Behavior is the observable effects of an operation or event,
Examples:
eg 1:
Object: House
State: Current Location, Color, Area of House etc
Behavior: Close/Open main door.
eg 2:
Object: – Car
State: Color, Make
Behavior: Climb Uphill, Accelerate, SlowDown etc
Note: Everything a software object knows (State) and can do (Behavior) is represented by variables and methods (functions) in the object respectively.
Characteristics of Objects:
  1. Abstraction
  2. Encapsulation
  3. Message passing

Message passing

A single object by itself may not be very useful. An application contains many objects. One object interacts with another object by invoking methods (or functions) on that object. Through the interaction of objects, programmers achieve a higher order of functionality which has complex behavior.
One object invoking methods on another object is known as Message passing.
It is also referred to as Method Invocation.
OOPs concepts, Message passing

Class

A class is a prototype that defines the variables and the methods common to all objects of a certain kind. Member Functions operate upon the member variables of the class. An Object is created when a class in instantiated.
How to create an Object?
An object is created when a class is instantiated
Declaring an Object of class :
ClassName Objectname;
Object definition is done by calling the class constructor
Constructor: A special member function which will be called automatically to initialize the data member of a class whenever object is instantiated.
Memory space is allocated only when a class is instantiated i.e. when an object is created
Defining a variable :
E.g. GraduationCourse mycourse= new GraduationCourse();
Object Oriented Programming features:
Object-oriented-programming-features

Abstraction

The purpose of abstraction is to hide information that is not relevant or rather show only relevant information and to simplify it by comparing it to something similar in the real world.
Abstraction means “The process of forming of general and relevant concept from more complex scenarios”. 
Note 1: Abstraction is used to build complex systems.
Key to simplify a complex design into smaller, manageable parts which then become the components of a bigger and complex system.
The idea of hiding the complexity within a smaller/simple component of a system.
Note 2: Abstraction is not a feature of Object oriented concepts alone. Even in procedural language programming, abstraction can be achieved to a limited extent by hiding complex internals through well formed business logic and functions.

Encapsulation

Encapsulation means the localization of the information or knowledge within an object.
Encapsulation is also called as “Information Hiding”.
1) Objects encapsulate data and implementation details. To the outside world, an object is a black box that exhibits a certain behavior.
2) The behavior of this object is what which is useful for the external world or other objects.
3) An object exposes its behavior by means of methods or functions.
4) The set of functions an object exposes to other objects or external world acts as the interface of the object.
Benefits of Encapsulation
1) The functionality where in we can change the implementation code without breaking the code of others who use our code is the biggest benefit of Encapsulation.
2) Here in encapsulation we hide the implementation details behind a public programming interface. By interface, we mean the set of accessible methods our code makes available for other code to call—in other words, our code’s API.
3) By hiding implementation details, We can rework on our method code at a later point of time, each time we change out implementation this should not affect the code which has a reference to our code, as our API still remains the same
How to bring in Encapsulation
1) Make the instance variables protected.
2) Create public accessor methods and use these methods from within the calling code.
3) Use the JavaBeans naming convention of getter and setter.
Eg: getPropertyNamesetPropertyName.
Example for encapsulation
class EmployeeCount
{
   private int NoOfEmployees = 0;
   public void setNoOfEmployees (int count)
   {
       NoOfEmployees = count;
   }
   public double getNoOfEmployees () 
   {
       return NoOfEmployees;
   }
}
class Encapsulation
{
   public static void main(String args[])
   {
      System.out.println("Starting EmployeeCount...");
      EmployeeCount employeeCount = new EmployeeCount ();
      employeeCount. setNoOfEmployees (12003);
      System.out.println("NoOfEmployees = " + employeeCount. getNoOfEmployees ());
    }
}
Takeaway from above example:
The application using an Object of this class EmployeeCount will not able to get the NoOfEmployees directly.
Setting and getting the value of the field NoOfEmployees is done with the help of Getter and setter method as shown below.
  1. JAVA TRAINING IN KATHMANDU
  2. JAVA TRAINING IN LALITPUR

No comments:

Post a Comment