473,377 Members | 1,119 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,377 developers and data experts.

A quick introduction to Java concepts 3

13,262 8TB
Overriding therefore allows you to use the same method name to refer to differently implemented methods. Here is an example using the famous shapes example.

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Shape { 
  3.       public Shape() {
  4.  
  5.        }
  6.        public double getArea() {
  7.              return 5.0;
  8.        }
  9. }
  10.  
  11. class Circle extends Shape {
  12.       double radius;
  13.       public Circle(int radius) {
  14.              this.radius = radius;
  15.        }
  16.       public double getArea() {
  17.              return Math.PI * radius * radius;
  18.       }
  19. }
  20.  
  21. class Rectangle extends Shape {
  22.       int length;
  23.        int width;
  24.       public Rectangle(int length, int width) {
  25.              this.length = length;
  26.              this.width = width;
  27.        }
  28.  
  29.       public double getArea() {
  30.               return length * width;
  31.        }
  32. }
  33.  
  34.  
Here the Rectangle and Circle classes both extend from Shape and override the useless public double getArea() method in the Shape class to provide more meaningful implementations unique to each of these classes.
Access modifiers for an overriding method can allow more, but not less, access than the overridden method. i.e., a protected instance method in the super class can be made public, but not private, in the subclass. Remember that private methods are not accessible in subclasses and therefore cannot be overridden. If you want to override a method but want to use the result calculated by the super class version of the method you are overriding, then simply call that super class method using the keyword super as follows:

Expand|Select|Wrap|Line Numbers
  1. public double getArea() {
  2.      return 5 + super.getArea();
  3. }
  4.  
Here the getArea() method is being overridden but the value calculated from the direct super class is being used to come up with the new version of the overriding method.
That is pretty much all there is about overriding!
Do not confuse overloading and overriding. Overloading occurs between methods in the same class with the same name but different signatures while overriding occurs between methods in different classes with one being the subclass of the other and the methods must have the same signature and return type or covariant return types.

If class A extends class B then variables of type B can hold references to objects of type A but not vice versa. That's the English version of it, the java code version of it is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class B{
  3. }
  4. class A extends B{
  5. }
  6. then
  7.  
Expand|Select|Wrap|Line Numbers
  1. B bVariable = new B(); //obviously allowed
  2. B bVariable = new A(); //also allowed
  3. A aVariable = new A();
  4. A aVariable = new B();//! incompatible types.
  5.  
  6.  
Hiding is not overriding

Hiding occurs when a static method in a class has the same signature and return type as one in that class' super class.

Expand|Select|Wrap|Line Numbers
  1.  
  2. class B{
  3.      public static void test() {
  4.             System.out.println("test from b");
  5.      }
  6. }
  7. class A extends B{
  8.        public static void test() {
  9.               System.out.println("test from a");
  10.        }
  11. }
  12. public class Test{
  13.        public static void main(String[] args) {
  14.              B b = new B();
  15.              B a = new A();
  16.              b.test();
  17.             a.test();
  18.       }
  19. }
  20.  
  21.  
Prints
Expand|Select|Wrap|Line Numbers
  1.  test from b
  2. test from b (this is not a typing error)
  3.  
The method test is static in B and so belongs to the class.
In the main method the two variables b and a share the same class, B, and so calling a class method on them calls the same method.
Expand|Select|Wrap|Line Numbers
  1. A a = new A();
  2. a.test();
  3.  
will print test from a because now the class type is A and inside A, the test method from B is now hidden (not overridden by the test in A).
The difference with instance methods becomes clear with

Expand|Select|Wrap|Line Numbers
  1.  
  2. class B{
  3.      public static void test() {
  4.            System.out.println("test from b");
  5.      }
  6.      void testInst() {
  7.            System.out.println("testInst from b");
  8.      }
  9. }
  10. class A extends B{
  11.         public static void test() {
  12.               System.out.println("test from a");
  13.         }
  14.         void testInst() {
  15.              System.out.println("testInst from a");
  16.         }
  17. }
  18. public class ShapeTest{
  19.            public static void main(String[] args) {
  20.                  B b = new B();
  21.                  B a = new A();
  22.                  b.test();
  23.                  a.test();
  24.                  b.testInst();
  25.                  a.testInst();
  26.         }
  27. }
  28.  
Use: In one sentence, use overriding to achieve more particular implementations of methods inherited from super classes.

abstract keyword

A class can be declared abstract meaning that no objects can be created using that class.

Expand|Select|Wrap|Line Numbers
  1.  abstract class A {
  2. }
  3. A a = new A();//!compile time error.
You may be beginning to think that an abstract class is quite useless. That's only until inheritance comes in. You can inherit from an abstract class allowing you to create a class that can be instantiated. The idea of making a class abstract is that you have a general group which objects belong to but instantiating an object of the general group really does not make sense since every object belongs to a more particular group. This is the case with the shapes example. The class Shape should be made abstract because a shape can be identified more precisely that just by calling it a shape. So
Expand|Select|Wrap|Line Numbers
  1. abstract class Shape {
  2. }
  3. class Circle extends Shape {
  4. }
  5.  
is more meaningful.

Methods can be declared abstract as well in which case you don't provide the method body but leave that to inheriting subclasses. In the shapes example you know that
all shapes have a getArea method but cannot write any code for it because the implementation is different for each shape so declare the getArea() method in the shape class
because that's where it belongs (all shapes have a getArea method) but you leave it abstract because you don’t know how to write it yet.

Expand|Select|Wrap|Line Numbers
  1. abstract class Shape {
  2. public abstract double getArea();
  3. }
  4.  
Note that no {} are used. Just the signature and the ; at the end.
If a class contains an abstract method then that class is implicitly abstract and must the declared abstract otherwise the compiler will complain.
If a class inherits from an abstract class, then that class must provide implementations of all the abstract methods in the super class otherwise that inheriting class must also be declared abstract.
These things are not at all abstract and you should try them out on the compiler to see the effects.

Interfaces

An interface is a reference type whose members can be classes, other interfaces, constants and abstract methods.
They differ from classes in that they have no implementation. This is obvious since all their methods are abstract. Therefore all interfaces are abstract but you should not
put the public modifier. All interface members are implicitly public. Basically an interface is a contract that classes can choose to sign. Classes sign this contract by implementing the interface using the implements keyword.
Here are two example interfaces

Expand|Select|Wrap|Line Numbers
  1. interface Shape {
  2. double getArea();
  3. }
  4. interface Drawable {
  5. void draw();
  6. }
  7.  
These interfaces each contain one method. If any class implements the Shape interface then it must provide an implementation of the getArea() method otherwise that class must be declared abstract.
It is really as simple as that. Implement or else you're abstract, that's the rules of the contract. The interface is the boss. All this is checked at compile time.
Interfaces can declare constants as well which become available in implementing classes. A class can implement any number of interfaces. You can use this to achieve some level of multiple inheritance in Java by implementing multiple interfaces and thus being able to characterize an object into multiple groups. Here is an example. We want to be able to say that a Circle is a Drawable object and also that a Circle is a Shape.
Extending the Shape class says that a Circle is a shape. Extending the Drawable class says a Circle is a Drawable object. But we can only inherit from one class at a time so we make one of them an interface, implement it and
extend the other or we make both of them interfaces and implement them. The decision is ours, however, interfaces do not really provide any code reuse because they don't contain any implementations so it's wiser to choose a method that allow us the best in terms of code reuse. What interfaces provide is polymorphism. One interface, multiple implementations. By defining the Drawable interface, for example, we can implement it differently in all the classes that choose to implement this interface. If we had one method in some class draws shapes, we would simply need to pass an interface variable and call its draw method.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. class Painter {
  4. void drawShape(Drawable d) {
  5. d.draw();
  6. }
  7. }
  8.  
  9.  
Now we can create classes that extend Shape and implement Drawable. In these classes since we have implemented Drawable, we then provide customized implementations of the draw method. If we wish to draw any shape, we pass it to the Painter's drawShape method which calls the draw method for that particular shape. When we change the draw method for the Circle class, the Painter is not affected, neither is any of the other shapes. This is one of the main goals of OOP. To be able to create a design which minimizes effects of changes in parts of the system. The classes in the Java packages make unforgiving use of these principles to define classes that can be used in varied circumstances by programmers.
Feb 5 '07 #1
0 15377

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: inquirydog | last post by:
Hello- I, the inquirydog, would like to solicit suggestions for a new web page I am making: I am creating a simple website that will translate concepts between windows os's, Linux, and the...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
29
by: Ralathor | last post by:
Hello everyone. I just wanted to introduce myself, and maby get a few of you... Well, anyway... I am pretty new to programming... At first i looked at the "python" language for a couple...
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
0
RedSon
by: RedSon | last post by:
Chapter 2: How to handle Exceptions When you call a program, sometimes you get a message about an Unhandled exception type. This means, that something throws (or might throw) an Exception, but the...
0
RedSon
by: RedSon | last post by:
Chapter 3: What are the most common Exceptions and what do they mean? As we saw in the last chapter, there isn't only the standard Exception, but you also get special exceptions like...
29
by: Martin Schmied | last post by:
Dear folks I'm looking for good sites/material for this topic: Introduction to programming, using Javascript. So, not introduction to programming *in" Javascript. Well, I guess this would be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.