0% found this document useful (0 votes)
2 views21 pages

8 Inheritance

Uploaded by

anovalexter
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

8 Inheritance

Uploaded by

anovalexter
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INHERITANCE

INHERITANCE
• Inheritance is the mechanism that permits new classes to be created
out of existing classes by extending and refining its capabilities. The
existing classes are called the base classes/parent classes/super-
classes, and the new classes are called the derived classes/child
classes/subclasses. The subclass can inherit or derive the attributes
and methods of the super-class(es) provided that the super-class
allows so. Besides, the subclass may add its own attributes and
methods and may modify any of the super-class methods. Inheritance
defines an “is – a” relationship.
TYPES OF INHERITANCE
• Single Inheritance − A subclass derives from a single super-class.
• Multiple Inheritance − A subclass derives from more than one super-
classes.
• Multilevel Inheritance − A subclass derives from a super-class which
in turn is derived from another class and so on.
• Hierarchical Inheritance − A class has a number of subclasses each of
which may have subsequent subclasses, continuing for a number of
levels, so as to form a tree structure.
• Hybrid Inheritance − A combination of multiple and multilevel
inheritance so as to form a lattice structure.
TYPES OF INHERITANCE
POLYMORPHISM
Polymorphism is originally a Greek word that means the ability to take
multiple forms. In object-oriented paradigm, polymorphism implies
using operations in different ways, depending upon the instance they
are operating upon. Polymorphism allows objects with different
internal structures to have a common external interface.
Polymorphism is particularly effective while implementing inheritance.
For example consider the operation of addition. For two numbers, the
operation will generate a sum. If the operands are strings, then the
operation would produce a third string by concatenation.
POLYMORPHISM
Java "Hello, World!" Program
// Your First Program
class HelloWorld
{
public static void main(String[] args)
{
[Link]("Hello, World!");
}}
If you have copied the exact code, you need to save the file name as
[Link]. It's because the name of the class and filename should match in
Java.
• In Java, any line starting with// is a comment
• In Java, every application begins with a class definition
POLYMORPHISM
public static void main(String[] args) { ... } • static is a keyword. If we declare any method
as static, it is known as the static method. The
This is the main method. Every application in core advantage of the static method is that
Java must contain the main method. The Java there is no need to create an object to invoke
compiler starts executing the code from the the static method. The main method is
main method. executed by the JVM, so it doesn't require to
create an object to invoke the main method.
So it saves memory.
Parameters used in First Java Program
• void is the return type of the method. It means
Let's see what is the meaning of class, public, it doesn't return any value.
static, void, main, String[], [Link]().
• main represents the starting point of the
program.
• class keyword is used to declare a class in java. • String[] args is used for command line
• public keyword is an access modifier which argument.
represents visibility. It means it is visible to all. • [Link]() is used to print
statement. Here, System is a class, out is the
object of PrintStream class, println() is the
method of PrintStream class.
POLYMORPHISM
How to set path in Java
• The path is required to be set for using tools such as javac, java, etc.
• If you are saving the Java source file inside the JDK/bin directory, the path is not
required to be set because all the tools will be available in the current directory.
• However, if you have your Java file outside the JDK/bin folder, it is necessary to
set the path of JDK.
How to set the Path of JDK in Windows
• To set the path of JDK, you need to follow the following steps:
• Open the command prompt
• Copy the path of the JDK/bin directory
• Write in command prompt: set path=copied_path
For Example:
• set path=C:\Program Files\Java\jdk1.6.0_23\bin
POLYMORPHISM
The command-line arguments in Java allow us to pass arguments during the execution of the
program.
Example
The following program displays all of the command-line arguments that it is called with -
public class CommandLine
{
public static void main(String args[])
{
for(int i = 0; i<[Link]; i++)
{
[Link]("args[" + i + "]: " + args[i]);
}
}
}
POLYMORPHISM
Try executing this program as shown here –
$java CommandLine this is a command line 200 -100
Output
This will produce the following result -
args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100
JDK COMPONENTS
• appletviewer: This tool is used to run and debug Java applets
without a web browser.
• java:The loader for Java applications. This tool is an interpreter and
can interpret the class files generated by the javac compiler.
• javac:It specifies the Java compiler, which converts source code into
Java bytecode.
• javadoc:The documentation generator, which automatically
generates documentation from source code comments.
• javap:the class file disassembler.
• JConsole:Java Monitoring and Management Console.
• jdb:Java debugger, which helps us to find errors in our programs.
JDK COMPONENTS
An application programming interface (API), in the context of Java, is a collection of
prewritten packages, classes, and interfaces with their respective methods, fields and
constructors. Similar to a user interface, which facilitates interaction between humans
and computers, an API serves as a software program interface facilitating interaction.
• Language Support package: A collection of classes and method required for
implementing basic features of java.
• Utility Package: A collection of classes to provide utility functions such as date and time
function.
• Input/output Package: A Collection of classes required for input/output manipulation.
• Networking package: A collection of classes for communicating with other computers
via internet.
• AWT package: The Abstract Window Tool kit package contain classes that implement
platform-independent graphical user interface.
• Applet package: This include a set of classes that allow us to create Java applet
COPE OF VARIABLE IN JAVA
Java variables are actually classified into three kinds –
• Instance variable
• Class variable
• Local variable
Instance variable and class variable are declared inside a class. Instance variable
are created when the objects are instantiated and therefore they are associated
with the objects. They take different values for each object.
On the other hand, class variables are global to a class and belong to the entire
set of objects that class creates. Only one memory location is created for each
class variable.
Variable declared and used inside methods are called local variables. They are
called so because they are not available for use outside the method definition.
COPE OF VARIABLE IN JAVA
• Type casting: the process of converting one data type to another is called casting. Casting is
often necessary when a method return a type different from the one we require.
e.g. malloc()
The process of assigning a smaller type to a larger one is known as widening or promotion and
that of assigning a larger type to a smaller one is known as narrowing. Note that narrowing may
result in loss of information.

• Increment and decrement operator:


m=5;
y=++m; in this case y and m would be 6.
m=5;
y=m++; in this case y would be 5 and m would be 6.
INSTANCEOF OPERATOR
• Instanceof operator:
The instanceof is an object reference operator and return
true if the object on the left-hand side is an instance of the
class given on the right-hand side. This operator allows us to
determine whether the object belongs to a particular class
or not.
Example: person instanceof student
Is true if the object person belong to the class student;
otherwise it is false
JAVE ABSTRACT CLASSES AND METHODS
Java Abstract Classes and Methods
• Data abstraction is the process of hiding certain details and showing only
essential information to the user.
Abstraction can be achieved with either abstract classes or interfaces.
The abstract keyword is a non-access modifier, used for classes and
methods:
• Abstract class: is a restricted class that cannot be used to create objects
(to access it, it must be inherited from another class).

• Abstract method: can only be used in an abstract class, and it does not
have a body. The body is provided by the subclass (inherited from).
• An abstract class can have both abstract and regular methods:
JAVE ABSTRACT CLASSES AND METHODS
abstract class Animal
{
public abstract void animalSound();
public void sleep()
{
[Link]("Zzz");
}
}

From the example above, it is not possible to create an object of the Animal class:
Animal myObj = new Animal(); // will generate an error
To access the abstract class, it must be inherited from another class.
JAVE ABSTRACT CLASSES AND METHODS
// Abstract class
[Link]("The pig says: wee wee");
abstract class Animal
{ }
public abstract void animalSound(); // Abstract }
method (does not have a body)
class MyMainClass
public void sleep() // Regular method
{ {
[Link]("Zzz"); public static void main(String[] args)
} {
}
Pig myPig = new Pig(); // Create a Pig object
class Pig extends Animal // Subclass (inherit from
Animal) [Link]();
{ [Link]();
public void animalSound()
} }
{
Why And When To Use Abstract Classes and Methods?
• To achieve security - hide certain details and only show the important details of an object.
• This allows us to manage complexity by omitting or hiding details with a simpler, higher-
level idea.
Points to Remember:
• An abstract class must be declared with an abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• To implement features of an abstract class, we inherit subclasses from it and create
objects of the subclass.
• It can have final methods which will force the subclass not to change the body of the
method.
• If subclass does not implement all the abstract methods of the superclass, then subclass
may be declare abstract.
Why can’t we create the object of an abstract class?
• Because these classes are incomplete, they have abstract methods that have
no body so if java allows you to create object of this class then if someone
calls the abstract method using that object then What would happen? There
would be no actual implementation of the method to invoke.
Also because an object is concrete. An abstract class is like a template, so
you have to extend it and build on it before you can use it.
Why can’t we create the object of an abstract class?
class Rectangle public static void main(String args[])
{ {
int length,width; int area1, area2;
void getData(int x,int y) Rectangle rect1=new Rectangle();
{ Rectangle rect2=new Rectangle();
length=x; width=y; [Link]=15;
} [Link]=10;
int rectArea() area1=[Link]*[Link];
{ int area=length*width; [Link](20,12);
return(area); area2=[Link]();
[Link](“Area1 =“ + area1);
} }
[Link](“Area2 =“ + area2);
class RectArea()
}
{
}

You might also like