Explicit type conversion


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Explicit type conversion

  1. #1
    Join Date
    May 2003
    Posts
    4

    Smile Explicit type conversion

    Here is a way in which we can do type conversions explicitly.
    In the following example, class B extends class A.

    public class A {

    private int aa ;
    protected int ab ;
    public int ac ;

    public void make_it_A(int intparam1){

    aa = intparam1;
    }
    }

    public class B extends A {

    private int ba ;
    protected int bb ;
    public int bc ;

    public A make_it_A(int aa){

    A newA = this;
    newA.ba = null;
    newA.bb = null;
    newA.bc = null;
    newA.make_it_A(aa);
    return newA;

    }
    }

    instead of doing
    A aobject = bObject;
    you can do
    A aobject = bobject.make_it_A(myInt);

    Please express your views about cases where this can be useful if you used this.

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    I can't really see the point of what you are doing. You are trying to use this strange system in place of proper inheritance and polymorphism. Why would you *ever* want to do this?????
    ArchAngel.
    O:-)

  3. #3
    Join Date
    May 2003
    Posts
    4

    Lightbulb Application

    This new system can be useful when class A represents "Person" and class B extends from "person" and represents a "policeman" . After the office hours the "policeman" is just a "person" with no previleges of a policeman on duty.

    Please forward your reply. I would like to know the other ways of handling the same.

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    No. This still doesn't make much sense. What's wrong with:
    Code:
      public class Person { // Person methods... }
      public class Policeman extends Person { // Police methods... }
    (Actually, in advanced OO design, these are really roles, but I'll leave that for now)
    ArchAngel.
    O:-)

  5. #5
    Join Date
    May 2003
    Posts
    4
    I am really ignorent compared to you, but how do you prevent a "Person" from invoking methods in "policeman"?

    for example, aPerson.arrest_Joe() i.e. A.Bmethod()?

    Here, "Person" and "Policeman" are the classes we are giving to the police department and they reuse these classes.

  6. #6
    Join Date
    Mar 2003
    Posts
    84
    that doesn't happen. inheritance only goes down.

    if you had a person object, they can't invoke methods in a policeman class.

    but policemen can invoke methods in a person class.

    read up on inheritance.. you are thinking of it incorrectly.
    I'm surprised more of you people don't get hit by cars.

  7. #7
    Join Date
    Mar 2003
    Posts
    834
    Inheritance doesn't work like that! Here are some slightly more complete definitions:
    Code:
      public class Person() {
        public Person(String firstName, String surname) {
          this.firstName = firstName;
          this.surname = surname;
        }
    
        public String getName() {
          return firstName + " " + surname;
        }
    
        private String firstName;
        private String surname;
     }
    
      public class Policeman extends Person {
        public Policeman(String firstName, String surname, int hatSize) {
          super(firstName, surname);
          this.hatSize = hatSize;
        }
    
        public void arrest(Person p) {
          // some code here....
        }
        private int hatSize;
      }
    Here is how they would be used:
    Code:
      public class TestClasses {
        public static void main(String[] args) {
           Person robber = new Person("Robin", "Banks");
           System.out.println("Person: " + robber.getName());
    
           Policeman pc_plod = new Policeman("Chief", "Wiggum", 12);
           System.out.println("Policeman: " + robber.getName());
    
           pc_plod.arrest(robber);
    
           // You can't call robber.arrest(pc_plod).
        }
      }
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center