Slides05_Object Oriented Concepts Using Java

Embed Size (px)

Citation preview

  • 1

  • 2

  • 3

  • 4

  • 5

  • Assume that other instance variables of Customer class like customerName, contactNos[] are present in the class diagram

    6

  • 2009-2011, Infosys Limited. Confidential7

    We can have a displayCustomerInformation() method for displaying customer details.

    In which class will this method be defined in

    Customer class or Regular/Privileged

    classes?

    If this method is placed in Customer class, it can access only the instance variables ofCustomer class

    If this method is placed in child classes, it cannot access the instance variables of Customerclass since all the instance variables of Customer class are private in nature

    We need to know how to define the method in both the parent class and child class(es)and invoke it based on the type of Customer

    Retail Application Case Study

    Retail Application Case Study Steps Forward

  • 8

  • 9The weaker access privilege concept would be discussed alongwith other access specifier in Day 6

  • 10

    Assume that the instance variables of Customer class are customerId, customerName, contactNos[] and address

  • 11

    Assume that the instance variables of Customer class are customerId, customerName, contactNos[] and address

  • 12

  • 13

  • 14

  • 15

    Dynamic binding is also called as late binding or runtime polymorphism

  • 16

  • 17

  • 18

  • 19

    Q1.BaseVariable:3Derived variable: 2CommonVar:2

  • 20

    Q2. Compilation Error: a private variable of the super class cannot be accessed directly in the sub class

  • 21

    There is a compilation error-.: cannot find symbolsymbol : constructor Base()location: class Base

    Der(int v){^

    1 error

    This is because, in the Base class a parameterized constructor has been defined, but when the object of derived class is made, there is no explicit invocation of the base class constructor and hence the system tries to invoke the default constructor of Base class which is not available.

  • 22

    Q4.Compilation Error: when a super class reference is pointing to a sub class object, only the overridden methods of the sub class and the methods of the super class can be accessed

  • 23

  • 24

  • 25

  • 26

  • 27

  • 28

  • 29

  • 30

  • 31

  • 32

  • 33

  • 34

    -Constructors cannot be abstract as they are used for construction of the object and cannot be incomplete. They are also not overridden in the derived classes-static methods are not overridden and hence cannot be abstract-private methods are available only to the class in which it is defined , hence cannot be overridden and hence cannot be abstract-

  • 35

    Composite Aggregation (or Composition) created and destroyed with the containing object (e.g. house & walls)It is represented using a filled diamond. The relationship between Purchase and Customer is composition as the Customer ceases to exist if the Purchase class is destroyed

  • 36

    Q1.Ans:Compilation Error: abstract method is not overridden in the sub class and the sub class is not abstract

  • 37

    Q2.Compilation error : Constructor cannot be made abstract

  • 38

    Q3.Compilation Error: private method cannot be made abstract

  • 39

    Q4.Parent Id:1000 Child Id:2000

    Three .class files will be created. One for ParentClass, One for ChildClass and One for Demo

  • 40

    Q5.display in Example1

  • 41

    Q6.Compilation error: abstract class cannot be instantiated

  • 42

    Q7.Compilation error: abstract method cannot have body

  • 43

  • 44

  • 45

    Interface are syntactically like class. But it will not have instance variables and all the method are abstract

    Why?Variables of an interface are public static final be default due to the following reasons:

    public: every class using the interface must be able to access the variables of the interfacestatic: an object for an interface cannot be created, hence every variable of the interface belongs to the interface itselffinal: if variables are not maintained as final, it will not be possible to identify its value at point of time since every class which is using the interface can change its value

    If final is used with an instance variable, the instance variable will be considered as a CONSTANT

    All the methods of an interface are public and abstract by default since the implementation of the method will depend upon class which is using the interface

  • 46

    An interface represents a realization relationship.In UML a realization relationship is one between two elements, in which one element say A realizes the methods provided by the element B. The symbol used is

  • 47

  • An interface represents a realization relationship.In UML a realization relationship is one between two elements, in which one element say A realizes the methods provided by the element B. Here Tax interface is realized by the class Purchase as it implements the method computeTax() declared in the interface Tax.

    48

  • 49

    Concrete methods are methods which have a definition.

  • 50

    Q1. a. Trueb. Truec. Trued. Falsee. Truef. False

  • 51

    Q2.Compilation Error: abstract method is not overridden in the sub class and the sub class is not abstract

  • 52

    Q3.display in Example1900

  • 53

    Q4.

    display in Example3disp in Example3

  • 54

  • 55

  • 56

  • 57

    A package is a collection of related classes and interfaces. Packages are containers for classes that are used to keep the class name space compartmentalized.

    The package declaration, if any, must be at the beginning of the source file. You may precede it with white space and comments, but nothing else. Only one package declaration is permitted and it governs the entire source file.

    A java program can contain any of the following four parts.A single package statement (optional)Any number of import statements (optional)Any number of classes and interfaces out of which only one class can be public

    The package statement defines a namespace in which classes are stored. It is nothing but a directory, in which a class is defined. When the package name is omitted, it is put into the default package, which has no name (i.e.. The current directory)

  • 58

  • 59

    Organize your classes into smaller units and make it easy to locate and use the

    appropriate class file i.e., you can split up the classes logically.

    Avoid naming conflicts Hierarchical approach to store files/classes.

    Packages allow you to protect your classes, data and methods in a larger way than

    on a class-to-class basis.

    Package names can be used to identify your classes.

    Some rules to follow while creating packages

    In one .java file, there can be only one class declared public and that becomes the name of the .java file

    A package can however have several public classes spread across

    multiple .java files

    More than one class/interface can have the same name but it should

    be in a different package

  • 60

    Here, sqrt() method is a static method of Math class, hence can be invoked using the class name.

  • 61

  • 62

  • 63

  • 64

    If no access specifiers are provided for a class, methods and variables in Java , the default access specifier is considered

  • 65

  • 66

    The access level cannot be more restrictive than those given in parentThe access level can be less restrictive than that given in the parent

    Overridden method cannot have a weaker access specifier as mentioned in Slide 60 of Day 4

  • 67

    With the help of packages , the name clashes can be avoided . But class names cannot be repeated in a package.

    While using C language , the values of global variables can be changed by any code/programmer in the same project. But in Java this can be prevented by default access specifier. Only the class in the same package can access the data member or the member method

  • 68

  • 69

  • 70

  • 71

  • 72

    Q1.protected int num2

  • 73

    Q2. Compilation Error : Inside a .java file, only one class can be made public and if there is a starter class, then that class should be made public

  • 74

    Q3. Compilation Error : Overridden method cannot have a weaker access privilege

  • 75

    Q4. Compilation Error : Overridden method cannot have a weaker access privilege

  • 76

    Q5.Compilation error: attempting to assign weaker access privileges

  • The class diagram for the complete retail store application is shown above.

    77

  • 78

  • 79