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

2018-10-12

posted Oct 12, 2018, 6:19 AM by Konstantinovich Samuel
Goal: I want my inheritance! (Triskaidekaphobia begone!)

Inheritance: 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. (Every class you wrote so far)


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 which we didn't learn) from its superclass. Constructors are not methods, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.


Multiple Inheritance:  one class can extend multiple other classes, which means a child class has two parent classes. For example class C extends both classes A and B. Java DOES NOT support multiple inheritance.

Java does support these ideas however:

Single Inheritance: a class can extend one and only one other class, so each child can have only one parent class.

Multilevel inheritance one can inherit from a derived class, so when A has child B it means that B can have a child C. This means that C is the descendant of A, but the direct child of B.

Hierarchical inheritance: more than one classes extends the same class. For example, classes B, C & D extends the same class A.


All Classes in the Java Platform are Descendants of Object

The Object class, defines and implements behavior common to all classes—including the ones that you write.

Some classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes.


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 (non-static 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.


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 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.

Syntax (EACH CLASS IS A SEPARATE FILE!!! I SHOULD NOT HAVE TO STATE THIS!!!!!)


class Super {
   .....
   .....
}
class Sub extends Super {
   .....
   .....
}


//SAMPLE CLASSES:

class Teacher {

   //when no constructor is provided, java will add one:
   //public Teacher(){} //empty default constructor 
   
   public void does(){
	System.out.println("Teaching");
   }
}




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);
   }
}


//POINT

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?
}
Comments