































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The basics of defining and using classes in java, including the structure of classes, defining a class with instance variables and symbolic constants, creating and initializing objects, and manipulating those objects. It also includes a comparison with c++ and a task to define a student class with specific characteristics and methods. The document also touches upon the concepts of static variables, methods, and garbage collection in java.
Typology: Slides
1 / 39
This page cannot be seen from the preview
Don't miss anything!
































OOP Vocabulary Review
Classes
Definition or a blueprint of a userdefined datatype Prototypes for objects
Objects
Nouns, things in the world
Constructor
Given a Class, the way to create an Object (that is, an Instance of the Class) and initialize it
Attributes
Properties an object has
Methods
Actions that an object can do
Anything we can put a thumb on
Defining a Class
Comparison with C++
Specify access modifiers (public, private or protected ) for each member method or data members at every line.
The Point Class
class Point {
……….
}
public : Accessible anywhere by anyone Private : Only accessible within this class Protected : Accessible only to the class itself and to it’s subclasses or other classes in the same “package” Package : Default access if no access modifier is provided. Accessible to all classes in the same package
Same name as class name Does not have a return type No initialization list JVM provides a zero-argument constructor only if a class doesn’t define it’s own constructor
Destructors are not required in a java class
Defining a Class
Comparison with C++ (cont)
Student Implementation Code
// Student.java
/*
Demonstrates the most basic features of a class. A student is defined by their name and rollNo. There are standard get/set accessors for name and rollNo.
NOTE A well documented class should include an introductory comment like this. Don't get into all the details – just introduce the landscape.
*/
public class Student {
private String name;
private int rollNo;
// Standard Setters
public void setName (String name) { this.name = name; }
// Note the masking of class level variable rollNo public void setRollNo (int rollNo) { if (rollNo > 0) { this.rollNo = rollNo; }else { this.rollNo = 100; } }
Student Implementation Code cont.
// Constructor that uses a default value instead of taking an argument.
public Student() { name = “not set”; rollNo = 100; }
// parameterized Constructor for a new student
public Student(String name, int rollNo) { setName(name); //call to setter of name setRollNo(rollNo); //call to setter of rollNo }
// Copy Constructor for a new student public Student(Student s) { name = s.name; rollNo = s.rollNo; }
Student Implementation Code cont.
// method used to display method on console
public void print () {
System.out.println("Student name:" +name+ ", roll no:" +rollNo);
}
Student Implementation Code cont.
Using a Class
Comparison with C++
Student s = new Student () // no pointer operator “*” between // Student and s
Only String constant is an exception String greet = “Hello” ; // No new operator
However you can use String greet2 = new String(“Hello”);
s.setName(“Ali”); SsetName(“Ali”) is incorrect and will not compile in java
Using a class
Comparison with C++