• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects and classes

 
Ciaran Mooney
Ranch Hand
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm very new to java(and programming) and I am trying to get my head around the concept of public and private methods in classes.

My understanding is you can create a object of a parent class though the syntax Class object name = new Class();.



Then utilise any methods defined in the parent class under public method declarations, with syntax object.method()

this object can be both created and used outside of the parent class.

However I have no idea what the relationship is bewteen a private method declaration (or instance variable??), public declaration and a constructer function.

Any help appreciated

Cheers
Ciaran
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ciaran Mooney wrote:My understanding is you can create a object of a parent class though the syntax Class object name = new Class();.

You create an object of a class through the syntax: ClassName variableName = new ClassName();
(It doesn't have anything to do with parent classes and objects don't have names; variables do).

Ciaran Mooney wrote:However I have no idea what the relationship is bewteen a private method declaration (or instance variable??), public declaration and a constructer function.

Words such as "public", "protected" and "private" determine from where exactly you can call methods. A method that is public can be called from anywhere. A method that is private can only be called from inside the class itself, not from methods and constructors in other classes. This allows you to hide methods inside classes. See Controlling Access to Members of a Class for more information.

A constructor is a special block of code inside your class that initializes a new instance (object) of the class. It's called when you use "new" to create a new instance of the class. A constructor is not a method. See Providing Constructors for Your Classes for more information.
 
vinay chaturvedi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you declare a method with a private modifier, then that method is private to that particular class. The child classes of that class cannot access that method.

In case of public modifier, methods in parent class with public modifiers are accessible in child classes.

Constructors are called whenever a new object is created for a class. Constructors can be private or public.

 
Ciaran Mooney
Ranch Hand
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
I have a piece of code from a tutorial which appears
to declare a private method in Class A and is then
called on by in class b.

public class GradeBook 6{


private String courseName; // course name for this GradeBook
Instance variable courseName

// method to set the course name
public void setCourseName( String name )
{
courseName=name
}

public String getCourseName()
{ return courseName
}
set method for courseName

This method is later called on in a separate GradeBookTest class:

GradeBook myGradeBook= new GradeBook();
Sytem.out.printf("course is %s/n: " myGradeBook.getCpurseName());

So im trying to understand why a private method is used , followed by a "set" and a "get" public method in the same class, then the getCoursename method is called in a separate class.

Cheers
C
 
Pyla Rao
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ciaran,

Welcome to coderacnh. Use Code Tags for your code in the post then how much better it looks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch vinay chaturvedi

They are not called parent class and child class. They are called superclass and subclass. Different languages (eg C#) use different names.
 
Ciaran Mooney
Ranch Hand
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers for the welcome.
I will have a go at using the code tags.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That other class does not use a private method. It uses a public method to gain access to the private field.
I tried to add code tags, since you haven’t seen them before, but your code isn’t indented, so they wouldn’t have worked.
 
Ciaran Mooney
Ranch Hand
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks starting to make sense.

Still a bit hazy on the diff between a class/static
varible and a instance varible?

C
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
About the difference between instance and class members: Understanding Instance and Class Members
 
Ciaran Mooney
Ranch Hand
Posts: 74
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers everyone, been a lotta help.
C
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic