first three lecs
Majorly there are FOUR concepts in OOPs
1. Encapsulation 2. Abstraction 3. Polymorphism 4. Inheritance
A class can extend a class which extends another class etc.
Multilevel inheritance
What is Object-Oriented Programming (OOP)?
"a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of a hierarchy of classes united via inheritance relationships". (Grady Booch)
Polymorphism in java can be classified into two types:
1.Static / Compile-Time Polymorphism 2.Dynamic / Runtime Polymorphism
Java was invented by a team of -- employees of --- Microsystems, Inc. which is lead by ------- -------, in ----
13, Sun , james gosling , 1991
ORACLE buys SUN isn
2010
Variable
A location in the computer's memory where a value can be stored for use later in a program.
Encapsulation
A program exhibits encapsulation if its data is hidden; that is, if its data is difficult to access from outside of the structure (class or method) where the data is declared.
What is an object?
A software bundle of related state and behavior. ▸ Can have a physical existence e.g., a customer, a ticket, a car. ▸ Can have an intangible conceptual existence e.g., a meeting, a process.
Process of focusing on the essentials, ignoring the irrelevant and unimportant things Simply abstraction is hiding information which is not required We use access modifiers to implement this concept We use keywords like public, private, protected, friend....
ABSTRACTION
What is Abstraction?
Abstraction is a process which displays only the information needed and hides the unnecessary information.
A set of related data which identifies the current state of the object + a set of behaviors.
An Object
Which is the stronger (more hidden) form of encapsulation?
An instance variable may be shared by all the methods in the class, whereas a local variable is local to one method, so a local variable is more encapsulated
is one of the crucial OOPS concepts. Interface in Java is a completely abstract class. It contains only abstract methods and has to be implemented by another class. An interface is derived by a class by using the keyword "implements" instead of "extends". Interfaces are another way to achieve abstraction in Java
An interface
To initialize an ArrayList reference variable, in a declaration, use this syntax:
ArrayList reference-variable = new ArrayList<>();
◦ English-like abbreviations that represent elementary operations. ◦ Translator programs called assemblers convert early assemblylanguage programs to machine language. ◦ Unfortunately, it is also machine dependent
Assembly Languages
Why is encapsulation a good thing?
Because other structures aren't able to directly access the encapsulated data, it's more difficult for the other structures to mess things up
• Programs are more understandable - • Because people tend to think about problems in terms of objects, it's easier for people to understand a program that's split into objects. • Fewer errors - • Since objects provide encapsulation (isolation) for the data, it's harder for the data to get messed up.
Benefits of OOP
• How is encapsulation implemented?
Break a big problem into separate classes where each class holds a set of encapsulated data (those data items are called instance variables, and they should use the private access modifier). • Break a class's tasks into separate methods where each method holds a set of encapsulated data (those data items are called local variables) .Declaring data as private instance variables within a class is one form of encapsulation, and declaring data as local variables within a method is another form of encapsulation.
What are Classes?
Classes are like object constructors for creating objects. The collection of objects is said to be a class.
What is Compile-Time Polymorphism in Java?
Compile-Time polymorphism in java is also known as Static Polymorphism. to resolved at compile-time which is achieved through the Method Overloading.
Device that can perform computations and make logical decisions phenomenally faster than human beings can
Computer
Process of combining data and code Encapsulation = Data + Code This concept can be implemented using class Class = variables + functions
ENCAPSULATION
is one of the concepts in OOPs concepts; it is the process that binds together the data and code into a single unit and keeps both from being safe from outside interference and misuse. In this process, the data is hidden from other classes and can be accessed only through the current class's methods. Hence, it is also known as data hiding
Encapsulation
- Java and Internet (Applets) Applets are usually small programs and these helped in moving some user interactive programs from server to client, hence improving the usability of the web application. - Servlets • With Servlets, Java spanned on the both sides of client server system. • Servlets can be used to dynamically generate web content. - Byte Code • Java in order to support security and portability, does not compile the source code to executable code. It is translated into bytecode, which is highly optimized set of instructions designed to be executed by the Java run-time system, which is called Java Virtual Machine (JVM) • Converting the java program to bytecode instead of compiled code, offers a great ability to run those programs on multiple platform
Features of Java
◦ Single statements accomplish substantial tasks. ◦ Compilers (and interpreters) convert high-level language programs into machine language. ◦ Allow you to write instructions that look almost like everyday English and contain commonly used mathematical notations. ◦ Compiling a high-level language program into machine language can take a considerable amount of computer time. ◦ Interpreter programs execute high-level language programs directly, although slower than compiled programs run.
High-Level Languages
when to Declare a variable locally within a method?
If the variable needs to persist (remain alive) only for the duration of that particular method.
• Important: when to Declare a variable as an instance variable -
If the variable stores a fundamental attribute of the class's objects. • If the variable needs to persist for the duration of the class's objects (that is, it needs to persist longer than the duration of one particular method execution)
What is Inheritance?
Inheritance is a method in which one object acquires/inherits another object's properties, and inheritance also supports hierarchical classification.
◦ Delimited by /** and */. ◦ All text between the Javadoc comment delimiters is ignored by the compiler. ◦ Enable you to embed program documentation directly in your programs. ◦ The Javadoc utility program (Appendix M) reads Javadoc comments and uses them to prepare your program's documentation in HTML format.
Javadoc comments
Programmers write instructions in various programming languages, some directly understandable by computers and others requiring intermediate translation steps. These may be divided into three general types:
Machine languages Assembly languages High-level languages
is a set of binary-format instructions that can be directly run by a computer to solve a problem. A binary-format instruction is made up of all 0's and 1's because computers understand only 0's and 1's. Here is an example of an object-code instruction: 0100001111101010 • This particular object-code instruction is referred to as a 16-bit instruction because each of the 0's and 1's is a bit, and there are 16 of them. • Each object-code instruction is in charge of only a simple computer task. For example, an object-code instruction could possibly be in charge of copying a single number from some place in main memory to someplace in the CPU. • Programmers sometimes refer to object code as machine code. Object code is called machine code because it's written in binary and that's what a computer "machine" understands.
Object code
What are Objects?
Objects are always called instances of a class which are created from a class in java or any other language. They have states and behaviour.
◦ Software systems that make using computers more convenient. ◦ Provide services that allow each application to execute safely, efficiently and concurrently (i.e., in parallel) with other applications. ◦ The software that contains the core components of the operating system is called the kernel. ◦ Popular desktop operating systems include Linux, Windows 7 and Mac OS X. ◦ Popular mobile operating systems used in smartphones and tablets include Google's Android, BlackBerry OS and Apple's iOS
Operating systems
- Simple - Object Oriented - Robust - Multi Threading - Architecture Neutral - Interpreted and High Performance - Distributed - Dynamic
Other Features of Java
• A class may have more than one method of the same name, but with different signature.
Overloading methods
means that subclasses modify the methods they inherit from a superclass. This modification is limited in that the body of the method can be modified, but the method signature must stay the same
Overriding
means that subclasses modifies the methods they inherit from a superclass. • This modification is limited in that the body of the method can be modified, but the method signature must stay the same.
Overriding methods
The ability of an object to take many forms.
Polymorphism
What is Polymorphism?
Polymorphism refers to many forms, or it is a process that performs a single action in different ways. It occurs when we have many classes related to each other by inheritance
What is Runtime Polymorphism in Java?
Runtime polymorphism in java is also known as Dynamic Binding which is used to call an overridden method that is resolved dynamically at runtime rather than at compile time
A class can only extend ONE AND ONLY one parent class.
Single inheritance
Provide a single statement that declares, creates, and assigns a 100-element array that stores book titles
String [ ] bookTitles = new String[100];
• After writing a program, you'll normally want to have a computer perform the tasks specified by the program. Getting that to work is normally a two-step process: 1) Perform a compile command, 2) Perform a run command. • When you perform a compile command, you tell the computer to translate the program's programming language instructions to a binary format (all 0's and 1's). When you perform a run command, you tell the computer to read the binary-format instructions and perform the tasks specified by them. • The computer contains a special program called a compiler that's in charge of the compilation process. If you submit programming language instructions to a compiler, the compiler translates them into binary-format instructions. More formally, if you submit source code to a compiler, the compiler compiles the source code and produces object code as the result. Source code is the formal term for programming language instructions. Object code is the formal term for binary-format instructions.
The Compilation Process for Non-Java Programs
is the interface to the huge library of pre-built Java classes.
The Java API
• Why is top-down design beneficial?
The programmer can postpone worrying about the details of each subtask's implementation. That way, the programmer can focus on the big picture first. And focusing on the big picture is crucial because if the big picture is wrong, the whole thing is wrong.
Syntax for creating and assigning an array object:
array-variable = new element-type[array-size]; Example: long[ ] phonelist; phonelist = new long[ 10];
Computers process data under the control of sets of instructions called
computer programs
Array declaration syntax:
element-type[] array-variable;
- a way of hiding some of the private details of a class from other objects
encapsulation
Declaring data as private instance variables within a class is one form of ______________________
encapsulation
The key word __________ is used to express inheritance
extends
• In Java,• It is not possible to have more levels of inheritance
false
a class can inherit directly from only two other classes
false(ONE)
java was developed as a part of the ------ project
green
A computer consists of various devices referred to as
hardware
• A program exhibits encapsulation if its data is ____________; that is, if its data is difficult to ____________ from _______ of the structure (class or method) where the data is ____________
hidden , access , outside , declared
• Before.... • The C language developed in 1972 by Dennis Ritchie had taken a decade to become the most popular language • one named Object-Oriented Programming. With OOP, a person may write a certain code which may be reused later without rewriting the code over and over again • In 1979, Bjarne Stroustrup developed C++, an enhancement to the C language with included OOP fundamentals and features • A project named "Green" was initiated in December of 1990, whose aim was to create a programming tool that could render obsolete the C and C++ programming languages.
history of java
The ArrayList class is defined in the Java API's java.util package, so for files that use the ArrayList class, import it like this:
import java.util.ArrayList;
If we need various objects that are similar in structure, but not exactly the same → use
inheritance
a way of creating new classes from existing class without modifying it
inheritance
• If we need various objects that are similar in structure, but not exactly the same → use _____
inheritance
Array declaration examples:
int[] ids; double[] workHours; String[] names;
has become the language of choice for implementing Internet-based applications and software for devices that communicate over a network
java
was created based on the C & C++. Java uses C syntax and many of the object oriented features are taken from C++.
java
Any computer can directly understand only its own machine language, defined by its hardware design; i.e. it is machine dependent
machine languages
A method is invoked by a
method call
For many decades, hardware costs have fallen rapidly. ◦ Every year or two, the capacities of computers have approximately doubled without any increase in price. ◦ this Observation often is called
moore's law
As with all objects, arrays are created/instantiated with the ----- operator
new
a way of using common operation in different ways for different data input
polymorphism
All the members which are declared under private can be accessed by same class members only. No other class can not access the private members of any other class.
private
--- is good for simple and small programs, while the --- is better suited for large programs.
procedure-oriented programming (POP), Object-Oriented Programming (OOP)
These programs guide the computer through orderly sets of actions specified by people called computer
programmers
All the members which are declared under protected, can be accessed by same class members and its derived class members.
protected
All the members which are declared under public, can be accessed by any class members and from any where.
public
The programs that run on a computer are referred to as
software
A class that is derived from another is called a
subclass or child class.
The class from which the subclass is derived is called a
superclass or parent class.
Declare a variable locally within a method - • If
the variable needs to persist (remain alive) only for the duration of that particular method
: Declare a variable as an instance variable - • If
the variable stores a fundamental attribute of the class's objects If the variable needs to persist for the duration of the class's objects (that is, it needs to persist longer than the duration of one particular method execution)
"Hiding" of implementation details promotes good software engineering.
true
In Java, arrays are objects.
true
It's legal to combine an array's declaration, creation, and assignment operations.
true
Java innovated many new features to solve the fundamental problems which the previous languages could not solve
true
Overloading is not directly related to inheritance, but it is important to understand overloading when dealing with classes in an inheritance hierarchy
true
Return type void indicates that a method will perform a task but will not return any information to its calling method.
true
The concept of OOP is to create reusable code. There are three key principles of using OOP: - Inheritance - a way of creating new classes from existing class without modifying it. - Encapsulation - a way of hiding some of the private details of a class from other objects. -Polymorphism - a way of using common operation in different ways for different data input
true
The object-oriented programming breaks the programming task into objects, which combine data (known as attributes) and behaviors/functions (known as methods). Therefore, there are two main components of the OOP: class and object
true
When a class B inherits from a class A, then objects of class B are in some sense extended versions of class A objects.
true
in the interest of encapsulation, you should use local variables, as opposed to instance variables, whenever possible
true
java Initially it was called Oak, later it was changed to Java in 1995
true
machine languages Generally consist of strings of numbers (ultimately reduced to 1s and 0s) that instruct computers to perform their most elementary operations one at a time
true
• In Java, • a class can inherit directly from only ONE other class
true
• In Java,a class can have more than one subclass
true
• It's legal to combine an array's declaration, creation, and assignment operations
true
• How can bytecode be run on any type of computer?
• As a Java program's bytecode runs, the bytecode is translated into object code by the computer's bytecode interpreter program. The bytecode interpreter program is known as the Java virtual machine, or JVM for short. The next slide shows how the JVM translates bytecode to object code. It also shows how a Java compiler translates source code to bytecode.
Why is top-down design also called stepwise refinement?
• Because you implement your code in separate steps. • You first implement the top level task and then you go back later and refine your program by implementing the subtasks in the next lower level
• Given a problem description. What steps should you use to implement its solution?
• Decide on the classes/objects that are needed. • For each class, decide on its instance variables. • For each class, decide on its public methods (the public methods form the class's interface). • For each method, implement it using top-down design. • Use top-down design for your methods: • In writing a public method (a "top" method), if the method is fairly involved and can be broken into subtasks, then have the public method call private helper methods to do the work for each subtask. • Finish writing your public methods before starting to write your private helper methods.
: Happens during method overriding, that is a method with the same signature exists both in parent and child class. When a parent reference is used to refer to a child object, the method that will be executed with be defined at run-time, therefore will be the child's overridden method.
‣ Dynamic Polymorphism
Happens during method overloading, that is more than one method have the same name but different signatures. ‣ Also known as Compile-Time Polymorphism, Static binding, Compile-Time binding, Early binding
‣ Static Polymorphism
What is a class?
▸ A blueprint or prototype from which objects are created. ▸ An object is an instance of a class and the process of creating it is called instantiation.
Software may be divided into:
◦ Operating Systems; e.g. Windows ◦ Applications and Programs; e.g. Microsoft PowerPoint
Java programs may be divided into:
◦ Standalone/desktop applications (do not need a web browser to run) ◦ Applets (need a web browser to run