Study Guide Notes PDF
Document Details
Uploaded by FuturisticSpring
Tags
Summary
This document provides study notes on fundamental object-oriented programming (OOP) concepts. It covers topics like objects, classes, constructors, and inheritance.
Full Transcript
Object: - Must have no explicit return type - Cannot be abstract, static, final, and - An entity that has state and behavior...
Object: - Must have no explicit return type - Cannot be abstract, static, final, and - An entity that has state and behavior synchronized - Can be defined as an instance of a class - Types of constructors: - Contains an address o Default - Takes up some space in memory o Parameterized - Can communicate without knowing the details of each otheer’s data or code Default: - Only thing necessary is the type of message - A no-args constructor accepted and the type of response returned - Compiler inserts by the objects - Contains a default to call super() Class: - Implementing any constructor → do not receive a default constructor - Templates that are used to create objects and define object data types and methods Parameterized Constructor: - Core properties: - Has a specific number of parameters o Data types - Used to provide different values to distinct o Methods objects - Can be defined as a blue print where you can create an individual object Constructor overloading: - Don’t consume any space - Technique of having one or more - A blueprint from which individual objects constructor with different parameter lists are created - Arranged in a way that each constructor Advantages of Oops over Procedure Oriented performs a different task Programming: - Differentiated by the compiler by the number of parameters in the list and their - Make development and maintenance easier types - Procedure Oriented Programming is not easy to manage if code grows Inheritance: - Provides data hiding - A mechanism where one object acquires all - Procedure Oriented Programming language the properties and behaviors of a parent is a global data that can be accessed object anywhere - An important part of OOPs Constructor: - You can reuse methods and fields of the parent class - Block of codes similar to the method - you can add new methods and fields in your - Called when an instance of the object is current class also created - represents IS-A relationship - Memory is allocated for the object - Used to initialize the object advantages of Inheritance: - Called when an object is created - method overriding - Just like a method but without return type - code reusability Rules for creating constructor: Inheritance terms: - Name must be the same as class name - class: a group of objects that have common different ways to overload: properties - changing number of arguments or data type - sub class: a class which inherits the other class; aka derived, extended, or child class method overriding: - super class: the class from which the - subclass has the same method as declared in subclass inherits features; base or parent parent class - usage: - reusability: a mechanism which facilitates o to provide specific implementation you to reuse fields and methods of the of a method which is already existing class when you create a new class provided by its superclass syntax: o runtime polymorphism - rules: - extends keyword: indicates that user is o method must have the same name as making a new class that derives from an in the parent class existing class o method must have the same inheritance types: parameter as in the parent class o must be an IS-A relationship - single - used to provide specific implementation of - multilevel the method - hiererchial - occurs in tow classes that have IS-A - multiple relationship - hybrid - parameter must be the same multiple inheritance not supported by Java: - example of the run time polymorphism - return type must be same - reduces complexity and simplifies the language can static method be overridden? polymorphism: - No because the static method is bound with class whereas instance method is bound with - the ability of an object to take on many an object forms - Static belongs to the class area - most common occurs when a parent - Instance belongs to the heap area reference is used to refer to a child class object Encapsulation: - java that pass more than one IS-A test - A process of wrapping code and data method overloading: together into a single unit - Advantages: - a class has multiple methods having the o Can make the class read-only or same name but different in parameters write-only skipping getter and setter - having same name of the methods increases methods readability of the program o Provides the control over the data - used to increase the readability of program o A way to achieve data hiding - performed within class because other class will not be able - parameter must be different to access the data through private - example of compile time polymorphism data members - return type can be same or different o Easy to test - Points: o Better for unit testing o Must be declared with an abstract keyword Modifiers: o Can have abstract and non-abstract - Types: methods o Access o Cannot be instantiated o Non-access o Can have constructors and static methods Access Modifiers: o Can have final methods which forces - Specifies accessibility of a data member, the subclass not to change the body method, constructor, or class of the method - Types: Abstract method: o Private o Default - Method that is declared abstract and does o Protected not have the implementation known as an o Public abstract method - Overridden method must not be more String handling methods: restrictive - Char charAt(int index): returns the character Abstraction: at the specified index - A process of hiding the implementation and - Int compareTo(Object o): compares this showing only functionality to the user string to another Object - Shows only essential things to the user and - String concat(String str): concatenates the hides the internal details specified string to the end of this string - Puts focus on what the object does instead of - Boolean equals(Object anObject): compares the how this string to the specified object - Ways to achieve: - Boolean equalsIgnore Case(String o Abstract class (0 to 100%) anotherString): compares this string to o Interface (100%) another string, ignoring case considerations Generalization: Package: - The process of extracting shared - A group of similar types of classes, characteristics from 2 or more classes and interfaces, and sub-packages combining them into a generalized - Two form: superclass o Built-in package o User-defined Specialization: - Advantages: - Creating new subclasses from an existing o Used to categorize the classes and class interfaces so that they can be easily maintained Abstract class: o Provides access protection - A class that is declared abstract o Removes naming collision - Can have abstract and non-abstract methods Accessing a package from other packages: - Needs to be extended and its method implemented - Import package.*;: all classes and interfaces - Interface is implemented by class not of this package will be accessible but not extended subpackages - Interface can extend multiple interfaces - Import package.classname;: only declared Declaring interfaces: class of this package will be accessible - Fully qualified name: only declared class of - Interface keyword is used to declare an this package will be accessible interface - Do not need to use the abstract keyword Sequence of program while declaring an interface because it is - Must be package then import then class implicitly abstract - Methods are implicitly abstract Interface: - Methods are implicitly public - A reference type that is similar to a class Implementing interfaces: o A class implements an interface and inherits the abstract methods of the - If a class does not perform the behaviors of interface an interface, the class must declare itself - A collection of abstract methods abstract - Method bodies exist only for default - Class uses the implements keyword to methods and static methods implement an interface - Writing interface similar to writing class - Rules: - Class describes the attributes and behaviors o A class can implement multiple of an object interfaces - Interface contains behaviors that a class o Class can only extend one class implements o An interface can extend another - Methods of the interface need to be defined interface like class to another class in the class Extending interfaces: Class and interface similarities: - Extends keyword is used to extend an - Interface can contain any number of interface methods - Child interface inherits methods of parent - Name of the interface matches the name of interface the file Extending multiple interfaces: - Byte code of interface appears in a.class file - Interfaces appear in packages - Class can only extend one parent class - Corresponding bytecode file must be in a - Multiple inheritance is not allowed directory structure that matches the package - Interfaces are not classes but can extend name more than one parent interface - Extends is used once and parent interfaces Differences: are declared in a comma separated list - Cannot instantiate an interface Exceptions: - Interface does not contain any constructors - Methods in interface are abstract - Events that occur during the execution of - Interface cannot contain instance fields only programs that disrupt the normal flow of static and/or final instructions - An object that wraps an error event that - Error subclass is derived from the occurred within a method Throwable class - Contains: Catching exceptions: o Information about the error o The state of the program when the - Method catches an exception using a error occurred combination of try and catch keywords o Other custom information - Try/catch block is placed around the code - Categories: that might generate an exception o Checked o Code is referred to as protected code o Unchecked o Code that is prone to exceptions is o Errors placed in try block o Exception is handled by catch block Checked exceptions: associated with it - Compiler enforces you handle them - Catch statement involves declaring the type explicitly of exception you are trying to catch - Methods that generate checked exceptions Throw/throws keyword: must declare that they throw them - Methods that invoke other methods that - Used if a method does not handle a checked throw checked exceptions must handle them exception or propagate by declaring that they throw - Throws keyword appears at the end of a them method’s signature - One can throw an exception using the throw Unchecked exceptions: keyword - Errors and RunTimeExceptions - Throws is used to postpone the handling of a o Compiler does not enforce that you checked exception handle them explicitly - Throw is used to invoke an exception - Methods do not have to declare that they explicitly throw them Finally block: - Application cannot do anything from these exceptions - follows a try or catch block - always executes Errors: - allows you to run any cleanup-type - Not exceptions statements that you want to execute - Problems that arise beyond the control of the - appears at the end of the catch blocks user or programmer array: - Typically ignored in code because you can rarely do anything about it - an object which contains elements of a - Happens when a stack overflow occurs similar data type - Ignored at the time of compilation - stores similar elements - fixed set Exception hierarchy: - first element is stored at 0 index - Exception classes are subtypes of the - advantages: java.lang.Exception class o code optimization: can retrieve or - Exception class is a subclass of the sort the data efficiently Throwable class o random access: can get any data The List Interface: located at an index position - Extends Collections - disadvantages: - Declares the behavior of a collection that o size limit: doesn’t grow its size at stores a sequence of elements runtime; to solve this problem - Elements can be inserted or accessed by collection framework is used in java their position in the list which grows automatically - Uses a zero-based index - types: - May contain duplicate elements o single dimensional - Defines some of its own o multidimensional - Methods will throw an Queue: UnsupportedOperationException if the collection cannot be modified - available in java.util package - ClassCastException is generated when one - extends the Collection interface object is incompatible with another - fifo - most frequently used implementations: ArrayList: o LinkedList - A part of collection framework o ArrayBlockingQueue - Present in java.util package o PriorityQueue - Proves dynamic arrays Collection interface: - Slower than standard arrays - Helpful where lots of manipulation in the - Foundation upon which the collections array is needed framework is built - Inherits AbstractList class and implements - Declares the core methods that all List interface collections will have - Initialized by a size - Collection framework is unified architecture o Size can increase if collection grows for representing and manipulating or shrunk if objects are removed collections from the collection - Collections framework contain: - Allows us to randomly access the list o Interfaces - Can not be used for primitive types, like int, o Implementation/ classes char, etc o Algorithms: methods that perform o Need a wrapper class for such cases useful computations - searching and sorting – on objects that implement The Set Interface: collection interfaces; said to be - A Collection that cannot contain duplicate polymorphic – the same method can elements be used on many different - Models the mathematical set abstraction implementations of the appropriate - Contains only methods inherited from collection interface Collection - Framework defines several map interfaces - Adds restriction that duplicate elements are and classes prohibited o Maps store key/value pairs - Adds a stronger contract on the behavior of - Maps are not collections but are fully the equals and hashCode operations integrated with collections The Map Interface: - Maps unique keys to values - Allows users to access data in the relational o Key: an object used to retrieve a database management systems value at a later date - Allows users to describe the data - Key and value → can store the value in a - allows users to define the data in a database Map object and manipulate that data o Can retrieve a value using a key - allows to embed within other languages - NoSuchElementException: when no items using SQL modules, libraries, and pre- exist in invoking map compilers - ClassCastException: thrown when an object - allows users to create and drop databases is incompatible with the elements in a map and tables - NullPointerException thrown if an attempt - allows users to create view, stored is made to use a null object and null is not procedure, functions in a database allowed in the map - allows users to set permissions on tables, - UnsupportedOperationException thrown procedures, and views when an attempt is made to change an SQL Architecture unmodifiable map Database: - A collection of information that is organized so that it can be easily accessed, managed, and updated Relational Database: - A type of database that uses a structure that allows us to identify and access data in relation to another piece of data in the database - Often organized into tables Tables: Rows and Columns: - Rows are called records - Columns are labeled with a descriptive name Primary Key: Relational database management system: - a field in a table which uniquely identifies - A program that allows you to create, update, HTML: and administer a relational database - Use SQL language to access database - stands for Hyper text makeup language - describes the structure of web pages using SQL – Structured Query Language: markup - Used to communicate with data stored in a - elements are the building blocks of html relational database management system pages - Used to access the data in tables - elements are represented by tags - tags label pieces of content such as Why do we need SQL? “heading”, “paragraph”, “table”, and so on - browsers do not display html tages o use them to render the content of the - == operator tests for abstract equality and page does the necessary tupe converions before doing the equality comparison CSS: - == tests for strict equality and does not do - stands for Cascading Style Sheets the type conversion - describes how html elements are to be o If values are not the same then it will displayed on screen, paper, or in other media return false - saves a lot of work - can control the layout of multiple web pages all at once - stores external stylesheets - can be added to html elements in 3 ways: o inline ▪ used to apply a unique style to a single html element ▪ uses the style attribute of an html element o internal: ▪ used to define a style for a single html page o external: ▪ used to define the style for many html pages ▪ to use add a link to it in the head section of the html page Css Selectors: - patterns used to select the elements you want to style JavaScript: - can update and change both html and css - can calculate, manipulate, and validate data - strings are text written within double or single quotations Javascript Variables: - used to store data values - uses the var keyword to declare variables - an equal sign is used to assign values to variables Comparison Operator: