Home » Posts tagged 'class'

Tag Archives: class

Classes and Objects in JavaScript

A class is a set or category of things having some property or attribute in common i.e., car is a class having similar properties such as color, engine, gear, model number etc.

The “class” construct allows us to define prototype-based classes.

Syntax for declaring a class:

Objects

Objects are the instance of classes i.e., Car is a class and Jaguar is an object of the Car class

Let’s consider a variable named person:

var person = “Sherlock Holms”;

Objects are also like variables but they can contain many values; like

this code assigns many values (Sherlock, Holms, male) to a variable named person:

var person = {name:”Sherlock”, title:”Holms”, gender:”male”};

The values are written as name:value pairs.

Object Definition

var person = {FirstName:”Sherlock”, lastName:”Holms”, age:50};

Spaces and line breaks are not important. An object definition can span multiple lines:

Object Properties

The name:values pairs in JavaScript objects are called properties:

Property Value
firstName John
lastName Doe
age 30

Accessing Object Properties

Object properties can be accessed in 2 ways:

  1. objectName.propertyName

or

2. objectName[“propertyName”]

Access Method 1

Output

Access Method 2

PHP Abstract Class

Abstract keyword is another new functionality in OOPS.

When a class declare by Abstract keyword this class cannot be instantiate. Means if user declares a class as an Abstract class he or she cannot create object of that class.

Abstract class is only use for inheriting another class. Main purpose of creating abstract class is to protected or restricted from direct object creation.

Example:

Output:

PHP Final Class

In PHP 5 a new mechanism added in OOPS which is Final Keyword.

If a class declares by final keyword then the class never extended. Child class can’t be created of any Final class.

It is restrict the overriding a method.

Only class and function may be declared as Final but no variable or properties cannot declare as Final.

Example

Output:

PHP Class and Objects

Class and objects is a programming template to write a program in a block.

It is helps to developer write complex program in simple way, reuse a program in multiple applications and create a structural program.

A class has two member one Variables and two Functions. A class is a program structure where developer defines application logic.

Object is a way which use to access class. Create an object developer access their application logic which defines in a class anywhere in a project.

Class and Object is a very useful and easy way to create a complicated application in a simple way.

Creating Class:    

Explanation:

Create a class with “class” keyword then define a class name. The name must start with string or underscore (_).

PHP is a case sensitive language so class name also case sensitive. Means Pen is write word to access this class. start PEN,  pEn, The name not the same.

Between { } define whole logic or code. Variable must be declared with var keyword or public keyword and all logic operation must be defined in function.

Creating Object

Explanation:

new keyword create object of a class. Object has authority to access the class variable and function out of the class.

In this example $obj is an object of Pen class, $obj can access $name, $price or can call the describe() function out of the class.

Object never creates within class.

Accessing Members and Functions

Output:

Explain:

Accessing class member variable and function by object. “->” this is accessing symbol.

$this variable is use to access class member variable in to the class member function. $this is object pointer, with reference an instance of class.

Java Classes and Objects

Class

A class forms the basis for object-oriented programming. It defines the shape and nature of the object.

Any concept that we want to implement in oop’s must be encapsulated within a class. A class is a template for an object and an object is an instance for a class.

In another words class is a user defined data type.

Object

An object is a run time entity which has state and behavior. Example: A dog has states – name, breed ,color, etc. and behaviors – barking, eating etc.

It is a basic unit of Object Oriented Programming and represents the real-life entities. In a java program, we can create many objects and it is an instance of a class.

How to create object

Syntex for object:   

new operator dynamically allocates  memory for an object

Write a program to demonstrate class and variables.

Result

In above program we can create an object in two lines as.                                                    

 First line declares the obj1 as a reference to an object. After this line obj1 contains a null value. Any attempt to  use obj1 at this point will result in compile time error.

Next line allocates a RectangleArea  object.

Getting id and class value of a html element

We have a div and we want to get its id value based on its class value.

Suppose a div is given as below and we want to fetch div id based on a button click.
for getting attribute id we use attr() method.

Example

Output

Managed by MochaHost - Fast & Easy WordPress Hosting