• Algorithmic/process decomposition of the problem focusses on sequence of the functions/tasks, here code acts on data (int area(int height, int weight) doesn’t make sense but int area(Height h, Breadth b) does). Changing sequence means changing functionality. Here, only computation is important. • OO decomposition focusses on real life objects. We can reuse, specialize, OO are resilient to changes. • Library Management: In algorithmic decomposition, Every function at higher level calls a function at the lower level. • Message passing: one object calls a method of another object. • OOA => OOD => OOP • OOP => bottom up approach, data acts on code (how data is being effected), the building blocks are objects, message passing, closer to real world, abstraction and encapsulation, reusability and is resilient to change Abstraction: two steps of abstraction: • Firstly, choose attributes. An object may have infinite amount of properties and behaviour, however not all of them are useful for me according to my OOA. Hence, different properties of the same entity may be used in different context. For example, a student in Medical Centre will have to give blood grp, height and weight, but they are not required in AUGSD, they only require my courses etc. in cab booking, I need not know the user’s height, but his blood grp may be important. • Secondly, expose only interface of the class. Abstraction gives an outside view of the object. For complex entities like car, I only need to know “change gear” function, how gears actually work is of no use to an outsider. Hence, they are made private. In cab booking, one may not know driver’s account balance, but he must know his name and gender. Hence account balance private, rest all public. Encapsulation: • It is the implementation of the second point of abstraction. Outside view if fine, but how do I implement the inside view? With the help of encapsulation, we bring together or bind the data and the code, compile them into a class such that all the properties of the object that are useful are represented while the implementation part is hidden with necessary access specifiers. It prohibits arbitrary access. • For example, in a stack, we encapsulate or bind or wrap the stack array, the top pointer, the base pointer, push and pop methods, etc. But to an outsider, things like array are not visible. Encapsulation is because of class, everything is inside a class. Modularity • Decompose the problem into smaller problems to reduce complexity. In OOP, the smaller problems are objects. In process oriented, it’s functions. • Now once I have achieved the basic units, I group these objects according to functionalities for better complexity. This is called modularity. It is achieved through packages or header files. Summary so far, abstraction gives an outside view of the object, encapsulation hides the inner details through class and modularity clusters related abstractions. Hierarchy • Inheritance is a type of hierarchy which reflects “is type of”. Top to bottom is specialization, bottom to top is generalization. Rest is selfexplanatory. Multiple inheritance is not allowed because of name clashes and repeated inheritance. • Aggregation is a “part of” relation. A wall is a part of the room. But it can be a standalone wall and it can be a part of more than one rooms. Hence it is not composition, but walls and windows are. Strongly Typed • Java is strongly typed, Python is not. Being strongly typed is good as conflicts are resolved at compile time and code is easier to read. Static/Dynamic Binding • Static (object name bound to memory at compile) o Shape s; o S=new Shape(); o S=new Line(); • Dynamic (at runtime) o Shape s; o if(string=”yes”) s=new Shape(); o else s=new Line(); Polymorphism • a super class reference can point to both sub class and super class object. • Functions can be overridden or overloaded. Concurrency • Process Control Block and Context switching Coupling and cohesion • Degree of association (dependency of one class on another) must be minimized. Strongly coupled class means that changes to this class will have to be reflected to many classes. • Cohesion among the members of the class be maximized, ie, they must be as related to the class and to each other as much as possible. Why reference of subclass cannot point to super class? • As a user, I know the properties related to the sub class. If I create a reference to the subclass, and point it to the super class, I may use the methods of the subclass which are not present with the super class. This is not the problem with other way round. Consider a calculator with only add and subtract as super class and multiply and divide as subclass. Problem with cloning • If the object cloned contains a reference to another object, then the cloned object’s object still points to the original object’s object. Hence the changes made there will be reflected here also. Hence clone method in Object class is protected, and the class must implement Cloneable interface to make sure unwanted properties are not there. Nested classes implement composition. • Important methods o protected void finalize() ▪ System.gc() explicitly asks JVM to execute garbage collector. It is up to JVM to decide when to execute garbage collector and hence call finalize(). When garbage collector calls finalize, the exceptions are ignored. When user calls, they are not. System.runFinalization() may be called after System.gc() o public boolean equals(Object o) o public String toString () o protected Object clone() ▪ throws exception. ▪ Must implement Cloneable interface Interface vs Abstract classes • None of them can have their instances. Though their subclasses can. Abstract class can also have constructors. • Interfaces create strong abstractions, and very loose coupling. JDK 8 allows interface to implement methods. • Abstract class and its subclasses are logically related to each other. Not so for interfaces. Hence if there is no common code, and multiple inheritance is required, use interfaces. • All variables in interfaces are static and final, and interface can have only abstract methods. Abstract classes can have simple methods which may be static protected etc. • A class that implements an interface must implement all the methods, otherwise it becomes an abstract class. • Example: HashMap implements Serializable, Cloneable and extends AbsractMap class Overriding • Dynamic method dispatch – what function is called is decided at runtime. • Which function is called depends on the type of object and not the reference. UML • UML is a modelling language that helps me visualize the system that I would be making, and its classes, objects, relations between classes, or flow of control. Why needed? In real world, the applications can get really complex and involves many teams. UML diagram breaks down and shows us the problem in a simpler and visual way, and hence multiple teams may focus on the individual problems, and all the teams are on the same page. Businessmen can understand our model as well, and it saves time. • Sequence diagrams show a single use case of use case diagram. o Leftmost object is the initiator object for the given use case, it may already be available due to some other interaction. o The objects already existent are at the top. o Collaboration diagram focusses on objects rather than the flow of control. Collab diagrams show the links between the objects, i.e., what objects can call what objects with flow of control written in numbers. • In object diagram, links show that those elements are related. An Object Diagram can be referred to as a screenshot of the instances in a system and the relationship that exists between them. Class diagram does not show state of the object, while object diagrams may. • State diagram, activity diagram. • Super class object can be type cast to subclass. • garbage collection of Java • process: every instance of a program is a process. E.g., you open notepad 3 times then there are 3 process. Every process has its own address space. • Address space is divided into 3 parts: for storing code, stack segment and heap segment. Stack segment holds the method and its local variables and references to objects. The memory for objects and the static variables is actually allotted in heap. A stack frame exists so long as the method doesn’t end. Recursion leads to multiple stacks; each is lost only when its return statement is encountered. Stack is implemented from top to bottom and heap from bottom to top(why?) Members of the heap remain there until the process is over Next lect • Doubts again on package • Static members: all the objects share the same copy of a static member. The member may be referred by an object(?). otherwise, static variable can be accessed without any reference or by class reference: className.variable; static variables are allocated when the class is loaded, instance var when object is created • Static methods: same as above. But can I call static methods with object? Yes. Can I call a static method without object? Yes. But static method must not have a non-static instance variable. Never. A non-static method can access static variable. • Static blocks: Executed only once when the class is loaded. • Static data members cannot access non static members. • Non static methods can access everything Skipped 2 lectures on inheritance However, here is a notes on them: • A super class object can reference to subclass object, but remember it cannot access the data members and methods exclusive to subclass. Hence if a constructor of a superclass receives an object of type Super, but the subclass sends an object of type Base, it is valid. • Also note :- While the base class cannot access the members of derived class, if base and subclass have a same function(overriding), base class reference will point to (and access) derived class version. • super() refers to the immediate super class only. 12th Sept Lect • method overriding, when a method is called, objects version of method is used. If you call a method of same name from within a derived class, use super.method() may be used. Otherwise the derived class’s method is invoked. • Note that super keyword can only be used in a derived class. If an reference of base class points to a derived method, the derived method is called. • Abstract Class: An object (not reference) of an abstract class can never be created. Abstract class must have the “abstract” keyword. At least one (or all?) subclasses of an abstract class must provide implementation to an abstract method, otherwise they also become incomplete and their objects cannot be created. • Abstract classes are created to provide template and generality. • final keyword: class is final, you cannot inherit it. Method is final, you can override it. • boolean equals(Object o) :it is PUBLIC. It checks whether the the address of 2 objects is same. If you define it as boolean equals(myClass A) then it is not overriding as original equals only receive parameter of Object class. o To override equals() method follow this: public boolean equals(Object ob){} you can send any object type because Object is superclass of all. • Sub class reference cannot point to superclass reference even if superclass is pointing to sub class object, but you can type-caste 14th September • Object clone(): assignment is not same as cloning. Using = operator is making references to same object. Home assignment, study java.lang package. • Try this: className ob1=new className(); className ob2=ob1; • • • • • ob1.age=75: print(ob2.age); Cloned object should avoid reference data type. String toString(): To Override, public String toString(){ return “blah blah”+length; } Overridden method is called unless super keyword is used. SOPln always calls toString function to print stuff. Try printing object Exception Handling • Exception is an object o java.lang.ArithmeticException => object of type ArithmeticEx • try, catch, throw, throws, finally. • There are exceptions and errors. o Errors are generated due to runtime environment’s fault. For example, stack overflow. try catch blocks are usually for this. o There are exceptions and runtime exceptions. Runtime exceptions are automatically handled by Java and code is compiled. Hence they do not require try catch block explicitly. • TRY BLOCK o Contains the code that it expected to throw an exception. o Control does not return to try block once an exception is thrown. It will execute one or none catch block and go to finally() block o Try block without both catch and finally generates error. o Therefore, there should be no code b/w catch() and try() block o Exception is the base class for all other runtime errors. Hence catch(Exception e) always works. But one type or error cannot catch another error. o The succeeding code is executed only when the exception is handled. finally() is always executed no matter what. • THROW o An exception is explicitly thrown by user to terminate program. It should be in a try block and corresponding catch should be there. o If you throw a random error new Exception(), compilation error will occur as the Exception object generated is not necessarily of RuntimeException type. • CUSTOM CLASS o Every custom class must ▪ Extend Exception ▪ Have a constructor to give an ID to itself ▪ Have the function: String toString() { return id+” InvalidAgeException”; } ▪ Wherever you use it, remember to use throws and throw keyword both