An introduction to Java
Object Object is a computer representation of some real-worldthing (i.e person, place) or event. Objects can have both a...
Class A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe thedetai...
Encapsulation Encapsulation (or information hiding) is a principle, about hidingthe details of the implementation of the ...
Association Association is a (*a*) relationship between two classes,where one class use another. It allows one object in...
Aggregation Aggregation is a special type of an association. Aggregation is the (*the*) relationship between twoclasses....
Composition Composition is recognized as a special type of anAggregation. In the composite relationship in-between say o...
Abstraction Abstraction is an emphasis on the idea, qualities andproperties rather than the particulars (a suppression of...
Generalization Generalization reduces the complexity by replacingmultiple entities which perform similar functions with a...
Specialization It is the process of defining a new object based on a(typically) more narrow definition of an existingobje...
Abstract Class Abstract class cannot be instantiated. It can only be used as a super-class for other classes that extend...
Interface Interface separates the implementation and defines thestructure. It is useful in cases where you need to chang...
Interface versus Abstract Interface has no implementation, but they have to be implemented. Abstract class’s methods can...
Polymorphism Polymorphism is the ability to create a variable, a function,or an object that has more than one form. It i...
Cohesion and Coupling Cohesion is an inter-class measure of how well-structured andclosely-related the functionality is i...
Method Overloading and Overriding Polymorphism is only concerned with the applicationof specific implementations to an in...
Inheritance It is a technique to compartmentalize and reuse codeby creating collections of attributes and behaviorscalled...
Access Modifiers They determine whether other classes can use a particular field orinvoke a particular method. There are...
Access Modifiers A class can only have public or default modifier. In every java source code file, only one class in the...
Constructors Constructors are used to initialize the instance variables (fields)of an object. Constructors must have the...
this and super Keywords this calls another constructor in same class. super calls a constructor in a parent class. The ...
Static keyword Fields that have the static modifier in their declaration arecalled static fields or class variables. The...
Final keyword The final modifier can be applied to four Java constructs: variables: a final variable can be set once and...
Final keyword continued Final parameters are not considered part of themethod signature, and are ignored by the compilerw...
Upcoming SlideShare
Loading in …5
×

Object Oriented Principles

883 views

Published on

Object Oriented Principles

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
883
On SlideShare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
51
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Object Oriented Principles

  1. 1. An introduction to Java
  2. 2. Object Object is a computer representation of some real-worldthing (i.e person, place) or event. Objects can have both attributes and behaviors. An object can be considered a "thing" that can perform aset of related activities. The set of activities that the object performs defines theobjects behavior. The process of creating objects from a class is calledinstantiation. An object is an instance of a class. A reference value denotes a particular object.
  3. 3. Class A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe thedetails of an object. A class is the blueprint from which the individualobjects are created. Class is composed of three things: a name, attributes,and operations.
  4. 4. Encapsulation Encapsulation (or information hiding) is a principle, about hidingthe details of the implementation of the interface. It is to reveal as little as possible about the inner workings of theInterface. The encapsulation is the inclusion within a program object of all theresources needed for the object to function - basically, the methodsand the data. The class is kind of a container or capsule or a cell, which encapsulatethe set of methods, attribute and properties to provide its indentedfunctionalities to other classes. In that sense, encapsulation also allows a class to change its internalimplementation without hurting the overall functioning of the system. An object has properties and behaviors that are encapsulated inside theobject. The object offers services to the client but implementation of itsproperties and behavior is not a concern of the client and can bealtered without client’s knowledge.
  5. 5. Association Association is a (*a*) relationship between two classes,where one class use another. It allows one object instance to cause another toperform an action on its behalf. The relationship is structural, because it specifies thatobjects of one kind are connected to objects ofanother. Association is the more general term that define therelationship between two classes, where as theaggregation and composition are relatively special.
  6. 6. Aggregation Aggregation is a special type of an association. Aggregation is the (*the*) relationship between twoclasses. When object of one class has an (*has*) object of another,if second is a part of first (containment relationship) thenwe called that there is an aggregation between two classes. Aggregation is used to represent ownership or a whole/partrelationship. It is either the process of creating a new object from two ormore other objects, or an object that is composed of two ormore other objects.
  7. 7. Composition Composition is recognized as a special type of anAggregation. In the composite relationship in-between say object1 andobject2, they mutually depend on each other. Composition is used to represent an even stronger form ofownership than Aggregation. Example: Say University aggregate Chancellor or University has an(*has-a*) Chancellor. But even without a Chancellor aUniversity can exists. But the Faculties cannot exist withoutthe University, the life time of a Faculty (or Faculties)attached with the life time of the University.
  8. 8. Abstraction Abstraction is an emphasis on the idea, qualities andproperties rather than the particulars (a suppression ofdetail). The importance of abstraction is derived from itsability to hide irrelevant details and from the use ofnames to reference objects. It places the emphasis on what an object is or doesrather than how it is represented or how it works. Thus, it is the primary means of managing complexityin large programs.
  9. 9. Generalization Generalization reduces the complexity by replacingmultiple entities which perform similar functions with asingle construct. Generalization is the broadening of application toencompass a larger domain of objects of the same ordifferent type. Programming languages provide generalization throughvariables, parameterization, generics and polymorphism. It places the emphasis on the similarities between objects. Thus, it helps to manage complexity by collectingindividuals into groups and providing a representativewhich can be used to specify any individual of the group.
  10. 10. Specialization It is the process of defining a new object based on a(typically) more narrow definition of an existingobject, or an object that is directly related to, and morenarrowly defined than, another object. To create a subclass is specialization, to factor outcommon parts of derived classes into a common base(or parent) is generalization.
  11. 11. Abstract Class Abstract class cannot be instantiated. It can only be used as a super-class for other classes that extendthe abstract class. Abstract class is the concept and implementation gets completedwhen it is being realized by a subclass. In addition to this a class can inherit only from one abstract class(but a class may implement many interfaces) and must overrideall its abstract methods/ properties and may override virtualmethods/ properties. Abstract classes can carry a functional “payload” that numeroussubclasses can inherit and use directly, compared to interfaces. Abstract classes can have all or no abstract methods. The abstract keyword cannot be applied to static methods. Abstract method cannot be declared private. Abstract class may have constructors, but they cannot be used toinstantiate the abstract class.
  12. 12. Interface Interface separates the implementation and defines thestructure. It is useful in cases where you need to change theimplementation frequently. They are used to define a generic template and then one or moreabstract classes to define partial implementations of theinterface. An interface like that of an abstract class cannot be instantiated. A class that implements an interface does not define all themethods of the interface, then it must be declared abstract andthe method definitions must be provided by the subclass thatextends the abstract class. An interfaces can inherit other interfaces.
  13. 13. Interface versus Abstract Interface has no implementation, but they have to be implemented. Abstract class’s methods can have implementations and they have to beextended. Interfaces can only have method declaration (implicitly public and abstract)and fields (implicitly public static) Abstract class’s methods can’t have implementation only when declaredabstract. Interface can inherit more than one interfaces Abstract class can implement more than one interfaces, but can inherit only oneclass Abstract class must override all abstract method and may override virtualmethods Interface can be used when the implementation is changing Abstract class can be used to provide some default behavior for a base class. Interface makes implementation interchangeable Interface increase security by hiding the implementation Abstract class can be used when implementing framework Abstract classes are an excellent way to create planned inheritance hierarchiesand also to use as non-leaf classes in class hierarchies.
  14. 14. Polymorphism Polymorphism is the ability to create a variable, a function,or an object that has more than one form. It is the ability to request that the same operations beperformed by a wide range of different types of things. It enables the objects belonging to different types torespond to a method, field, or property calls of the samename, each one according to an appropriate type-specificbehavior (dynamic binding). It enables the objects of various types to define a commoninterface of operations for users. It allows a reference to denote objects of different types atdifferent times during execution.
  15. 15. Cohesion and Coupling Cohesion is an inter-class measure of how well-structured andclosely-related the functionality is in a class. The objective is to design classes with high cohesion, performingwell-defined and related tasks. Lack of cohesion in a class means that the purpose of the class isnot focused, and unrelated functionality is ending up in theclass. Coupling is a measure of intra-class dependencies. Objects need to interact with each other to carry out the task. The dependencies should however be minimized to achieveloose coupling, and to aid in creating extensible applications. The exposure of implementation details of an object is a majorsource of Intra-class dependencies and can impede changes inthe implementation of the object. High cohesion and loose coupling help to achieve the main goalsof OO design: maintainability, reusability, extensibility, andreliability.
  16. 16. Method Overloading and Overriding Polymorphism is only concerned with the applicationof specific implementations to an interface or a moregeneric base class. Method overloading refers to methods that have samename but different signatures inside the same class. Method overriding is where a subclass replaces theimplementation of one or more of its parentsmethods. Neither method overloading nor method overridingare by themselves implementations of polymorphism
  17. 17. Inheritance It is a technique to compartmentalize and reuse codeby creating collections of attributes and behaviorscalled Objects based on previously created objects. Inheritance aids in the reuse of code. The new classes, known as subclasses (or derivedclasses), inherit attributes and behavior of the pre-existing classes, which are referred to as super classes. The inheritance relationships of classes gives rise to aclass hierarchy.
  18. 18. Access Modifiers They determine whether other classes can use a particular field orinvoke a particular method. There are two levels of access control: 1) At the top level—public, or package-private (no explicit modifier). 2) At the member level—public, private, protected, or package-private (no explicit modifier). Public: When the modifier is public member is visible to all classes. Private: The private modifier specifies that the member can only beaccessed within its own class. Protected: The protected modifier specifies that the member can onlybe accessed within its own package and its subclass even in anotherpackage. Default: If a class has no modifier (the default, also known as package-private), member is visible only within its own package.
  19. 19. Access Modifiers A class can only have public or default modifier. In every java source code file, only one class in the fileis public accessible, and that class must have the samename as the file. Use private unless you have a good reason not to. Avoid public fields except for constants.Specifier Public Private Protected DefaultClass Y Y Y YSub Class Y N Y NPackage Y N N YAll Classes Y N N N
  20. 20. Constructors Constructors are used to initialize the instance variables (fields)of an object. Constructors must have the same name as the class. Constructors do not have a return value declaration. If you dont define a constructor for a class, a default parameter-less constructor is automatically created by the compiler, callingthe default parent constructor (super()) and initializes allinstance variables to default values. If you define any constructor for your class, no defaultconstructor is automatically created. The first line of a constructor must either be a call on anotherconstructor in the same class, or a call on the superclassconstructor. If the first line is neither of these, the compiler automaticallyinserts a call to the parameter-less super class constructor. Constructors can have the same access specifiers used forvariables and methods.
  21. 21. this and super Keywords this calls another constructor in same class. super calls a constructor in a parent class. The Java compiler inserts a call to the parentconstructor (super) if you dont have a constructor callas the first statement of you constructor.
  22. 22. Static keyword Fields that have the static modifier in their declaration arecalled static fields or class variables. They are associated with the class, rather than with anyobject, hence can be manipulated without creating aninstance of the class. Methods having static modifier are invoked with the classname, without the need for creating an instance of class. A common use for static methods is to access static fields. Static methods cannot access instance variables orinstance methods directly without an object reference. Static methods cannot use the this keyword as there is noinstance for this to refer to.
  23. 23. Final keyword The final modifier can be applied to four Java constructs: variables: a final variable can be set once and only once,allowing to declare local constants. They must be assignedexactly once. fields: a final field can also be set only once, by theconstructor of the class which defines it. methods: a final method cannot be overridden norhidden. classes: a final class cannot be extended. A final class hasall its method declared final. On the other hand, fields of afinal class do not have any special property.
  24. 24. Final keyword continued Final parameters are not considered part of themethod signature, and are ignored by the compilerwhen resolving method calls. Anonymous local classes can only reference localvariables and parameters that are declared final. Private and static methods are always implicitely final,since they cannot be overridden.

×