0% found this document useful (0 votes)
16 views14 pages

Lecture 5

The document discusses classes, objects, inheritance and different types of inheritance in Java. It explains that a class is a blueprint for creating objects in Java. Inheritance allows classes to acquire properties and behaviors of other classes. The key types of inheritance are single, multilevel, hierarchical and multiple inheritance, but Java only supports single inheritance through classes and multiple inheritance through interfaces.

Uploaded by

shivamsaxena3718
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)
16 views14 pages

Lecture 5

The document discusses classes, objects, inheritance and different types of inheritance in Java. It explains that a class is a blueprint for creating objects in Java. Inheritance allows classes to acquire properties and behaviors of other classes. The key types of inheritance are single, multilevel, hierarchical and multiple inheritance, but Java only supports single inheritance through classes and multiple inheritance through interfaces.

Uploaded by

shivamsaxena3718
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
You are on page 1/ 14

DR.

APJ ABDUL KALAM TECHNICAL UNIVERSITY

Branch- CSE
Web Technology
Lecture-5

Classes, Inheritance
By

Mr. Madhukar
Assistant Professor
Department of Information Technology
Krishna Engineering College, Ghaziabad
Classes in java
• Java is an object-oriented programming language. Everything in Java is
associated with classes and objects, along with its attributes and methods.
• A Class is like an object constructor, or a "blueprint" for creating objects.
Create a class
To create a class, use the keyword class:
Syntax: class className {
// body of class
}
Example : class online{
public static void main(String[] ar){}
}
Objects in Java
• Object is an instance of a class.
• An object is created from a class.
Syntax to create an object:
ClassName objectName= new className();
• New is a keyword to assign memory location.
• className() is a constructor of a class.
Example: : class online{
public static void main(String[] ar){
online on= new online();
}
}
First java Program
Steps to write java program
1. Create a class
2. Write main method
public static void main(String[] ar){
Body of main method
}
To compile: javac filename.java
To run: java className
• For public class: file name and class name must be same.
Parameters used in First Java Program
• class keyword is used to declare a class in Java.
• public keyword is an access modifier that represents visibility. It means it is visible
to all.
• static is a keyword. If we declare any method as static, it is known as the static
method. The core advantage of the static method is that there is no need to create
an object to invoke the static method. The main() method is executed by the JVM,
so it doesn't require creating an object to invoke the main() method. So, it saves
memory.
• void is the return type of the method. It means it doesn't return any value.
• main represents the starting point of the program.
• String[] ar or String ar[] is used for command line argument. We will discuss it in
coming section.
• System.out.println() is used to print statement. Here, System is a class, out is an
object of the PrintStream class, println() is a method of the PrintStream class. We
will discuss the internal working of System.out.println()statement in the coming
section.
Inheritance in Java
• Inheritance in Java is a mechanism in which one object acquires all the
properties and behaviours of a parent object. Or one class acquires the
properties and behaviours of another class.
• 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 the parent class.
• Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
• Why use inheritance in java?
1. For Method Overriding (so runtime polymorphism can be achieved).
2. For Code Reusability.
Inheritance in Java contd.
• Super Class/Parent Class: Superclass is the class from where a subclass
inherits the features. It is also called a base class or a parent class.
• Sub Class/Child Class: Subclass is a class which inherits the other class. It is
also called a derived class, extended class, or child class.
• Syntax:
Class subclassName extends SuperclassName{
//methods and fields
}

Note: extends keyword is used to inherit


• The extends keyword indicates that you are making a new class that derives
from an existing class. The meaning of "extends" is to increase the
functionality
Inheritance in Java contd.
• a class which is inherited is called a parent or superclass, and the new class is
called child or subclass.
Types of Inheritance in java:
On the basis of class, there can be three types of inheritance in java:
i. Single Inheritance
ii. Multilevel Inheritance
iii. Hierarchical Inheritance
But java has support of below Inheritance through interface only
iv. Multiple Inheritance: But java does not support multiple inheritance and
Hybrid inheritance to reduce complexity.
v. Hybrid Inheritance
Single Inheritance in java
• When a class inherits another class, it is known as a single inheritance.
• Example:
Class on {
Void method{
System.out.println (“this is method”);
}
Class off extends on {
System.out.println (“this is inherited”);
}
Multilevel Inheritance in Java
• When there is a chain of inheritance, it is known as multilevel inheritance.
• Example:
Class on {
Void method{
System.out.println (“this is method”);
}
Class off extends on {
System.out.println (“this is inherited”);
}
Class online extends off{
System.out.println(“this is multilevel”);
}
Hierarchical Inheritance in Java
• When two or more classes inherits a single class, it is known as hierarchical
inheritance.
• Example
Class on {
Void method{
System.out.println (“this is method”);
}
Class off extends on {
System.out.println (“this is inherited”);
}
Class online extends on{
System.out.println(“this is multilevel”);
}
Multiple Inheritance in Java
• When one class inherits multiple classes, it is known as multiple inheritance
Example:
Class on {
Void method{
System.out.println (“this is method”);
}
Class off {
System.out.println (“this is inherited”);
}
Class online extends off,on{ // suppose if it is true(but java does not support multiple
inheritance.
System.out.println(“this is multilevel”);
}
Thank You

You might also like