aspose file tools*
The moose likes Beginning Java and the fly likes Objects and classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects and classes" Watch "Objects and classes" New topic
Author

Objects and classes

Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 65

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


I love deadlines, I love the whooshing sound they make as they fly by...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
vinay chaturvedi
Greenhorn

Joined: Jan 16, 2012
Posts: 14
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

Joined: Jan 21, 2013
Posts: 65

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

Joined: Jul 10, 2012
Posts: 50
Hi Ciaran,

Welcome to coderacnh. Use Code Tags for your code in the post then how much better it looks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Jan 21, 2013
Posts: 65

Cheers for the welcome.
I will have a go at using the code tags.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
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

Joined: Jan 21, 2013
Posts: 65

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

Joined: Aug 16, 2005
Posts: 13875
    
  10

About the difference between instance and class members: Understanding Instance and Class Members
Ciaran Mooney
Ranch Hand

Joined: Jan 21, 2013
Posts: 65

Cheers everyone, been a lotta help.
C
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects and classes
 
Similar Threads
Inheritance of private method ?
Inheritance of static methods
Inheritance and Polymorphism
Interface member declaration Issue
Inheritance, private fields