It is currently Thu Mar 28, 2024 3:42 am



Reply to topic  [ 54 posts ]  Go to page Previous  1, 2, 3, 4  Next
anyone here do Java? 
Author Message
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Nothing special. But you can learn inheritance (classes) together with implementation (interfaces). One class can ihnerit only one other class and not two or more. As well, all the public methodes from the parent classes would be accessible as public methods of the children class Private methodes would be accessible only in the parent class. Any of children would be unable to access them. At end, there are protected methodes, which will be accessible in the children classes but only if the object call them. Not from outside.

Code:
class parent {
   private void aa()
   {}
   
   protected void bb()
   {}
   
   public void cc()
   {}
}

class child extends parent {
   public void f()
   {
      // aa(); //! Won't work
      bb(); // Work
   }
}

public class test {

   static public void main(String s[])
   {
      child a = new child();
      
      // a.bb() // won't work
      a.cc(); // works      
   }
}


A children class can as well override a method from a parent class. See the example:

Code:
class parent {
   public void a()
   { System.out.println("from parent"); }
   
   public void b()
   { System.out.println("from parent"); }
}

class child extends parent {
   public void b()
   { System.out.println("from child"); }
}

public class test {

   static public void main(String s[])
   {
      child a = new child();
      a.a(); // a() from parent
      a.b(); // b() from child
   }
}


One class can inherit only one other class but it can implement as much interfaces as you like. One interface is just a set of function declarations. All functions from implemented interfaces must be implemented in the class. Otherwise it won't work.

Code:
interface int1 {
   void a();
}

interface int2 {
   void b();
}

interface int3 {
   void c();
}

class implementation implements int1, int2, int3
{
   public void a()
   {}
   
   public void b()
   {}
   
   public void c()
   {}
}


At end, you can implement and extend at the same time:

Code:
interface int1 {
   void a();
}

interface int2 {
   void b();
}

interface int3 {
   void c();
}

class parent {
   public void d()
   {}
}

class child
   extends parent
   implements int1, int2, int3
{
   public void a()
   {}
   
   public void b()
   {}
   
   public void c()
   {}
}


One question could be "why would I need inheritance when by implementing I can declare any function I wish?".

First the functions from an interface are just declarations. Programmer is not allowed to define bodies of them inside the interface. E.g. the following example would be false

Code:
interface int3 {
   void c()
   {}
}

There must not be body.

Second, when classes child1 and child2 inherit the class parent, you can set them in the same array of parent's type and use them. Although they are hold by a pointers of type parent, child1 will behave as child1 and child2 will behave as child2.

Code:
class parent {
   public void a()
   { System.out.println("from parent"); }
   
   public void b()
   { System.out.println("from parent"); }
}

class child1 extends parent {
   public void b()
   { System.out.println("from child"); }
}

class child2 extends parent {
   public void a()
   { System.out.println("from child"); }
}

public class test {

   static public void main(String s[])
   {
      parent arr[] = new parent[2];
      
      arr[0] = new child1();
      arr[1] = new child2();
      
      System.out.println("--- Child 1 ---");
      arr[0].a(); // a() from parent
      arr[0].b(); // b() from child
      
      System.out.println("\n--- Child 2 ---");
      arr[1].a(); // a() from child
      arr[1].b(); // b() from parent
   }
}


Of course one limit is that from pointer of type parent you can call only the methods which are defined/declared in parent, but if e.g. children3 had its characteristic method and you know it is a child3 object, you can cast the pointer parent to child3. See the example:

Code:
class parent {
}

class child3 extends parent {
   public void f()
   { System.out.println("from child"); }
}

public class test {

   static public void main(String s[])
   {
      parent a = new child3();
      
      // a.f(); //! This won't work
      
      ((child3)a).f(); // But this will
   }
}

_________________
++


Fri Apr 21, 2006 12:38 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Thanks for the 1337 tips GFree.

Heres another problem:

Ive started to write a program that will be used as a super-class eventually. It needs to take integers from the "user" so that it has row and column lengths for a 2d array:

Code:
import javax.swing.*;
public class Matrix
{
    private String rowLength;
    private String colLength;
    public void initMatrix()
    {
        rowLength = JOptionPane.showInputDialog("Specify row length");
        colLength = JOptionPane.showInputDialog("Specify column length");
    }
    public void fillMatrix()
    {
        int [][] matrix = new int [rowLength][colLength];
    }
}


The problem is that i dont know how to get integer from the user. Only String. It fails to compile because rowLength and colLength are String, not integer.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Sat Apr 22, 2006 9:24 am
Profile
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
http://www.scit.wlv.ac.uk/~jphb/java/basicio.html


Code:
   InputStreamReader stdin =
            new InputStreamReader(System.in);
      BufferedReader console =
            new BufferedReader(stdin);
      int   i1 = 0,i2 = 0;
      String   s1,s2;
      try
      {
         System.out.print("Enter first number ");
         s1 = console.readLine();
         i1 = Integer.parseInt(s1);
         System.out.print("Enter second number ");
         s2 = console.readLine();
         i2 = Integer.parseInt(s2);

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Sat Apr 22, 2006 9:34 am
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
The compiler doesnt like "try":

Code:
Matrix.java:13: 'try' without 'catch' or 'finally'


Below is the rather ugly-looking program. (for reference):

Code:
import java.io.*;
import javax.swing.*;
public class Matrix
{
    //private String rowLength;
    //private String colLength;
    public void initMatrix()
        {
            InputStreamReader stdin = new InputStreamReader(System.in);
          BufferedReader console = new BufferedReader(stdin);
          int   i1 = 0,i2 = 0;
          String   s1,s2;
          try
          {
          System.out.print("Enter first number ");
         s1 = console.readLine();
         i1 = Integer.parseInt(s1);
         System.out.print("Enter second number ");
         s2 = console.readLine();
         i2 = Integer.parseInt(s2);

       //rowLength = JOptionPane.showInputDialog("Specify row length");
        //colLength = JOptionPane.showInputDialog("Specify column length");
    }
    //public void fillMatrix()
    //{
        //int [][] matrix = new int [rowLength][colLength];
    //}
}}

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Sat Apr 22, 2006 9:59 am
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
After try you need at least one catch anyway. ;)

See a bit about it.

_________________
++


Sat Apr 22, 2006 1:56 pm
Profile WWW
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post 
lol, you were supposed to read the link, small fry. I just copied part of the source code...I omitted the catch part. Try and catch aren't required, though they can be handy in some situations.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Sat Apr 22, 2006 8:58 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Mmm... When i copy and paste the whole code (same as website exactly) into a new Java file, it doesnt seem to work.

It compiles fine, but when i run it i get the error:

Code:
Enter first number Input error


Any ideas?

Oh, btw, when i say "prompt the user", i mean displaying a dialog box so he can input one number and then the other.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Sun Apr 23, 2006 9:59 am
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
You get input error? May you enter something unexpected :roll: the program awaits two integers to add.

Code:
Enter first number 12
Enter second number 12
12 + 12 = 24

_________________
++


Sun Apr 23, 2006 2:45 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
I solved it.

I discarded the whole try & throw affair and replaced it simply with:

String numStr = JOptionPane.showDialogBox("Please enter number");
int numInt = Integer.parseInt(numString);

Bingo.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Sun Apr 23, 2006 4:26 pm
Profile
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Hey GFree, would you mind throwing in some parameters in the code you posted above explaining inheritance? Its just that im finding it difficult to put into perspective with parameters.

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Thu Apr 27, 2006 12:04 pm
Profile
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
My god, i think i just got it to work somehow... :shock:

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Thu Apr 27, 2006 12:39 pm
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
Some of them are pieces of code which are not ment to work without an public class with public static main methode.

Some of them (those who have one public static main methode in one of classes) are meant to work independent, but they must be saved in *.java file which has the same name as the class with main.

_________________
++


Thu Apr 27, 2006 3:32 pm
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Ahhh, ok.

*done*

Now im screwing around with Constructors, and with no proper textbook and no lecture until Wednesday, im struggling with the following concept:

Heres the question:

I need to implement a class that works out the perimiter and area of a rectangle, but apparently the question says: "There is need for a default constructor that initialises the length and width of a rectangle to zero and an additional constructor where the length and width can be initialised at the time of creating an object."

I just hit a mental block here because so far, ive only used constructors such as:

Quote:
Thingy thingy1 = new Thingy();


What can you tell me?

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Fri Apr 28, 2006 10:41 am
Profile
Emperor
User avatar

Joined: Wed Apr 16, 2003 1:25 am
Posts: 2560
Reply with quote
Post 
derf wrote:
Quote:
Thingy thingy1 = new Thingy();

That one is not a constructor but assignment of the new object of type Thingy to an pointer thingy1 of type Thingy.

I think you should read the chapter which introduces in constructors once more.

As for the question, here is the start:

Code:
// Rectangle.java
public class Rectangle {
   
   protected double length, width;
   
   public Rectangle()
   {
      length =
      width = 0;
   }
   
   public Rectangle(double l,double w)
   {
      length = l;
      width = w;
   }
   
   public void printme()
   {
      System.out.println("l = " + length + " | w = " + width);
   }
   
   public static void main(String aa[])
   {
      Rectangle p = new Rectangle();
      Rectangle q = new Rectangle(6,5);
      
      p.printme();
      q.printme();
   }
}

_________________
++


Fri Apr 28, 2006 11:35 am
Profile WWW
Minor Diety
User avatar

Joined: Fri Apr 11, 2003 2:17 pm
Posts: 7721
Location: Centre of the sun
Reply with quote
Post 
Ok, gotcha. Now thats done.

Now one last thing.....Polymorphism. There are loads of explanations in books and stuff, but it means nothing to me because i cant seem to put into context. Unfortunately, thats how my brain works. You could go on and on endlessly about concepts, but to me they will never have value without context.

GFree, could you write a class showing the most basic use polymorphism please?

_________________
"Well a very, very hevate, ah, heavy duh burtation tonight. We had a very derrist derrison, bite, let's go ahead and terrist teysond those fullabit who have the pit." - Serene Branson


Wed May 03, 2006 6:30 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 54 posts ]  Go to page Previous  1, 2, 3, 4  Next

Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.