Category Archives: OOP

OOP Topics Need To Cover

https://www.javacodegeeks.com/2017/05/oops-concepts-need-know.html

OOP in C#

Calculator.cs

Program.cs

Person.cs

 

SOLID Concepts

Object Oriented Programming Concepts

OOP Clear

http://jompesh.com/videos/list/category/76

PHP OOP Project

Basic Simple Project By Me:

By Others:
From OOP Fundamentals udemy Video project:

 

PHP OOP: Class, Method, Object, Instance, Declare, Echo, Inheritance,Constructor,Access Control,Static!

PHP OOP example described here from edwin’s video:

Inheritance example:

Constructor:

Access control: Public, Private, Protected

Static Data in Class:

 

Java OOP Examples

https://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.html

PHP OOP Practice

Without Constructors:

class_lib.php

index.php

With constructors:

class_lib.php

index.php

With modifiers:

class_lib.php

index.php

With Inheritance:
class_lib.php

index.php

Overriding methods:

 

 

 

 

PHP OOP basics

Reference:

Object Oriented PHP for Beginners: Page 4

Java Tut – Packages

Packages are used in Java in order to prevent naming conflicts,to control access, to make seraching/locating and usageof classes,interfaces,enumberations,annotations easier etc.

some of the existing packages in Java are:Java.lang – bundles the fundamental classes
java.io – classes of imput, output functions are bundled in this package
example:
http://www.tutorialspoint.com/java/java_packages.htm

Java Tut – Interface

Interface is a collection of abstract methods.A class implements an interface, thereby inheriting the abstract method of the interface.
Declaring Interafces:

interface keyword is used to declare an interface.simple example:

Implementing/Declaring Interfaces:
– An interface is implicitly abstract.You do not need to use the abstract keyword while declaring an interface.
– Each method in an interface is also implicitly abstract, so the abstract keyword is not needed.
– Methods in an interface are implicitly public.

example:
Animal.java

Implementing Interfaces:
When a class implements an interface, you can think of the class as signing a contract, agreeing to perform the specific of the behaviors of the iterafce, the class must declare itself as abstract.

A class uses the implements keyword to implement an interface.Thje implements keyword appears in the class declaration following the extends portion of the declaration.
example:
MammalInt.java

Output:

When overriding methods defined in interfaces there are several rules to be followed:
– Checked exceptions should not be declared on implementation methdos other than the ones declared by the interface method or subclasses of those declared by the interface methods.

– The signature of the interface method and the same retrun type or subtype should be maintained when overriding teh methods.

– An implementation class itself can be abstract  and if so interface methods need not to implemented.

When implementation interfaces there are several rules:
-A class can implement ore than one interface at a time.
– A class can extend only one class,but implement many interfaces.
-An interface can extend another interface,similarly to the way that a class can extend another class.

Extending Interfaces:

An interface can extend another interface, similarly to the way that a class can extend another class.The extends keyword is used to extend an intrerface, and the child interface inherits the methods of the the parent interface.

The following sports interface is extended by Hockey and Footbal interfaces.
Filename: Sports.java

The hockey interface has four methods, but it inherits two from Spors;
thus a class that implements Hockey need to implement all six methods.Similarly, a class that implements Football need to define the three methods from Football and the two methods from Sports.

Extending Multiple Interfaces:
An interface is not class so Interface can extend more than one parent interface.
example:

Tagging Interfaces:
the MouseListener interface in the java.awt.event package extended java.util.EventListener, which is:

An interface with no methods in it is referred to as a tagging interface.There are two basic design purposes of tagging interfaces
Creates a common parent:

Adds a data type to a class:

Reference:
http://www.tutorialspoint.com/java/java_interfaces.htm

 

Java Tut – Aggregation/HAS-A relationship

code:

reference:
http://www.javatpoint.com/aggregation-in-java

Java Tut – Encapsulation

Encapsulation is one of the four fundamental OOP concepts.The other three are inheritance,polymorphism and abstraction.

it is a mechanism of wrapping the data variables and code acting on the data together as single unit. In encapsulation teh variables of a class will be hidden from other classes and can be accessed only through the methods of their current class, therefore it is also known as data hiding.
To achieve-

  • Declare the variables of a class as private.
  • Provide public setter and getter methods to modify and view the variables values.

code:
EncapTest.java

the public setXXX() and getXXX() ethods are the access points of the instance variables of the EncapTest class.Normally these methods are referred as getters and setetrs.Therefore any class that wants to access teh variables should access them through these getters and setters.

RunEncap.java

output:

 

 

 

Java Tut – Abstraction

In OOP abstraction is a process of hiding the implementation details from the use, 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.

In java abstraction is achieved 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 o all abstract methods in it

Example:

and from another class

It will show error because of abstract class:

Abstract Method:

Declaring a method as abstract has two consequences:

  • The class containing it must be declared as abstract.
  • Any class inheriting the current class must either override the abstract method or declare itself as abstract.

Eventually a descendant class has to implement the abstract method; otherwise you would have a hierarchy of abstract classes that cannot be instatiated.

Suppose salary class is inherits the Employee class, then it should implement the computePay() method as shwon below:
//will add the code later

references:
http://www.tutorialspoint.com/java/java_abstraction.htm