Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes in Java Specifics for Java Copyright 1998-2005 Curt Hill.

Similar presentations


Presentation on theme: "Classes in Java Specifics for Java Copyright 1998-2005 Curt Hill."— Presentation transcript:

1 Classes in Java Specifics for Java Copyright Curt Hill

2 The Class Declaration public class x{ … }
A public class x must be in a file x.java Every application must be a public class Every applet must be a public class The class defines properties and methods Copyright Curt Hill

3 Properties Also known as fields The variables of a class
May be primitives or objects Belong to class and are instantiated with the class Copyright Curt Hill

4 Properties and Visibility
Variables may be declared using any visibility Protected, which gives access to derived classes but not clients Friendly visibility is also very commonly used Private is usually only used on classes that will be superclasses Public gives client access and is seldom used Copyright Curt Hill

5 Methods and Visibility
In functions the preferred visibility is public Friendly is also common Protected and private are not as frequent but have their uses Copyright Curt Hill

6 Java Files A file with a .java extension must contain one and only one public class It must have same name as file It may also contain other classes of friendly (but not private) visibility outside the public class These are put in file in any order The friendly classes function as helper classes for the public class Copyright Curt Hill

7 Order of declaration In C++ items could be declared almost anywhere
The declaration had to precede the use In Java a class can be used before its declaration Provided declaration is in the same file However an include provides declaration as well Copyright Curt Hill

8 Class derivation A class that is only slightly different from another class would be easier to derive that start over or copy The keyword is extends and is used in every applet public class newclass extends java.applet.Applet Copyright Curt Hill

9 Extends This keyword states that the new class starts with every property and method of the old class Applet is the super class or base class newclass is the sub class or derived class Copyright Curt Hill

10 The Derived Class This new class changes the functionality in some way
It may add new properties or methods It may overrides any or all old methods Those methods left alone continue to function Copyright Curt Hill

11 Accessing the Super class
If method X of the super class has been overridden it is still accessible Call function super.x(…) Using the super class’s constructor is just super(…) Copyright Curt Hill


Download ppt "Classes in Java Specifics for Java Copyright 1998-2005 Curt Hill."

Similar presentations


Ads by Google