Better Living in Objectville — Chapter 7

Upulie Handalage
3 min readApr 23, 2022

Inheritance(IS-A)

JVM searches the inheritance tree, starting at the class type you invoked the method on (the most specific version for that particular object). There’s never a chance that the JVM doesn’t ever find a match, cuz beforehand, the compiler always checks.

To use both the superclass version and the subclass version of a method(to add more stuff to the method)(to extend/append the functionality of the superclass)

public void roam(){

super. roam () ;

/ / my own roam stuff

}

Reference(HAS-A)

Bathroom HAS-A Tub and Tub HAS-A Bubbles. But nobody Inherits from (extends) anybody else.

Access Levels (who sees what)

A superclass can choose whether or not it wants a subclass to inherit a particular member by the level of access the particular member is given. There are 4 access levels. Moving from most restrictive to least,

  1. private — members are not inherited
  2. default
  3. protected
  4. public — members are inherited

Polymorphism

When you define a super type for a group of classes, any subclass can be used to substitute where the super type is expected.

Dog myDog = new Dog () ;

Notice that the reference type(Dog) and the object type(new Dog()) are the same.

With polymorphism, the reference type and the object type can be different. The reference type can be a superclass of the actual object type.

Animal myDog = new Dog() ;

Polymorphism lets you make polymorphic arrays.

Animal[] animals= new Animal[2];

animals [0] = new Dog();

animals [1] = new Cat();

Advantages of Polymorphism

  1. polymorphic arrays can be used to loop through for different customized methods accordingly.

for(Animal a: animals){

a.eat(); //performs dog/cat specific code

}

2. Polymorphic arguments and return types can be used.

public void giveShot(Animal a){

a.makeNoise();

}

Non-public classes

A class can’t be marked private. However, a class can be non-public (if you don’t declare the class as public). They can only be sub-classed/used by classes in the same package as the class.

To prevent a class from being sub classed

  1. Make a class non-public.
  2. Use the keyword modifier final (end of the inheritance line)(can’t extend a final class) — not common but used for security and consistency. Eg: The String class.
  3. Make the class have only private constructors, so it can’t be sub-classed.

Rules of Overriding

  1. Arguments must be the same, and return types must be compatible.

2. The method can’t be less accessible. (access level must be the same, or friendlier. (don’t override a public method and make it private).

Rules of Overloading

Two methods with the same name but different argument lists. No polymorphism is involved. It lets you make multiple versions of a method, with different argument lists, for convenience to the callers. An overloaded method is not the sane as an overridden method.

  1. The return types can be different, as long as the argument lists are different.
  2. You can’t change ONLY the return type. If only the return type is different, it’s not a valid overload-the compiler will assume you’re trying to override the method. And even that won’t be legal unless the return type is a subtype of the return type declared in the superclass.
  3. You can vary the access levels in any direction.

Note

Inherited methods can be overridden; instance variables cannot be overridden (but can be redefined) in the subclass.

If source code is not available for a class, sub classing can be used to change the way a method of that class works (by overriding).

If you want to protect a specific method from being overridden, mark the method as final.

Vocabulary

inner class — Is a special case. you can’t inherit it

Thanks for reading. Until next time! 👋🏽

--

--

Upulie Handalage

Everything in my point of view. Here for you to read on....