Java 8 Programming Black Book
Java 8 Programming Black Book
Chapter-I
Inheritance:
Inheritance basics
Super keyword
Multi-level hierarchy
Abstract classes
final keyword with inheritance.
drkkbaseer@gmail.com 1
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.
Difference between object and class
drkkbaseer@gmail.com 4
UNIT-II
Chapter-I
Inheritance:
Inheritance basics
Super keyword
Multi-level hierarchy
Abstract classes
final keyword with inheritance.
drkkbaseer@gmail.com 5
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.
• Inheritance is one of the cornerstones of object-oriented
programming because it allows the creation of hierarchical
classifications.
• In the terminology of Java, a class that is inherited is called a
superclass.
• The class that does the inheriting is called a subclass.
• Therefore, a subclass is a specialized version of a superclass.
It inherits all of the members defined by the superclass and
adds its own, unique elements.
drkkbaseer@gmail.com 6
Cont..
• The idea behind inheritance in java is that you can create new
classes that are built upon existing classes.
• When you inherit from an existing class, you can reuse
methods and fields of parent class, and you can add new
methods and fields also.
• Inheritance represents the IS-A relationship, also known
as parent-child relationship.
Note: Unless specified otherwise, all classes are derived from a
single root class, named Object. If no parent class is explicitly
provided, the class Object is implicitly assumed.
Why use inheritance in java:
For Method Overriding (so runtime polymorphism can be
achieved).
drkkbaseer@gmail.com 7
For Code Reusability.
Cont..
drkkbaseer@gmail.com 8
Cont..
// A simple example of inheritance.
// Create a superclass.
class A
{
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j); }}
// Create a subclass by extending class A.
class B extends A {
int k;
void showk() {
System.out.println("k: " + k); }
void sum() {
System.out.println("i+j+k: " + (i+j+k));
drkkbaseer@gmail.com }} 9
Cont..
class SimpleInheritance {
public static void main(String args []) {
A superOb = new A(); B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10; superOb.j = 20;
System.out.println("Contents of superOb: ");
superOb.showij();
System.out.println();
/* The subclass has access to all public members of its superclass. */
subOb.i = 7; subOb.j = 8; subOb.k = 9;
System.out.println("Contents of subOb:");
subOb.showij(); subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subOb:");
subOb.sum(); }}
drkkbaseer@gmail.com 10
Cont..
Output:
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k in subOb:
i+j+k: 24
drkkbaseer@gmail.com 11
Cont..
class A {
int i; // public by default
private int j; // private to A
…….
}
Note: A class member that has been declared as private will
remain private to its class. It is not accessible by any code
outside its class, including subclasses.
drkkbaseer@gmail.com 12
UNIT-II
Chapter-I
Inheritance:
Inheritance basics
Super keyword
Multi-level hierarchy
Abstract classes
final keyword with inheritance.
drkkbaseer@gmail.com 13
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.
Member Access Rules:
• A subclass includes all of the members of its super class, I
cannot access those members of the super class that have been
declared as private.
• A class member that has been declared as private will remain
private to its class. It is not accessible by any code outside its
class, including subclasses.
super keyword in java
• The super keyword in java is a reference variable which is used
to refer immediate parent class object.
• Whenever you create the instance of subclass, an instance of
parent class is created implicitly which is referred by super
reference variable. drkkbaseer@gmail.com 14
Cont..
drkkbaseer@gmail.com 15
1.super is used to refer immediate parent class instance variable.
class Animal{
Cont..
String color="white"; }
class Dog extends Animal{
String color="black";
void printColor(){
System.out.println(color);//prints color of Dog class
System.out.println(super.color);//prints color of Animal class
} }
class TestSuper1{
public static void main(String args[]){
Output:
Dog d=new Dog(); Black
d.printColor(); white
}} drkkbaseer@gmail.com 16
Cont..
2) super can be used to invoke parent class method
• It should be used if subclass contains the same method as
parent class. In other words, it is used if method is overridden.
class Animal{
void eat(){System.out.println("eating...");} }
class Dog extends Animal{
void eat(){System.out.println("eating bread...");}
void bark(){System.out.println("barking...");}
void work(){ super.eat(); bark(); } }
class TestSuper2{
public static void main(String args[]){ Output:
Dog d=new Dog(); eating...
barking...
d.work(); }}
drkkbaseer@gmail.com 17
Cont..
3) super is used to invoke parent class constructor.
class Animal{
Animal()
{
System.out.println("animal is created");}
}
class Dog extends Animal{
Dog(){
super();
System.out.println("dog is created");
} }
class TestSuper3{ Output:
animal is created
public static void main(String args[]){
dog is created
drkkbaseer@gmail.com 18
Dog d=new Dog(); }}
Cont..
Types of inheritance in java
drkkbaseer@gmail.com 19
Cont..
drkkbaseer@gmail.com 20
UNIT-II
Chapter-I
Inheritance:
Inheritance basics
Super keyword
Multi-level hierarchy
Abstract classes
final keyword with inheritance.
drkkbaseer@gmail.com 21
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.
Creating multilevel hierarchy
• Multilevel inheritance, it is a concept of grandparent class. If
we take the example of above diagram then class C inherits
class B and class B inherits class A which means B is a parent
class of C and A is a parent class of B.
• So in this case class C is implicitly inheriting the properties
and method of class A along with B that’s what is called
multilevel inheritance.
drkkbaseer@gmail.com 22
Cont..
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep(){System.out.println("weeping...");}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
weeping...
barking...
eating... drkkbaseer@gmail.com 23
Cont..
Hierarchical
class Animal{
void eat(){System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){System.out.println("barking...");}
}
class Cat extends Animal{
void meow(){System.out.println("meowing...");}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}
Output:
meowing...
eating… drkkbaseer@gmail.com 24
Cont..
Why multiple inheritance is not supported in java?
class A{
void msg(){System.out.println("Hello");}
}
class B{
void msg(){System.out.println("Welcome");}
}
class C extends A,B {//suppose if it were
public static void main(String args[]){
C obj=new C();
obj.msg();//Now which msg() method would be invoked?
} }
Compile Time Error drkkbaseer@gmail.com 25
UNIT-II
Chapter-I
Inheritance:
Inheritance basics
Super keyword
Multi-level hierarchy
Abstract classes
final keyword with inheritance.
drkkbaseer@gmail.com 26
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.
Difference between method overloading and method overriding
drkkbaseer@gmail.com 28
class Bank{ Cont..
int getRateOfInterest(){return 0;}
}
class SBI extends Bank{
int getRateOfInterest(){return 8;}
}
class ICICI extends Bank{
int getRateOfInterest(){return 7;}
}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
}
class Test2{ Test it Now
public static void main(String args[]){ Output:
SBI Rate of Interest: 8
SBI s=new SBI();
ICICI Rate of Interest: 7
ICICI i=new ICICI();
AXIS Rate of Interest: 9
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
} } drkkbaseer@gmail.com 29
Cont..
• There are situations in which you will want to define a
superclass that declares the structure of a given abstraction
without providing a complete implementation of every
method.
• Abstraction is a process of hiding the implementation details
and showing only functionality to the user.
• A class that is declared with abstract keyword, is known as
abstract class in java. It can have abstract and non-abstract
methods (method with body).
• Another way, it shows only important things to the user and
hides the internal details for example sending sms, you just
type the text and send the message. You don't know the
internal processing about the message delivery.
drkkbaseer@gmail.com 30
Note: Abstraction lets you focus on what the object does instead
of how it does it. Cont..
Ways to achieve Abstraction
There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
Abstract class
• A class that is declared as abstract is known as abstract class.
It needs to be extended and its method implemented. It
cannot be instantiated.
• Example:
abstract class A{}
abstract method
A method that is declared as abstract and does not have
implementation is known as abstract method.
drkkbaseer@gmail.com 31
Cont..
// A Simple demonstration of abstract.
abstract class A {
abstract void callme();
// concrete methods are still allowed in abstract classes
void callmetoo() {
System.out.println("This is a concrete method.");
}}
class B extends A {
void callme() {
System.out.println("B's implementation of callme.");
}}
class AbstractDemo {
public static void main(String args[]) {
B b = new B();
b.callme();
b.callmetoo();
}} drkkbaseer@gmail.com 32
Cont..
Note:
1. An abstract class can have data member, abstract method,
method body, constructor and even main() method.
2. If there is any abstract method in a class, that class must be
abstract.
3. If you are extending any abstract class that have abstract
method, you must either provide the implementation of the
method or make this class abstract.
drkkbaseer@gmail.com 33
Cont..
Scenario: 1
Shape is the abstract class, its implementation is provided by
the Rectangle and Circle classes. Mostly, we don't know about
the implementation class (i.e. hidden to the end user) and
object of the implementation class is provided by the factory
method. draw()
Scenario: 2
Bank is the abstract class, its implementation is provided by
the ICICI and SBI classes. Return of interest
drkkbaseer@gmail.com 34
abstract class Shape{ Cont..
abstract void draw();
}
//In real scenario, implementation is provided by others i.e. unkno
wn by end user
class Rectangle extends Shape{
void draw(){System.out.println("drawing rectangle");}
}
class Circle1 extends Shape{
void draw(){System.out.println("drawing circle");}
}
//In real scenario, method is called by programmer or user
class TestAbstraction1{
public static void main(String args[]){
Shape s=new Circle1();
//In real scenario, object is provided through method e.g. getShape() method
s.draw();
}
}
drkkbaseer@gmail.com 35
abstract class Bank{ Cont..
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
drkkbaseer@gmail.com 36
UNIT-II
Chapter-I
Inheritance:
Inheritance basics
Super keyword
Multi-level hierarchy
Abstract classes
final keyword with inheritance.
drkkbaseer@gmail.com 37
Herbert Schildt, “Java the Complete Reference,” Oracle Press, Ninth Edition, 2014.
Final Keyword in Java
The final keyword in java is used to restrict the user. The java
final keyword can be used in many context. Final can be:
1. variable
2. method
3. class
drkkbaseer@gmail.com 38
Cont..
drkkbaseer@gmail.com 39
Cont..
Example of final method
class Bike{
final void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph"
);}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
} }
Test it Now
Output: Compile Time Error
Note: Using final keyword, prevent Method Overriding.
drkkbaseer@gmail.com 40
Cont..
Example of final class
final class Bike{}
Uses:
• If you make any variable as final, you cannot change the
value of final variable (It will be constant).
• If you make any method as final, you cannot override it.
• If you make any class as final, you cannot extend it.
drkkbaseer@gmail.com 41
Cont..
Is final method inherited?
Ans.) Yes, final method is inherited but you cannot override it.
For Example:
class Bike{
final void run(){
System.out.println("running...");}
}
class Honda2 extends Bike{
public static void main(String args[]){
new Honda2().run();
} }
Test it Now
Output: running...
drkkbaseer@gmail.com 42