Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By jim829

Thread: Inheritance - a tricky program -lets discuss

  1. #1
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default Inheritance - a tricky program -lets discuss

    Hi ! Please clarify it ....

    Java Code:
    class ClassA {
    	int a = 1 ;
    	void sampleMethod() {
    		System.out.println("Value of a is " + a);
    	}
    }
    
    class ClassB extends ClassA {
    	int b = 2;
    	void simpleMethod() {
    		System.out.println("Value of b is " + b);
    	}
    }
    
    class MainClass {
    	public static void main(String args[]) {
    		ClassA caobj = new ClassB(); // this really tricky to me 
    		caobj.sampleMethod();
    		caobj.simpleMethod(); // this line throw an error.. ?
    
    	}
    }
    thanks in advance !

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: Inheritance - a tricky program -lets discuss

    Class A knows nothing of class B's methods. So when you assign an instance of Class B to a class A reference, only Class A's methods are visible.

    Regards,
    Jim
    j_arif123 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default Re: Inheritance - a tricky program -lets discuss

    so is this legal to assign a subclass object to the reference of the superclass? if yes, then can you give some realtime/situation in which this way of approach is handled?. Is it technically named ? excuse me for asking these kind of question... I blindly did few things too. That is , when i removed the inheritance (in the above code) , the error thrown was : incompatible types. Also i tried to assign the object of superclass say ClassA to the subclass say ClassB object reference . This is also not worked.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: Inheritance - a tricky program -lets discuss

    It's called polymorphism. It allows subclasses of various types to be handle by a common superclass or interface. Imagine a superclass of type Furniture and subclasses of Furniture called Chair, Table, and Couch. You can't put a Chair in a list of type Table. But you can put both in a list of type Furniture. The usefulness of this is that if you want to determine some furniture trait of all your tables, chairs,and couches, you can iterate of a list of furniture and call any of the superclass methods. I find this capability to be much more useful when implementing interfaces. For more on that, check out The Java™ Tutorials.

    Regards,
    Jim
    j_arif123 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    j_arif123 is offline Member
    Join Date
    Jan 2012
    Location
    India
    Posts
    34
    Rep Power
    0

    Default Re: Inheritance - a tricky program -lets discuss

    (i went through those tutorials u mentioned but still i have to make some clarifications) I heard about polymorphism which one of the oops concept . this is implemented by overloading concept .... but the things you said above is all about inheritance , isn't it?. (as far as i understood , accessing superclass members using subclass object etc are comes under inheritance ). May be, inheritance and polymorphism have some relationship. My doubt is that in how many ways the polymorphism is achieved (especially in java) ?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: Inheritance - a tricky program -lets discuss

    I believe you meant overriding, not overloading. It is a capability supported by polymorphism. Inheritance is one mechanism by which polymorphism is achieved.

    Also, note that overriding is declaring a subclass method with the same signature of a superclass method to make it more applicable to the subclass. Overloading in Java only applies to identically named methods with different method signatures. In other languages, there is the concept of operator overloading so that one can change how certain operators (+, -, *, /) are applied to objects. Java does not support operator overloading (although in my opinion it would make certain classes like BigInteger and BigDecimal easier to use).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. hello, got a fun project to discuss
    By lorenzop in forum Introductions
    Replies: 0
    Last Post: 10-27-2012, 12:36 PM
  2. Need some help with tricky program
    By CGHMN in forum New To Java
    Replies: 32
    Last Post: 06-20-2011, 08:46 AM
  3. Replies: 3
    Last Post: 08-21-2010, 09:01 AM
  4. Let's discuss about Alfresco Labs
    By java-walker in forum Web Frameworks
    Replies: 0
    Last Post: 04-20-2009, 08:59 PM
  5. Inheritance Program
    By Schaput in forum New To Java
    Replies: 6
    Last Post: 11-14-2008, 12:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •