330 likes | 456 Views
Classes. By Cheryl Slivinski. class. A construct that is used as a blueprint to create objects of that class. classes. This blueprint describes the state and behavior that the objects of the class all share. An object is also known as an instance of the class. Qualifiers.
E N D
Classes By Cheryl Slivinski
class A construct that is used as a blueprint to create objects of that class.
classes • This blueprint describes the state and behavior that the objects of the class all share. • An object is also known as an instance of the class.
Qualifiers • Public: access to all classes • Private: access only to the class to which they belong. • Protected: access only to the class to which they belong, and any subclasses
Methods • a method is a subroutine that is exclusively associated either with a class (in which case it is called a class method or a static method) or with an object (in which case it is an instance method)
Methods • A constructor method, supported by many languages, is called automatically upon the creation of an instance of a class. Some languages have a special syntax for constructors. • In Java, C++, C#, ActionScript, and PHP constructors have the same name as the class of which they are a member
Methods • A destructor method (i.e. a special instance method that is called automatically upon the destruction of an instance of a class), is implemented in some languages. • A destructor method (i.e. a special instance method that is called automatically upon the destruction of an instance of a class), is implemented in some languages.
Methods • A destructor method (i.e. a special instance method that is called automatically upon the destruction of an instance of a class), is implemented in some languages.
Methods • An accessor method is a method that is usually small, simple and provides the sole means for the state of an object to be accessed (retrieved) from other parts of a program.
Methods • An abstract method is a dummy code method which has no implementation. It is often used as a placeholder to be overridden later by a subclass of or an object prototyped from the one that implements the abstract method. In this way, abstract methods help to partially specify a framework.
Methods • An Instance method, in a typical implementation, is passed a hidden reference (e.g. this, self or Me) to the object (whether a class or class instance) it belongs to, so that it can access the data associated with it.
User Defined Class • User defined classes combine the data and methods that operate on that data
Syntax for defining a class: • accessModifiener class ClassName { //class definition goes here; }
Encapsulation • Encapsulation conceals the functional details of a class from objects that send messages to it. • Encapsulation is achieved by specifying which classes may use the members of an object. The result is that each object exposes to any class a certain interface — those members accessible to that class. The reason for encapsulation is to prevent clients of an interface from depending on those parts of the implementation that are likely to change in the future, thereby allowing those changes to be made more easily, that is, without changes to clients.
Inheritance • Inheritance allows the programmer to treat derived class members just like their parent class's members. This type of relationship is called child-Parent or is-a relationship. "Subclasses" are more specialized versions of a class, which inherit attributes and behaviors from their parent classes, and can introduce their own.
Polymorphism • Polymorphism is a process in which a class has all the state and behavior of another class.More precisely, Polymorphism in object-oriented programming is the ability of objects belonging to different data types to respond to calls of methods of the same name, each one according to an appropriate type-specific behavior. One method, or an operator such as +, -, or *, can be abstractly applied in many different situations. If a Dog is commanded to speak(), this may elicit a bark(). However, if a Pig is commanded to speak(), this may elicit an oink(). Each subclass overrides the speak() method inherited from the parent class Animal.
History • Started in 1991 • Designed for interactive TV • Called ‘Oak’ then ‘Green’ • Goal • Released in 1995 • ‘Write once, run anywhere’
Types • Java SE • Java EE • Java ME or J2ME
Running Java • Java Runtime Environment (JRE) • Java Virtual Machine (JVM) • Java Development Kit (JDK)
Editing and Compiling • NetBeanswww.netbeans.org • Eclipse www.eclipse.org
Hello World package hello; /* *by cheryl */ Public class HelloWorld { Public static void main(String[] args) { //print System.out.println (“Hello World”); } }
Data Types • byte • short • int • long • float • double • boolean • char
Control Structures • If/else • For • While • Do/while • Switch/case • Break • continue
Operators • *,/,%,+,- • Expr++, --expr • == • != • && • || • + can be overloaded
Classes <modifier> class <ClassName> { //class body }
Member Variables <modifier> <type> <name> public class Car { private int speed; private string make; private static intnumberOfCars; private static final int NUMBER_OF_WHEELS }
Methods <modifiers> <returnType> <methodName> (parameters)
Constuctors • Are Methods • Same name as class • No return type
Packages • Way of grouping related classes and level of access security cellphone cellphone.gui cellphone.database
java.lang.Object • Top level class in hierarchy • Defines several methods • Ex. Public String toString()
Access Modifiers • public • protected • private • No modifier
Naming Conventions • packages ex package mypackage • classes ex class MyPackage • methods ex private void myPackage • variables ex intmyPackage • Constants ex static final char MYPACKAGE=12