Home arrow Java & J2EE arrow Page 3 - Java and Object-Oriented Programming

Class, Inheritance and Interface - Java

In the olden days, not only did we have to walk a mile in the chilling winds of a snowstorm to get to school (hey our grandparents had it rougher; they had to do it to get to day care), we also had to make programs without buttons and scrollbars. Now, of course, we have object-oriented programming. This article will introduce you to the most important concepts as they relate to Java.

TABLE OF CONTENTS:
  1. Java and Object-Oriented Programming
  2. How State and Behavior Affect One Another
  3. Class, Inheritance and Interface
  4. All About Variables
By: James Payne
Rating: starstarstarstarstar / 15
September 19, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Working with Class

While an object represents one specific thing, a class represents a category of things. For example, a Ford dealership has many cars, all similar in make and model. If you were to go to one, you would notice they all have the same basic structure. Four tires, a couple of doors, a horn that unfortunately does not sound like the one in the Dukes of Hazard. You would say all of these Fords are in the same class.

But suppose you took one home. Maybe you added a DVD player to it, or a set of rims so high you could run over people without actually hitting them. Maybe you give this movie-playing monster truck a name. Herbert, why not? You no longer refer to it as a Ford, or its class, but instead as an individual object.

Class allows for uniformity within a program. All objects within a certain class, typically, should carry the same core properties. You wouldn't want to use a program where every button was a different color and size and behaved in an entirely new and wonderful way.

Unless you were Willy Wonka, in which case, you'd be better off sticking to chocolate.

Inheritance

In the above example we used Ford as a class of cars, and Herbert the Monster Truck as a special instance of a Ford, or an object. In addition to these two specifications, there is also a superclass. In the case of Ford, the superclass would be Cars and Trucks.

With inheritance, the subclass literally inherits certain qualities of the superclass. For instance, all Cars and Trucks drive on roads, have four tires, an engine, lights, etc. So therefore, all Fords inherit those same characteristics. That isn't to say that Fords can't have any different features; they can and do. But at the core, they are the same.

In programming this is done so that the programmer does not have to continually program objects and classes over and over. Instead, he can simply code it once and know that every time he creates an object, he will not have to worry about its core properties.

Interface

While an object is the abstract representation of everyday objects, the interface is its physical representation. This could be the buttons in a program, scrollbars, drop-down menus, etc. Simply put, the interface is the method by which you change the state and initiate the behavior of an object.

A good example would be the key to your car. You insert it into the ignition, which changes the state (sends electricity to the starter) and initiates the behavior change (starts the car).



 
 
>>> More Java & J2EE Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: