Sunday, March 6, 2016

Day 4 Object Oriented

Interfaces

As in the above example Teacher class contains the attributes and methods of Employee as well as Musician.
  1. Java does not support Multiple Inheritance
  2. Interface is similar to an abstract class that contains only abstract methods
  3. Declared with the keyword interface instead of the keyword class
  4. Keyword implements is used to represent the interfaces implemented by the class
Interface: Syntax
class ClassName extends Superclass implements Interface1, Interface2, ....
Example : class MusicTeacher extends Teacher implements Musician
Java OOps, Interface example diagram
All methods in an interface are implicitly public and abstract.
The keyword abstract before each method is optional.
An interface may contain definitions of final variables.
A class may extend only one other class, but it mayimplement any number of interfaces.
When a class implements an interface, it may implement (define) some or all of the inherited abstract methods.
A class must itself be declared abstract if it inheritsabstract methods that it does not define.
An interface reference can point to objects of its implementing classes.
Abstract method
1) A method that is declared but not defined
2) Declared with the keyword abstract
3) Example :
abstract public void playInstrument();
4) Has a header like any other method, but ends with a semicolon instead of a method body
5) Used to put some kind of compulsion on the class who inherits from this class. i.e., the class who inherits MUST provide the implementation of the method else the subclass will also become abstract
6) The following cannot be marked with “abstract” modifier
  • Constructors
  • Static methods
  • Private methods
  • Methods marked with “final” modifier

Abstract Classes

Abstract Classes
Outlines the behavior but not necessarily implements all of its behavior. Also known as Abstract Base Class.
Provides outline for behavior by means of method (abstract methods) signatures without an implementation.
Note 1: There can be some scenarios where it is difficult to implement all the methods in the base class. In such scenarios one can define the base class as an abstract class which signifies that this base class is a special kind of class which is not complete on its own.
A class derived from the abstract base class must implement those member functions which are not implemented in the abstract class.
Note 2: Abstract Class cannot be instantiated.
To use an abstract class one has to first derive another class from this abstract class using inheritance and then provide the implementation for the abstract methods.
Note 3: If a derived class does not implement all the abstract methods (unimplemented methods), then the derived class is also abstract in nature and cannot be instantiated.
Example of Abstract class and Abstract Method:
abstract class Costume {
  abstract public void Stitch();
   public void  setColour (){
     //code to set colour
   }
}
Here the class which inherits abstract class Costume can implement the abstract method Stitch depending upon what kind of Costume it is.

What is a Constructor

1) A method with the same name as class name used for the purpose of creating an object in a valid state
2) It does not return a value not even void
3) It may or may not have parameters (arguments)
4) A class contains one or more constructors for making new objects of that class
5) If (and only if) the programmer does not write a constructor, Java provides a default constructor with no arguments.
The default constructor sets instance variables as:
  • numeric types are set to zero
  • boolean variables are set to false
  • char variables are set to ‘’
  • object variables are set to null.
When a constructor executes, before executing its own code:
It implicitly call the default constructor of it’s super class Or can make this constructor call explicitly, with super(…);
A constructor for a class can call another constructor for the same class by putting this(…); as the first thing in the constructor. This allows you to avoid repeating code.
A Class do not inherit the constructors of a super class.
Generalization and Specialization:
In order to implement the concept of inheritance in an OO solution, one has to first identify the similarities among different classes so as to come up with the base class.
This process of identifying the similarities among different classes is calledGeneralization.
The class which is identified after generalization is the base class. The classes over which the generalization is made are the derived classes.
The classes over which the generalization is built are viewed asSpecialization.
Generalization identifies the common attributes and behavior among different entities whereas specialization identifies the distinct attributes and their behavior which differentiate a derived class from the other classes.
Generalization–Specialization hierarchy is used to depict the relationship between the generalized class and the corresponding specialized classes.

Access Specifiers

Access specifiers in a class
An access specifier is a keyword in OO Programming languages, which specify what access is permitted on a member.
There are three types of access specifiers:
public: Accessible to all. Other objects can also access this member variable or function.
private: Not accessible by other objects. Private members can be accessed only by the methods in the same class. Object accessible only in Class in which they are declared.
protected: The scope of a protected variable is within the class which declares it and in the class which inherits from the class (Scope is Class and subclass)
Default: Scope is Package Level
Approach to Object Oriented Design:
Step 1. Identify all the classes (Nouns; Type of objects) in the requirement specification,
Step 2. Identify the commonalities between all or small groups of classes identified (generalization) if it is obvious. Do not force fit generalization where it doesn’t make sense.
Step 3. In any given situation, start with the simplest object which can be abstracted into individual classes
Step 4. Identify all the member variables and methods the class should have
Step 5. Ensure that the class is fully independent of other classes and contains all the attributes and methods necessary.
Step 6. Keep all the data members private or protected
Step 7. The methods in the class should completely abstract the functionality
Step 8. The methods in the class should not be a force fit of procedural code into a class
Step 9. Inherit and extend classes from the base classes ONLY IF the situation has scope for it
Step 10. Define the relationships among the classes (“Has-A”, “Uses-A”)
Step 11. Keep the number of classes in your application under check (do not create any unnecessary classes)
Further more
  1. JAVA TRAINING IN KATHMANDU
  2. JAVA TRAINING IN LALITPUR

No comments:

Post a Comment