Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 264 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
The concepts of OOP in Java

#1
Like most mainstream programming languages, Java is Object Oriented. This means that Java utilizes 'objects',or data fields, that have specific procedures, or methods. An 'object', which is usually an instance of a class is used to interact with another object, or multiple other objects, to design applications and computer programs.
To understand object oriented programming, you must first understand its five basic structures:
A. The Object
B. The Class
C. Inheritance
D. the Interface
E. The Package

The Object:

An object in programming tries to mimic objects in the real world, such as a desk, a chair, a television, a bicycle, etc. This is done by mimicing the two things that every real-world object has in common, a state and a behavior.
State - name, color, breed, type, etc.
Behavior - changing the name, changing the color, changing the type, etc.

Examples of real-world states:

A. a dog's color
B. a dog's name
C. a dog's breed
D. a dog's hunger level

Examples of real-world behaviors:
A. changing the gear on a bike
B. turning off the light
C. increasing volume
D. increasing tempature

Software objects are conceptually similar to real-world objects as they too concist of states and related behaviors. Objects store their states in variables, such as Strings, chars, ints or doubles, and expose them through methods, or functions. Whenever a method accesses the internal memory of the object, it changes the state, thus, altering the behavior.

Examples of programming states:
A. String v = "Hello World";
B. int a = 9;
C. char f = 'g';
D. double d = 49.094;

Examples of programming behaviors:
A. v = "goodbye"; // this changes 'v' from "Hello World" to "goodbye"
B. a = a + a; // this changes 'a' to a (9) + a (9) (= 18)
C. f = 'd'; // this changes 'f' from 'g' to 'd'
D. d = 0; // this changes 'd' from 49.094 to 0

The above changes in state, or behaviors, radically affect how the variable acts.
For example:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


The above program would output:

> Hello World
> Goddbye

This is because we changed the state of the object 'Hello', thus, changing it's behavior.

The Class:

Now that we fully understand the object, we must understand the Class. A class is like a blueprint from which objects are created. In the real world, you'll often find many individual objects that can all be grouped together. Some examples of this would be:
A. bicycles
B. dogs
C. cats
D. Trees

Though there are different individual types of these objects, they can all be placed into one block, or in programming terms, one class. One specific example of this would be a dog.
a dog has:
A. a name
B. a type
C. an age
D. a weight
E. a height

an exmaple of a class for a dog would be:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

For some, the Java syntax will look new, the design of this class is based on the previous discussion of the Dog object. In this class, we have fields, or variables, to describe the dog. However, when the class is initialized the fields are blank. It is within the methods (changeName,changeType,changeAge,changeWeight and changeHeight) that we actually set the variables, or data fields.

In this Dog class, one may notice that a there is no main method (The method every java compiler looks for to run the program). That is because in a class like this, we would use a seperate class to call it through objects.


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


The output of this program would be:

Name:Chuck Type:bull dog Age:2 Weight:70 Height:1
Name:Joey Type:pug Age:4 Weight:30 Height:1


Inheritance:

Congratulations! You now fully understand both the Class and The Object! You are well on your way to being a pro at Object Oriented Programming! However, we must now learn about Inheritance, which privides a powerful mechanism for organizing your software.

As we stated in the Class examination, many objects can be grouped together, into one class. For example: Chevrulet, Honda, Toyota and Nisan are all different types of cars which can be grouped together under the title 'Car' because they share many of the same characteristics, i.e.Gas, wheels, radio, air conditioning, etc. However, they all have type-specific things as well. For example, some cars are automatic, some cars are stick-shift and some cars are convertable. Also, in the argument of car types, some types cost more than others, some types have more luxury than others, etc.

Example of Inheritance:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


This gives Nisan all the same methods and variables as 'Cars' but also allows it to use specific ones that only it can access. for example, the Nisan class would not be affected by the Honda class, and the Toyota class would not be affected by the Chevrulet class, etc.


The Interface:

Now that we fully understand Inheritance we can move on to another very important topic when it comes to OOP (Object Oriented Programming), the Interface.

The interface is like a contract, binding the class and the outside world. As you've already learned, objects define their real-world interactions through the methods they expose. These methods form the objects interface with the real-world. An example of a real-world interface would be the buttons on a car's radio, which would be the interface between you and the electrical wiring making up the radio.

The most common form of an interface in programming is a group of related methods with empty bodies. an example of an interface for our Dog class would be:

Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.

To implement this interface, one would make a class specific to a type of Dog (or other object, depending on what your interface is).
For example:


Hidden Content
You must

[To see links please register here]

or

[To see links please register here]

to view this content.


Note: If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

the package:


We are finally on to our last topic in Object Oriented Programming! The package, which is a namespace for organizing classes and interfaces in a logical manner.

Just as you would keep HTML files in seperate folders from images or scripts you could do the same with Java programs. This is because java software can be written with hundreds, even thousands, of seperate and individual classes which would be difficult to keep organized if they were all in the same folder. Packages act as seperate folders, better organizing your program.

Exercises:


1. Create new classes for 3 seperate types of Trees, refer to the Dog and Car methods for help.
2. For each new class that you've created above, create an interface that defines its behavior, then require your class to implement it. Omit one or two methods and try compiling. What does the error look like? Your answers will vary, depending on what methods you omit.



Congratulations! you now fully understand Object Oriented Programming and are ready to tackle it in real world situations!




Exercise Answers:

1. Answer will vary depending on types of trees you choose.
2. Error message will specifically list the required methods that have not been implemented.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through