Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎


posted Oct 12, 2017, 9:54 AM by Konstantinovich Samuel   [ updated Oct 12, 2017, 9:56 AM ]

All the primitive types have a fixed size. Thus, the primitive types are limited to a range of values. 

A smaller primitive type (byte) can contain a smaller range of values than a bigger one (long).

Integerbyte8-128127From +127 to -128byte b = 65;
char160216-1All Unicode characterschar c = 'A';
char c = 65;
short16-215215-1From +32,767 to -32,768short s = 65;
int32-231231-1From +... to -... 
(big values you can calculate)
int i = 65;
long64-263263-1From +... to -...  
(big values you can calculate)
long l = 65L;
float322-149(2-2-23)·2127From 3.402,823,5 E+38 
to      1.4 E-45
float f = 65f;
double642-1074(2-2-52)·21023From 1.797,693,134,862,315,7 E+308
      to 4.9 E-324
double d = 65.55;
Otherboolean1----false, trueboolean b = true;

Goal: I want my inheritance!

Inheritance: In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.

A class that is derived from another class is called a subclass (also a derived classextended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

With the single exception that is Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

The Java Platform Class Hierarchy

The Object class, defined in the java.lang package, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes.

All Classes in the Java Platform are Descendants of Object

All Classes in the Java Platform are Descendants of Object

At the top of the hierarchy, Object is the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.

What You Can Do in a Subclass

A subclass inherits all of the public members of its parent.
You can use the inherited members as is, replace them, hide them, or supplement them with new members:

  • The inherited fields can be used directly, just like any other fields.
  • You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (just like parameters this is not recommended).
  • You can declare new fields in the subclass that are not in the superclass.
  • The inherited methods can be used directly as they are.
  • You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
  • You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
  • You can declare new methods in the subclass that are not in the superclass.
  • You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

The following sections in this lesson will expand on these topics.

Private Members in a Superclass

A subclass does not inherit (access to) the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.


class Super {
class Sub extends Super {


class Teacher {
   public void does(){

public class SubjectTeacher extends Teacher{
   private String mainSubject;//new field

   public SubjectTeacher(String subject){
	mainSubject = subject; 

   public String getSubject(){
	return mainSubject;

   //override a method!
   public void does(){
	System.out.println("Teaching "+mainSubject);


public class Point2D{ private double x,y; //... lots of methods }

public class Point3D extends Point2D{
	private double z;

	public double getZ(){
	  return z;
	//add setZ and other methods
	//What about distance?