radius; // variables String color; double getRadius() { ...... } // methods double getArea() { ...... } } A class name shall be a noun All the words shall be initial-capitalized (camel-case) Use a singular noun for class name
particular item of a class • All the instances of a class have similar properties, as described in the class definition • The term object refers to the instance of a class
c1 and c2 Circle c1, c2; // Allocate and construct the instances via new operator c1 = new Circle(); c2 = new Circle(2.0); // You can declare and construct in the same statement Circle c4 = new Circle(2.0, “red”); To create an instance of a class, you have to: Declare an instance identifier (instance name) of a particular class. Construct the instance (i.e., allocate storage for the instance and initialize the instance) using the "new" operator.
radius; // variables String color; double getRadius() { ...... } // methods double getArea() { ...... } } A variable name shall be a noun or a noun phrase made up of several words. The first word is in lowercase and the rest of the words are initial- capitalized (camel-case) Take note that variable name begins with an lowercase, while class name begins with an uppercase.
radius; // variables String color; double getArea() { return radius * radius * Math.PI; } } A method name shall be a verb, or a verb phrase made up of several words The first word is in lowercase and the rest of the words are initial- capitalized (camel-case) Take note that variable name is a noun (denoting a static attribute), while method name is a verb (denoting an action).
the same method name as the class name • A constructor is used to construct and initialize all the member variables • To create a new instance of a class, you need to use a special "new" operator followed by a call to one of the constructors
Circle(2.0); Circle c3 = new Circle(3.0, "red"); The name of the constructor method is the same as the class name, and by classname convention, begins with an uppercase. Constructor has no return type (or implicitly returns void). Hence, no return statement is allowed inside the constructor's body. Constructor can only be invoked via the "new" operator. It can only be used once to initialize the instance constructed. You cannot call the constructor afterwards. Constructors are not inherited.
public Circle() { ...... } public Circle(int radius) { ...... } public Circle(int radius, String color) { ...... } } Method overloading means that the same method name can have different implementations The different implementations must be distinguishable by their argument list
from another class • The derived class is called a subclass, the class from which it is derived is called a superclass • The derived class inherits all the member variables and methods from it’s superclass • The constructors are not members, so they are not inherited.
...... } } public class Student extends Person { int studentID; } Student inherits from Person. Student is the subclass, Person is the superclass Inheritance is marked by the keyword extends. Now an instance of the Student class will have access to the studentID member as well as the fullName and makeBreakfast() members of it’s superclass
from a superclass and then to modify it as needed • The overriding method has the same name, number and type of parameters, and return type as the method that it overrides • Optional: use the @Override annotation to signal the compiler that you want to override a method