Object-oriented concepts in practice in Java

--Originally published at Alan TC201

Here is a sum of all that I have published, the Journey for the Java programming its just starting, I am still learning every concept.

And to entretaing you, let’s see some hype.

  • Object-oriented concepts in practice in Java

 


Object-oriented concepts in practice in Java

Masteir: mechanics for class description

--Originally published at Alan TC201

Masteir: mechanics for class description

 

 

 

 

 

 

 

 

Java Program:  a set of instructions for the computer to follow.  The source code.

Data:  data for the Java program

Java Compiler:  translates the program from Java to a language that the computer can understand.  Compilers differ by make of machine and operating systems.

Byte Code Program:  the compiler translates Java into a language called byte-code.  Byte code is the machine language for a hypothetical computer called the Java Virtual Machine.

Byte Code Interpreter:  the interpreter translates each instruction of byte code into instructions that the computer in use can execute.

Machine Language:  is the language the computer in use understands.

Info (http://gee.cs.oswego.edu/dl/cpj/mechanics.html)

 

Masteir: mechanics for class description

 

 


Masteir: mechanics for class description

Masterie ”has-a” and “is-a” .

--Originally published at Alan TC201

One of cool thing you can do in Object-Oriented programming language is to reuse a code. There are two ways we can do code reuse, by implementation of inheritance or object composition. But you need to be careful how and what method you will use depending if it IS-A relationship or HAS-A relationship.

Masterie ”has-a” and “is-a” .

This image is very clear, but if you still can get the concept I will explain using the same example. You have a class with specifications about cars, like they uses some type of combustible, they move, and have doors, then you have a class name Maruti (a type of car) well that class is a relationship between the class car, everything inside the class car is valid for the class maruti.

Masterie ”has-a” and “is-a” .
On the other hand we have another class name engine, the maruti class HAS a relationship with the class engine because there are many types of engine but the maruti only cares about its engine, at the same time the engine has a relationship with the murati because that car needs an engine but the engine class does not care about the doors or of the car moves.

Masterie ”has-a” and “is-a” .

Down here are more specifications if you still are having trouble getting this.

IS-A relationship

  • This refers to inheritance or implementation.
  • Expressed using keyword “extends”.
  • Main advantage is code reusability.

In object oriented programming, the concept of IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. It is just like saying “A is a B type of thing”. For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-directional. For example House is a Building. But Building is not a House.

It is key point to note that you can easily identify the IS-A relationship. Wherever you see an extends keyword or implements keyword in a class declaration, then this class is said to have IS-A relationship.

HAS-A relationship
Composition(HAS-A) simply mean use of instance variables that are references to other objects. For example: Maruti has Engine, or House has Bathroom.

  • Has-A means an instance of one class “has a” reference to an instance of another class or another instance of same class.
  • It is also known as “composition” or “aggregation”.
  • There is no specific keyword to implement HAS-A relationship but mostly we are depended upon “new” keyword.

More info (http://learnwithharsha.com/my-first-java-lesson-is-a-and-has-a-relationships/)

And a video that explains everything with details.


Masterie ”has-a” and “is-a” .

WSQ06 – Greatest Common Divisor

--Originally published at Alan TC201

In this assignment I was rquired to write a program that calculates the greatest common denominator of two positive integers using Euclid’s algorithm.

If you do not know what Euclid’s algorithm is, here’s a page that explains it. http://www.cut-the-knot.org/blue/Euclid.shtml
WSQ06 – Greatest Common Divisor

 

This is “officially” the second homework and I am still fresh with this of program in Java, this may be easy because I did the same WSQ in the last semester in python. The trick here should be to implement this with message calls, not a simple iterative solution. This must be more oriented programing and nothing like “Python style”, now it is time for JAVA to shine:

WSQ06 – Greatest Common Divisor

This is my second language I am learning and the first object oriented, and I gotta say it is very different, python is very stylish and easy to understand the basics in less than 20 minutes. But Java is different…. very…. very different, just to do a hello world was very weird. This is a new experiences and I hope I can suprius it like in the last semester. The new thing I use in this WSQ was the “input” to give the machine the numbers to fiend the GCD. Video of how to do it. (https://www.youtube.com/watch?v=D3AYFeLjJmU)

WSQ06 – Greatest Common Divisor

And here is the github: https://github.com/alanoswalt/GCD/blob/master/redme.java


WSQ06 – Greatest Common Divisor

Masterie: UML Class Diagrams

--Originally published at Alan TC201

Class Diagrams are usful in diferent situations, for example.

Graphical way to illustrate relationships between classes in an object-oriented system.

Appearance varies between different drawing tools.

Round-trip tools are able to generate UML class diagrams from existing code, and generate template code from existing diaframs.

Masterie: UML Class Diagrams

“UML is popular for its diagrammatic notations. We all know that UML is for visualizing, specifying, constructing and documenting the components of software and non software systems. Here the Visualization is the most important part which needs to be understood and remembered by heart.

UML notations are the most important elements in modeling. Efficient and appropriate use of notations is very important for making a complete and meaningful model. The model is useless unless its purpose is depicted properly.

So learning notations should be emphasized from the very beginning. Different notations are available for things and relationships. And the UML diagrams are made using the notations of things and relationships. Extensibility is another important feature which makes UML more powerful and flexible.

The chapter describes the UML Basic Notations in more details. This is just an extension to the UML buildling block section I have discussed in previous chapter.” (http://www.tutorialspoint.com/uml/uml_basic_notations.htm)

Structural Things:
Graphical notations used in structural things are the most widely used in UML. These are considered as the nouns of UML models. Following are the list of structural things.

Classes
object
Interface
Collaboration
Use case
Active classes
Components
Nodes

Masterie: UML Class Diagrams

More info: http://www.tutorialspoint.com/uml/uml_basic_notations.htm

http://www.lynda.com/Java-tutorials/Diagramming-UML/375490/415273-4.html


Masterie: UML Class Diagrams

Masterie: CRD cards

--Originally published at Alan TC201

CRC cards are a way to se program object orientated.

Masterie: CRD cards

CRC cards are 4 by 6 index cards used in the design of object oriented
programs.  The initials CRC stand for “Class, Responsibility and Collaboration”.
Normally, a significant program written in an object oriented language like Java
is divided into several different classes.  These classes cooperatively work
together to accomplish the goals of the program.

Masterie: CRD cards
It can be difficult to understand how to divide a program into separate
classes, and how to divide a given class into separate methods. CRC cards help in this design process and organize in a very schematic way.

Masterie: CRD cards

This video explains everything in an easy way to understand.


Masterie: CRD cards

Masterie: What is delegation?

--Originally published at Alan TC201

Inheritance”
Other term that plays in Object-Oriented programming is inheritance”, this term tell us that we can “link” or transfer different methods between different classes that have similarities in them and use the same methods besides they use particularly methods. This helps to not duplicated methonds in diferent clases that have methods in commun.

If we couldn’t use Inheritance we will have to specify each method in each class we will have to explicit define each method in each class individually.
Multiple Inheritance means that a class have common things between two classes but those two classes don’t have Inheritance between them.

BUT BE CAREFUL, YOU DON’T CIRCULATE AN INHERITANCE, this means that a class cannot inheritance from itself, like a circle.

Overriding means that you could have different classes chain together and inheritance from the top one. For example, we have class, and we have a method call MON in the class V, normally all the classes below but if we have a method also call MON in the X class the the classes below X will inheritance the MON from the x class.

V – MON
W – MON FROM CLASS V
X – MON
Y – MON FORM CLASS X
Z – MON FROM CLASS X

Intarface is a set of methods that can have commun things inside different classes.

Masterie: What is delegation?

Inheritence uses the java compiler’s type system to implicitly include code from elsewhere, with delegation you explicitly implement the callout.

Inheritance is limited to a single parent class (and ancestors) but it can sometimes be non-obvious where your code is going; delegation is less elegant, but you can bring in functionality from multiple other classes and the flow is clearly visible.

As far as invoking external functions is concerned, delegation is generally preferred for just this reason, inheritance should be reserved for cases where you want to implement a class whose behaviour specifically is like the base-class, so it can essentially be treated as an instance of the baseclass by other code (ie. it is polymorphic).” (http://stackoverflow.com/questions/13245610/what-is-difference-between-inheritance-and-delegation-in-java)

 


Masterie: What is delegation?

Masterie: What is encapsulation?

--Originally published at Alan TC201

“Encapsulation”
Now after all this information we can dig more in terms of Java programing, first of all, the term “encapsulation”.
This means that, to have a better program, that is easy to read, to understand and fix the classes need to be private, so that the methods of a class only affect the data or the objects inside that class.

The encaptulation is used whent you have two packages with objects, clases and methods and you do not want that one package know the variables of the other package.

For example, here I have two diferent packages one that has the spesifications, the other one has the methods.

Masterie: What is encapsulation?

 

Masterie: What is encapsulation?

But when I change my methods to privite the encapsulated class could not recive inmormation from the Persona class.

Masterie: What is encapsulation?

Here is different info about encaptulation: http://www.lynda.com/Java-tutorials/Understanding-encapsulation/377484/421339-4.html

Masterie: What is encapsulation?


Masterie: What is encapsulation?

Masterie: What is abstraction?

--Originally published at Alan TC201

Abstraction in Java is like for example talking about animals, this is abstract because you do not know the context of that animal, it can be a cat, dog or a monkey, but you only know that it is animal.

Masterie: What is abstraction?

Abstraction has the same proces of hiding the implementation details from other people, only the functionality will be provided to the user. In other words user will have the information on what the object does instead of how it does it. Or in other words is telling the people the basic, hiding all the complex process that it requires.

In Java you can create an Abstraction using Abstract classes and Interfaces.

“Abstract Class

  • A class which contains the abstract keyword in its declaration is known as abstract class.
  • Abstract classes may or may not contain abstract methods ie., methods with out body ( public void get(); )
  • But, if a class have 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.” (http://www.tutorialspoint.com/java/java_abstraction.htm)

Masterie: What is abstraction?

Taking the example of the animals again, you can have a class name Animal and that class is going to be abstract, it has inside methods that all animals do, they eat and an abstract method that says that animals move.

Masterie: What is abstraction?

Then you have another class name Dog that extends from the animal class so the Dog class has in it the method eat, because all animals eat, but there is also an abstract method that the dog class need to verify, the move method, because not all animals move the same way, ones fly, otherones swim and dogs walk in four legs, so the method move this to be verify in the Dog class.

Masterie: What is abstraction?

Now that you have the dog class in is possible to create another class, that extends from the animal class, this new class is going to be a bird, birds can eat, but they move by air while using their wings.

Masterie: What is abstraction?

Finally the main class that controls the other classes and send them inputs to receive outputs.

Masterie: What is abstraction?

Links with more info:
https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
http://www.tutorialspoint.com/java/java_abstraction.htm
http://www.lynda.com/Java-tutorials/Using-abstract-classes-methods/377484/421352-4.html

 


Masterie: What is abstraction?