1 / 33

Classes

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.

mostyn
Download Presentation

Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes By Cheryl Slivinski

  2. class A construct that is used as a blueprint to create objects of that class.

  3. 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.

  4. 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

  5. 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)

  6. 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

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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.

  12. User Defined Class • User defined classes combine the data and methods that operate on that data

  13. Syntax for defining a class: • accessModifiener class ClassName {

           //class definition goes here;

}

  14. 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.

  15. 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.

  16. 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.

  17. Java

  18. History • Started in 1991 • Designed for interactive TV • Called ‘Oak’ then ‘Green’ • Goal • Released in 1995 • ‘Write once, run anywhere’

  19. Types • Java SE • Java EE • Java ME or J2ME

  20. Running Java • Java Runtime Environment (JRE) • Java Virtual Machine (JVM) • Java Development Kit (JDK)

  21. Editing and Compiling • NetBeanswww.netbeans.org • Eclipse www.eclipse.org

  22. Hello World package hello; /* *by cheryl */ Public class HelloWorld { Public static void main(String[] args) { //print System.out.println (“Hello World”); } }

  23. Data Types • byte • short • int • long • float • double • boolean • char

  24. Control Structures • If/else • For • While • Do/while • Switch/case • Break • continue

  25. Operators • *,/,%,+,- • Expr++, --expr • == • != • && • || • + can be overloaded

  26. Classes <modifier> class <ClassName> { //class body }

  27. Member Variables <modifier> <type> <name> public class Car { private int speed; private string make; private static intnumberOfCars; private static final int NUMBER_OF_WHEELS }

  28. Methods <modifiers> <returnType> <methodName> (parameters)

  29. Constuctors • Are Methods • Same name as class • No return type

  30. Packages • Way of grouping related classes and level of access security cellphone cellphone.gui cellphone.database

  31. java.lang.Object • Top level class in hierarchy • Defines several methods • Ex. Public String toString()

  32. Access Modifiers • public • protected • private • No modifier

  33. 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

More Related