Java Classes                 

   Contact
   C
   C++
   Visual Basic
   Java
   JavaScript
   DHTML
   Style Sheets
   About
   Normalization
   Active X
   TDC Binding
   PHP
   Perl and CGI
   Flash
   XML
   SQL
   Chat
   MCSE
   Linux
   Cabling   
 

   
 
    
    
Java 2  Java 3  Supplementary  Events  AWT Project  AWT Project  Language  Swing  Sound/Images


Classes in Java provide a way to further modularize and organize your code.  Like C++ classes, Java classes are designed to encapsulate real world objects. They combine attributes of an object (data members or variables) with functions and methods of an objects. In this fashion, unlike old C structures, C++ and Java classes combine what an object is, has and does into a single entity. Classes in both C++ and Java practice "inheritance". In C++, you have base classes and derived classes and you declare a class to inherit from another with "class CHILD : public PARENT". In Java, you use the keyword "extends" to do the same thing. Example:
"public class CHILD extends PARENT". One aspect in which Java is different than C++ is that unlike C++, Java will allow you to nest classes inside of each other.

Example 1 - Declaring a Class:

import javax.swing.*;

public
class CreatingAClass
{
      public void main(String[] args)
      {


             System.exit(0);

      }
//close main() function
 
}
//close CreatingAClass class

Example 2 - Class Inheritance:

import javax.swing.*;

public
class ParentSuperClass
{
      public void main(String[] args)
      {
             System.exit(0);
      }
//close main() function
 
      public void DoParentStuff() { }

}
//close ParentSuperClass class
 

public class ChildSubClass extends ParentSuperClass
{
      public void main(String[] args)
      {          
             System.exit(0);
      }
//close main() function
 
      public void DoChildStuff() { }

}
//close ChildSubClass class

Note: To instantiate an object of the classes defined above you would use the keyword "new". They don't call it a "pointer" in Java, but you can tell that's exactly what it is under the hood when you see the code:

Java (no pointers):   ChildSubClass CHILD = new ChildSubClass();
C++ (pointers)         ChildSubClass * CHILD = new ChildSubClass(); 

In the C++ example, you are creating a new object and allocating memory for it on the heap (free store) and using a pointer to access the memory address that references the object. In the Java example, they simply define this method as the way to instantiate an object.
As objects created on the stack in C++, Java objects access their methods and functions via the dot operator. Example:

ParentSuperClass PARENT = new ParentSuperClass();
PARENT.DoParentStuff();

ChildSubClass CHILD = new ChildSubClass();
CHILD.DoChildStuff();

As in nature, in C++ and Java a child object inherits the data members, functions and methods of its parent class. So you may:

CHILD.DoParentStuff();

Java 2  Java 3  Supplementary  Events  AWT Project  HOME  AWT Project  Language  Swing  Sound/Images