Class is a set of object which shares common characteristics/ behavior and common properties/ attributes. Class is not a real world entity. It is just a template or blueprint or prototype from which objects are created. Class does not occupy memory. Class is a group of variables of different data types and group of methods.

A class in java can contain: • data member • method • constructor • nested class and • interface

A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:

Modifiers: A class can be public or has default access (Refer this for details). Class keyword: class keyword is used to create a class. Class name: The name should begin with an initial letter (capitalized by convention). Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface. Body: The class body is surrounded by braces, { }. Constructors are used for initializing new objects. Fields are variables that provide the state of the class and its objects, and methods are used to implement the behavior of the class and its objects.

Homework Assigned

FRQ 2021 1a 3a

2021 frq

//1a

public int scoreGuess(String guess)
{
  int count = 0;

  for(int i = 0; i < secret.length(); i++)
  {
    int j = i + guess.length();

    if(j <= secret.length() && secret.substring(i, j).equals(guess))  //check if substring are equal to secret
      count++;
  }

  return count * (guess.length() * guess.length());
}
//3a

public void addMembers(String[] names, int gradYear)
{
  for(String name : names)
    memberList.add(new MemberInfo(name, gradYear, true));  //included parameters here for members
}

Why are there no returns for constructors

What actually happens with the constructor is that the runtime uses type data generated by the compiler to determine how much space is needed to store an object instance in memory, be it on the stack or on the heap.

This space includes all members variables and the vtbl. After this space is allocated, the constructor is called as an internal part of the instantiation and initialization process to initialize the contents of the fields.

Then, when the constructor exits, the runtime returns the newly-created instance. So the reason the constructor doesn't return a value is because it's not called directly by your code, it's called by the memory allocation and object initialization code in the runtime.

Its return value (if it actually has one when compiled down to machine code) is opaque to the user - therefore, you can't specify it.

Class naming conventions

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

Examples

class Raster; class ImageSprite;

Accessors

The accessor method’s name is driven by the word “access” which allows the user to access the private information in a class.

If you have ever heard of the “get” method or “getters”, it is the same thing as accessors. The getters retrieve the private variables and constants to access outside the scope of a class.

public class Student {

	private String name;

	public String getName() {
		return name;
	}
}

Mutators

The mutator method in Java is driven by the word “mutate”, which literally means to modify. Mutators allow the users to set/mutate the value of private variables of a class object.

In the object-oriented programming context, the “set” method or “setters” are also known as mutators. Setters facilitate encapsulation as private data members can not be modified directly. So setter methods/mutators are used to update a variable’s value outside the class scope.

public class Student {

	private String name;

	public void setName(String name) {
		this.name = name;
	}
}

Why do we need accessors and mutators?

We need getters and setters or accessors and mutators to protect sensitive information in a class. The information is protected from Illegal use by using these standard methods. Moreover, the data set in a mutator can also be validated if it fulfils all the requirements of a program.

Using Static/class variables

class Main
{
   static int count=0;
   public void increment()
   {
       count++;
   }
   public static void main(String args[])
   {
       VariableDemo obj1=new VariableDemo();
       VariableDemo obj2=new VariableDemo();
       obj1.increment();
       obj2.increment();
       System.out.println("Obj1: count is="+obj1.count);
       System.out.println("Obj2: count is="+obj2.count);
   }
}

Main.main(null)

Access Modifiers

Access Specifier Inside Class Inside Package Outside package subclass Outside package Private Yes No No No Default Yes Yes No No Protected Yes Yes Yes No Public Yes Yes Yes Yes