CS270 Java 3 4
CS270 Java 3 4
What is Inheritance?
another class. For example, a child inherits the traits of his/her parents. With
inheritance, we can reuse the fields and methods of the existing class. Hence,
class.
facilitates you to reuse the fields and methods of the existing class
when you create a new class. You can use the same fields and
Advantages of Inheritance
maintenance costs.
extensibility.
Types of Inheritance in Java
On the basis of class, there can be three types of inheritance in java: single,
For Example:
Single Inheritance Example
example given below, Dog class inherits the Animal class, so there is the
single inheritance.
As you can see in the example given below, BabyDog class inherits the Dog
inheritance.
inheritance. In the example given below, Dog and Cat classes inherits the
Consider a scenario where A, B, and C are three classes. The C class inherits
A and B classes. If A and B classes have the same method and you call it
from child class object, there will be ambiguity to call the method of A or B
class.
Since compile-time errors are better than runtime errors, Java renders
Inheritance is the procedure in which one class inherits the attributes and
methods of another class. The class whose properties and methods are
inherited is known as the parent class or superclass. And the class that
inherits the properties from the parent class is the child class or derived class
Role of Constructors in inheritance
Constructor has the same name as the class name. A constructor doesn't have
class is created.
overridden.
1. Default constructor
2. Parameterized constructor
What is the order of execution of constructor in Java inheritance?
While implementing inheritance in a Java program, every class has its own
constructor. Therefore the execution of the constructors starts after the object
inheritance.
Constructors in Inheritance:
When a drived class is extended from the base class, the constructor of the
base class is executed first followed by the constructor of the derived class.
the order:
1. C1- Parent
2. C2 - Child
3. C3 - Grandchild
super(a, b) calls the constructor from the parent class which takes 2
variables
package com.company;
class Base1{
Base1(){
System.out.println("I am a constructor");
Base1(int x){
}}
In a class hierarchy, when a method in a subclass has the same name and
type signature as a method in its superclass, then the method in the subclass
is called from within a subclass, it will always refer to the version of that
method defined by the subclass. The version of the method defined by the
with the super key word and a dot (.). Consider the following:
Use of “super”
superclasses and subclasses that have methods with the same name.
Polymorphism in inheritance
As the name implies, polymorphism is the ability to take multiple forms or
allows you to treat objects that share the same superclass, whether directly or
Classes Dog, Fish, Bird, and Snake, for example, move differently even
though they all implement the move method in the superclass animal. The
animalMove() { System.out.println("Dog is
animalMove() { System.out.println("Fish is
{ System.out.println("Snake is crawling"); }}
To test whether the above code applies polymorphism, let’s use the primary
snake.animalMove();}
The following will be printed out on our console when we run this
application:
crawling
Types of polymorphism
There are two types of polymorphism, and they are listed below:
1. Compile-time polymorphism
2. Run-time polymorphism
Compile-time polymorphism
When the compiler encounters a method call, it checks the object’s type to
determine if it can make that call. The program is compiled if the class
Method overloading
This is a concept in which methods are declared with the same name but
Java provides various data types just like any other dynamic languages
such as boolean, char, int, unsigned int, signed int, float, double, long, etc
while storing in memory. When you assign a value of one data type to
another, the two types might not be compatible with each other. If the data
types are compatible, then Java will perform the conversion automatically
known as Automatic Type Conversion, and if not then they need to be cast
variable.
Widening or Automatic Type Conversion
Widening conversion takes place when two data types are automatically
Here, the target type specifies the desired type to convert the
While evaluating expressions, the intermediate value may exceed the range
package and user-defined package. There are many built-in packages such as
java, lang, awt, javax, swing, net, io, util, sql etc.
Here, we will have the detailed learning of creating and using user-defined
packages.
1) Java package is used to categorize the classes and interfaces so that they
1. //save as Simple.java
2. package mypack;
6. }
7. }
If you are not using any IDE, you need to follow the syntax given below:
For example
1. javac -d . Simple.java
The -d switch specifies the destination where to put the generated class file.
You can use any directory name like /home (in case of Linux), d:/abc (in
case of windows) etc. If you want to keep the package within the same
class.
Output:Welcome to package
The -d is a switch that tells the compiler where to put the class file i.e. it represents destina
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
1) Using packagename.*
If you use package.* then all the classes and interfaces of this package will
The import keyword is used to make the classes and interface of another
1. //save by A.java
2. package pack;
3. public class A{
5. }
1. //save by B.java
2. package mypack;
3. import pack.*;
4.
5. class B{
6. public static void main(String args[]){
8. obj.msg();
9. }
10. }
Output:Hello
If you use fully qualified name to import any class into your program, then
only that particular class of the package will be accessible in your program,
other classes in the same package will not be accessible. For this approach,
there is no need to use the import statement. But you will have to use the
fully qualified name every time you are accessing the class or the interface.
This is generally used when two packages have classes with same names.
Example
In this example, we are creating a class A in package pack and in another
System.out.println("Hello");
} }
obj.msg();
} }
Hello
Package can have many classes but sometimes we want to access only
specific class in our program in that case, Java allows us to specify class
Example:
In this example, we created a class Demo stored into pack package and in
another class Test, we are accessing Demo class by importing package name
System.out.println("Hello");
} }
obj.msg();
} }
Copy
Hello
this package will be accessible but the classes and interface inside
System.out.println("Hello");
} }
Second {
obj.msg();
} }
Hello