Lecture 5
Lecture 5
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
}