Object Oriented Programming

¡Supera tus tareas y exámenes ahora con Quizwiz!

Private Scope

A variable identified as private can only be used in the class. Variable needs to be identified at class level, see code above. In this code intNumber2 is identified as private and can just be used in this piece of code.

Procedural Programming

A procedural language is a type of computer programming language that specifies a series of well-structured steps and procedures within its programming context to compose a program. It contains a systematic order of statements, functions and commands to complete a computational task or program.

Getters and Setters

A property that is given the "public" modifier but that has been specifically given a "get" method but no "set" method cannot be set from outside its class

Abstraction

Using abstraction, one can simulate real world objects. Abstraction provides advantage of code reuse Abstraction enables program open for extension

Benefit of using Aggregation?

We could use two other classes College and Staff that need an Address. To set the College Address and Staff's address we don't need to input the same code again and again. We just have to use the reference of Address class while defining College and Staff:

for each loop

A "for each" statement accesses each successive element of an array, List, or Set without the bookkeeping associated with iterators or indexing.

Functions

A call to a function is an expression that returns a value. A function can be defined similar to a procedure except that we replace void with the output type public <output type> <procedure name> ( <input list> ) { //code }

What is a class?

A class, in the context of Java, are templates that are used to create objects, and to define object data types and methods. ... All class objects should have the basic class properties. Classes are categories, and objects are items within each category

Example of 2D Array

A multi-dimensional array is one that can hold all the values above. You set them up like this: int[ ][ ] aryNumbers = new int[6][5];

Encapsulation

Encapsulation means putting together all the variables (instance variables) and the methods into a single unit called Class. It also means hiding data and methods within an Object. Encapsulation provides the security that keeps data and methods safe from inadvertent changes. Programmers sometimes refer to encapsulation as using a "black box," or a device that you can use without regard to the internal mechanisms.

Polymorphism

Polymorphism definition is that Poly means many and morphos means forms. It describes the feature of languages that allows the same word or symbol to be interpreted correctly in different situations based on the context. There are two types of Polymorphism available in Java. For example, in English the verb "run" means different things if you use it with "a footrace," a "business," or "a computer." You understand the meaning of "run" based on the other words used with it. Object-oriented programs are written so that the methods having same name works differently in different context. Java provides two ways to implement polymorphism.

Recursion

Recursion is a basic programming technique you can use in Java, in which a method calls itself to solve some problem. A method that uses this technique is recursive. One of the classic problems for introducing recursion is calculating the factorial of an integer.

static polymorphism

The ability to execute different method implementations by altering the argument used with the method name is known as method overloading. In below program we have three print methods each with different arguments. When you properly overload a method, you can call it providing different argument lists, and the appropriate version of the method executes.

2DArrays

The arrays you have been using so far have only held one column of data. But you can set up an array to hold more than one column. These are called multi-dimensional arrays.

Association

The association relationship is a way of describing that a class has a relationship (knows about and holds a reference) to another class through their objects. The relationship can be One to One, One to Many, Many to One and Many to Many.

Catch Block

The catch block contains code that is executed if and when the exception handler is invoked. The runtime system invokes the exception handler when the handler is the first one in the call stack whose ExceptionType matches the type of the exception thrown. The system considers it a match if the thrown object can legally be assigned to the exception handler's argument.

Try Block

contains set of statements where an exception can occur. A try block is always followed by a catch block, which handles the exception that occurs in associated try block. A try block must be followed by catch blocks or finally block or both.

Protected Scope

A variable identified as protected can be used anywhere within a package. Variable needs to be identified at class level, see code above. In this code intNumber3 is identified as public and is available everywhere inside the package.

Public Scope

A variable identified as public can be used anywhere. Variable needs to be identified at class level, see code below. In this code intNumber1 is identified as public and is available everywhere.

Method Scope

A variable identified inside a method can only be used inside that method, i.e. In the previous code intNumber1 can only be used inside the main method

Abstraction

Abstraction is about dealing with ideas rather than events.

Aggregation in Java

Aggregation is a special form of relationship or association between separate classes. .

Abstract Classes with constructors, data members and methods

An abstract class can have data member, abstract method, method body, constructor and even main() method

Abstract Class

An abstract class which contains the abstract keyword in its declaration. • Abstract classes may or may not contain abstract methods i.e. methods without body ( public void get(); ) • If a class has at least one abstract method, then the class must be declared abstract. • If a class is declared abstract it cannot be instantiated. • To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it. • If you inherit an abstract class you have to provide implementations to all the abstract methods in it.

Inheritance

An important feature of object-oriented programs is inheritance—the ability to create classes that share the attributes and methods of existing classes, but with more specific features. Inheritance is mainly used for code reusability. So you are making use of already written class and further extending on that. That why we discussed about the code reusability the concept. In general, one line definition we can tell that deriving a new class from existing class, it's called as Inheritance. You can look into the following example for inheritance concept. Here we have Mobile class extended by other specific class like Android and Blackberry.

Arrays

Arrays are programming constructs that store data and allow us to access them by numeric index or subscript. They are a lot like a CD rack. You know: one of those rectangular boxes with slots to slide CDs in, each above another. Arrays help us create shorter and simpler code in many situations as we will see in the next two examples.

Break Statement

Break: Sometimes we need to exit from a loop before the completion of the loop then we use break statement and exit from the loop and loop is terminated. The break statement is used in while loop, do - while loop, for loop and also used in the switch statement. An unlabelled break statement terminates the innermost switch, for, while, or do-while statement, but a labelled break terminates an outer statement. Below is an example of a labelled break:

Composition

Composition is a restricted form of Aggregation in which two entities (or classes) are highly dependent on each other. For e.g. Human and Heart. A human needs heart to live and a heart needs a Human body to survive. In other words when the classes are dependent on each other and their life span are same (if one dies then another one too) then its a composition. Heart class has no sense if Human class is not present.

Exception Handling

If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch block) and handle it in some rational manner. System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw.

Abstract Methods

If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract. • abstract keyword is used to declare the method as abstract. • You have to place the abstract keyword before the method name in the method declaration. • An abstract method contains a method signature, but no method body. • Instead of curly braces an abstract method will have a semi colon ( ; ) at the end.

Class Inheritance

Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order. The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

Static Variable

Java - static variable with example. A static variable is common to all the instances (or objects) of the class because it is a class level variable. In other words you can say that only a single copy of static variable is created and shared among all the instances of the classes.

for statement

Many loops consist of three operations surrounding the body: (1) initialization of a variable, (2) testing a condition, and (3) updating a value before the next iteration. The for loop groups these three common parts together into one statement, making it more readable and less error-prone than the equivalent while loop. For repeating code a known number of times, the for loop is the right choice.

method overloading

Method overloading is used to increase the readability of the program. Method overloading is performed within class. In case of method overloading, parameter must be different. Method overloading is the example of compile time polymorphism. In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.

Object Oriented Programming

Object Oriented programming is a programming style which is associated with the concepts like class, object, Inheritance, Encapsulation, Abstraction, Polymorphism. ... I have discussed Java Classes and Objects which is also a part of object-oriented programming concepts, in my previous blog.

What is the difference between a Default and Overload Constructor?

Overloading constructors. It's common to overload constructors - define multiple constructors which differ in number and/or types of parameters. For example, exact hours are common, so an additional constructor could be defined which takes only the hour parameter. You can then set to minutes to a default value

Continue Statement

The continue statement skips the current iteration of a for, while , or do-while loop. The unlabelled form skips to the end of the innermost loop's body and evaluates the Boolean expression that controls the loop. A labelled continue statement skips the current iteration of an outer loop marked with the given label

Sorting Array

The java.util.Arrays class has static methods for sorting arrays, both arrays of primitive types and object types. The sort method can be applied to entire arrays, or only a particular range. For object types you can supply a comparator to define how the sort should be performed.

Return Statement

The return statement exits from the current method, and control flow returns to where the method was invoked.

While Statements

The while statement is used to repeat a block of statements while some condition is true. The condition must become false somewhere in the loop, otherwise it will never terminate.

Aggregation

This is a special form of association which is a unidirectional one way relationship between classes (or entities), for e.g. Wallet and Money classes. It represents a Has-A relationship.

UML References

UML References Diagram (Moodle Notes)

UML Class

UML class is represented by the diagram shown below. The diagram is divided into four parts. • The top section is used to name the class. • The second one is used to show the attributes of the class. • The third section is used to describe the operations performed by the class. • The fourth section is optional to show any additional components

Method Overriding

When a child class overwrites a method inherited from a parent class. Method overriding is used to provide the specific implementation of the method that is already provided by its super class. Method overriding occurs in two classes that have IS-A (inheritance) relationship. In case of method overriding, parameter must be same. Method overriding is the example of run time polymorphism. Return type must be same or covariant in method overriding.

Constructors

When an instance of a class is first created the constructor code is called, this piece of code sets the initial / default values for the attribute variables. Initially we will program the constructor with no information being passed in, we will then program the constructor with all the information being passed in.

Procedures

When certain functionality is needed on different occasions in a program (at different point in code) a procedure can be used to avoid coding the same instructions (responsible for implementing that functionality) more than once. A procedure can be defined once, and called whenever needed.

dynamic polymorphism

When you create a subclass by extending an existing class, the new subclass contains data and methods that were defined in the original superclass. In other words, any child class object has all the attributes of its parent. Sometimes, however, the superclass data fields and methods are not entirely appropriate for the subclass objects; in these cases, you want to override the parent class members. Let's take example used in inheritance explanation.

do-while loop

When you want to test at the end to see whether something should be repeated.

Switch case statement

a switch or case statement is an n-way selector

If Statement

an if statement with no else is a one-way selector

if-else statement

an if-else statement is a two-way selector

checked exception

are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.

Unchecked Exception

are the exceptions that are not checked at compiled time. In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception. It is up to the programmers to be civilized, and specify or catch the exceptions. In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.

String sport[] = new String[6] ;

sport[0] = "Soccer"; sport[1] = "Cricket"; sport[2] = "Rugby"; sport[3] = "Aussie Rules"; sport[4] = "BasketBall"; sport[5] = "Hockey"; System.out.println("Name of the Sport in the third location " + sport[2])


Conjuntos de estudio relacionados

lesson 5: other managed products

View Set

Financial Mathematics Unit 4: Investments

View Set

2.2c Histograms and Frequency Tables for Grouped Data

View Set

unit 2: Compound Events and the Fundamental Counting Principle

View Set