Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By SurfMan

Thread: can't access parent class?

  1. #1
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default can't access parent class?

    i mada a parent class then i inherit into my main class and then when i make object of my class and try to call parent methods it shows some error

    Java Code:
    // My Child Class
    
    class Exr extends Iv
    {
       public static void main(String Dev[])
       {
           Exr e=new Exr();
    	   e.setName("Vivek");
    	   e.setAge(20);
    	   e.setClas(12);
    	   
    	   System.out.println("Name : "+getName());
    	   System.out.println("Age : "+getAge());
    	   System.out.println("Class : "+getClas());
    	   
       
        }
    
    }

    Java Code:
    //My Parent class
    
    public class Iv
    {
         String name;
    	 int age;
    	 int cls;
        public void setName(String nm)
    	{
    	    name=nm;
    	}
    	public void setAge(int x)
    	{
    	     age=x;
    	 
    	 }
    	 
    	 public void setClas(int c)
    	 {
    		 cls=c;
    	 }
    
    	 public int getAge()
    	 {
    	 return(age);
    	}
    	public String getName()
    	{
    	 return(name);
    	}
    	
    	public int getClas()
    	{
    		return(cls);
    	}
    	
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: can't access parent class?

    it shows some error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    3

    Default Re: can't access parent class?

    The same problem repeats heres as it was in your previous question.
    https://www.java-forums.org/new-java...tor-wrong.html

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: can't access parent class?

    Remember that Java is not a crystal-ball-language. It doesn't know things and it doesn't assume things. It's like a little kid: you have to tell it how to do stuff. Every time. getName()? Which object has that method? You did it right with e.setName(), why not use e.getName() then?
    vknehra10 likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: can't access parent class?

    vknehra10, you should be using IDE and reading compiler error messages.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: can't access parent class?

    Using IDE, not necessarily. Reading compiler error messages. Absolutely!

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Access child methods from parent class
    By bmL in forum New To Java
    Replies: 2
    Last Post: 07-29-2012, 02:25 PM
  2. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  3. Access Parent and child node
    By emyk in forum Advanced Java
    Replies: 0
    Last Post: 05-02-2011, 11:16 PM
  4. How to Access and Set variable in parent class
    By java_newbie88 in forum New To Java
    Replies: 6
    Last Post: 01-20-2011, 02:48 AM
  5. Replies: 7
    Last Post: 05-06-2009, 12:42 PM

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
  •