0% found this document useful (0 votes)
8 views22 pages

Java Class Concepts and Inheritance

The document outlines various Java programming concepts including classes, type casting, inheritance, polymorphism, encapsulation, and abstract classes. It provides definitions, properties, and examples for each concept, demonstrating how they are implemented in Java. The content is structured as a series of experiments for a B.Tech course in Information and Technology.

Uploaded by

09akshatmishra
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)
8 views22 pages

Java Class Concepts and Inheritance

The document outlines various Java programming concepts including classes, type casting, inheritance, polymorphism, encapsulation, and abstract classes. It provides definitions, properties, and examples for each concept, demonstrating how they are implemented in Java. The content is structured as a series of experiments for a B.Tech course in Information and Technology.

Uploaded by

09akshatmishra
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

Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore

Department of Information and Technology


Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

EXPERIMENT 1

Write a program to show concept of Class in java :

Java Classes

A class in Java is a set of objects which shares common characteristics/ behavior and common
properties/ attributes. It is a user-defined blueprint or prototype from which objects are created. For
example, Student is a class while a particular student named Ravi is an object.

Properties of Java Classes

1. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are
created.

2. Class does not occupy memory.

3. Class is a group of variables of different data types and a group of methods.

4. A Class in Java can contain:

 Data member

 Method

 Constructor

 Nested Class

 Interface

If you’re looking to gain a deeper understanding of Java and its object-oriented principles, exploring a
comprehensive can be a valuable step. It will help you master the core concepts of classes and objects,
and how to effectively use them in building robust Java applications

Class Declaration in Java

access_modifierclass<class_name>
{
datamember;
method;
constructor;
nested

Example of java class

// Java Program for class example

// data member (also instance variable)


1
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

class Student {

int id; // data member (also instance variable)

String name;

public static void main(String args[])

Student s1 = new Student(); // creating an object of Student

[Link]([Link]);

[Link]([Link]);

2
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

EXPERIMENT 2

Write a program showing Type Casting

Type Casting in Java

In Java, type casting is a method or process that converts a data type into another data type in both
ways manually and automatically. The automatic conversion is done by the compiler and manual
conversion performed by the programmer. In this section, we will discuss type casting and its
types with proper examples.

Types of Type Casting

There are two types of type casting:

o Widening Type Casting

o Narrowing Type Casting

Widening Type Casting

Converting a lower data type into a higher one is called widening type casting. It is also known
as implicit conversion or casting down. It is done automatically. It is safe because there is no chance to
lose data. It takes place when:

byte -> short -> char -> int -> long -> float -> double

Narrowing Type Casting

Converting a higher data type into a lower one is called narrowing type casting. It is also known
as explicit conversion or casting up. It is done manually by the programmer. If we do not perform
casting then the compiler reports a compile-time error.

double -> float -> long -> int -> char -> short -> byte

Example of type casting

public class TypeCasting

public static void main(String args[])

double d = 166.66;

//converting double data type into long data type

long l = (long)d;

//converting long data type into int data type

3
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

int i = (int)l;

[Link]("Before conversion: "+d);

//fractional part lost

[Link]("After conversion into long type: "+l);

//fractional part lost

[Link]("After conversion into int type: "+i);

4
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

EXPERIMENT 3

Write a program showing Different type of inheritance .

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a
parent object. It is an important part of OOPs (Object Oriented programming system).

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.
Moreover, you can add new methods and fields in your current class also.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Types of inheritance in java

On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.

In java programming, multiple and hybrid inheritance is supported through interface only. We will
learn about interfaces later.

Note: Multiple inheritance is not supported in Java through class.

The syntax of Java Inheritance

class Subclass-name extends Superclass-name

//methods and fields

EXAMPLE :

class Animal{

void eat(){[Link]("eating...");}

class Dog extends Animal{

5
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

void bark(){[Link]("barking...");}

class TestInheritance{

public static void main(String args[]){

Dog d=new Dog();

[Link]();

[Link]();

}}

6
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

EXPERIMENT 4

Write a program Showing Different type of Polymorphism:

Polymorphism is considered one of the important features of Object-Oriented Programming.


Polymorphism allows us to perform a single action in different ways. In other words, polymorphism
allows you to define one interface and have multiple implementations. The word “poly” means many
and “morphs” means forms, So it means many forms.

Polymorphism enables Java developers to create more flexible and reusable code. Understanding when
and how to use it can make your code more maintainable. The Java Programming Course offers
practical examples and projects that showcase the power of polymorphism in real-world applications.

Types of Java Polymorphism

In Java Polymorphism is mainly divided into two types:

 Compile-time Polymorphism

 Runtime Polymorphism

Compile-Time Polymorphism in Java

It is also known as static polymorphism. This type of polymorphism is achieved by function


overloading or operator overloading

Method Overloading

When there are multiple functions with the same name but different parameters then these functions
are said to be overloaded. Functions can be overloaded by changes in the number of arguments or/and
a change in the type of arguments.

Subtypes of Compile-time Polymorphism

1. Function Overloading

It is a feature in C++ where multiple functions can have the same name but with different parameter
lists. The compiler will decide which function to call based on the number and types of arguments
passed to the function.

2. Operator Overloading

It is a feature in C++ where the operators such as +, -, *, etc. can be given additional meanings when
applied to user-defined data types.

3. Template

it is a powerful feature in C++ that allows us to write generic functions and classes. A template is a
blueprint for creating a family of functions or classes

// Java program for Method Overloading by Using Different Numbers of Arguments

7
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

// Class 1 Helper class

class Helper { // Method 1 Multiplication of 2 numbers

static int Multiply(int a, int b){ // Return product

return a * b;

// Method 2 Multiplication of 3 numbers

static int Multiply(int a, int b, int c){ // Return product

return a * b * c;

class GFG { // Class 2 Main class

// Main driver method

public static void main(String[] args)

{ [Link]([Link](2, 4)); // Calling method by passing input as in


arguments

[Link]([Link](2, 7, 3));

Runtime Polymorphism in Java

It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden
method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding. Method
overriding, on the other hand, occurs when a derived class has a definition for one of the member
functions of the base class. That base function is said to be overridden

Subtype of Run-time Polymorphism

i. Virtual functions

It allows an object of a derived class to behave as if it were an object of the base class. The derived
class can override the virtual function of the base class to provide its own implementation. The
function call is resolved at runtime, depending on the actual type of the objec

// Java Program for Method Overriding

8
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

// Class 1 Helper class

class Parent {

void Print() // Method of parent class

[Link]("parent class"); // Print statement

}}

class subclass1 extends Parent { // Class 2 Helper class

void Print() { [Link]("subclass1"); }

class subclass2 extends Parent { // Class 3 Helper class

void Print()

[Link]("subclass2");

class GFG {

public static void main(String[] args)

Parent a;

a = new subclass1();

[Link]();

a = new subclass2();

[Link]();

9
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

10
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

EXPERIMENT 5

Write a program to Showing Encapsulation :

Encapsulation in Java is a fundamental concept in object-oriented programming (OOP) that refers to


the bundling of data and methods that operate on that data within a single unit, which is called a class
in Java. Java Encapsulation is a way of hiding the implementation details of a class from outside
access and only exposing a public interface that can be used to interact with the class.

In Java, encapsulation is achieved by declaring the instance variables of a class as private, which
means they can only be accessed within the class. To allow outside access to the instance variables,
public methods called getters and setters are defined, which are used to retrieve and modify the values
of the instance variables, respectively. By using getters and setters, the class can enforce its own data
validation rules and ensure that its internal state remains consistent.

// Java Program to demonstrate

// Java Encapsulation

class Person { // Person Class

private String name; // Encapsulating the name and age

private int age; // only approachable and used using

public String getName() { return name; } // methods defined

public void setName(String name) { [Link] = name; }

public int getAge() { return age; }

public void setAge(int age) { [Link] = age; }

public class Main { // Driver Class

// main function

public static void main(String[] args)

11
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

// person object created

Person person = new Person();

[Link]("John");

[Link](30);

// Using methods to get the values from the

// variables

[Link]("Name: " + [Link]());

[Link]("Age: " + [Link]());

12
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

EXPERIMENT 8

Write a program to showing Abstract class .

Java abstract class is a class that can not be instantiated by itself, it needs to be subclassed by another
class to use its properties. An abstract class is declared using the “abstract” keyword in its class
definition.

Abstract classes are a key component of OOP in Java, allowing you to define incomplete classes that
other classes can extend. For a deeper exploration of abstract classes and their applications, the Java
Programming Course provides detailed lessons with practical projects.

Illustration of Abstract class

Abstract class shap{

Int color;

Abstraction void main()

{ // statement }

// Abstract class

abstract class Sunstar {

abstract void printInfo();

} // Abstraction performed using extends

class Employee extends Sunstar {

void printInfo()

String name = "avinash";

int age = 21;

float salary = 222.2F;

[Link](name);

[Link](age);

[Link](salary);

13
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

// Base class

class Base {

public static void main(String args[])

Sunstar s = new Employee();

[Link]();

14
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

EXPERIMENT 7

Write a program to showing interface :

An interface in Java is a blueprint of a class. It has static constants and abstract methods.

The interface in Java is a mechanism to achieve abstraction. There can be only abstract methods in the
Java interface, not method body. It is used to achieve abstraction and multiple inheritance in Java.

In other words, you can say that interfaces can have abstract methods and variables. It cannot have a
method body

Syntax:

interface <interface_name>{

// declare constant fields

// declare methods that abstract

// by default.

Java Interface Example:

//Interface declaration: by first user

interface Drawable{

void draw();

class Rectangle implements Drawable{ //Implementation: by second user

public void draw(){[Link]("drawing rectangle");}

class Circle implements Drawable{

public void draw(){[Link]("drawing circle");}

//Using interface: by third user

class TestInterface1{

public static void main(String args[]){

Drawable d=new Circle();//In real scenario, object is provided by method e.g. getDrawable()

[Link](); }}
15
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

16
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

EXPERIMENT 6

Write a program to showing Abstraction .

In Java, abstraction is achieved by interfaces and abstract classes. We can achieve 100% abstraction
using interfaces.

Data Abstraction may also be defined as the process of identifying only the required characteristics of
an object ignoring the irrelevant details. The properties and behaviors of an object differentiate it from
other objects of similar type and also help in classifying/grouping the objects.

Java Abstraction Example

// Java program to illustrate the

// concept of Abstraction

abstract class Shape {

String color;

abstract double area();

public abstract String toString();

// abstract class can have the constructor

public Shape(String color)

[Link]("Shape constructor called");

[Link] = color;

public String getColor() { return color; }

class Circle extends Shape {

double radius;

public Circle(String color, double radius)

super(color);

[Link]("Circle constructor called");

[Link] = radius;

17
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

@Override double area()

return [Link] * [Link](radius, 2);

@Override public String toString()

return "Circle color is " + [Link]()

+ "and area is : " + area();

class Rectangle extends Shape {

double length;

double width;

public Rectangle(String color, double length,

double width)

super(color);

[Link]("Rectangle constructor called");

[Link] = length;

[Link] = width;

} @Override double area() { return length * width; }

@Override public String toString()

return "Rectangle color is " + [Link]()

+ "and area is : " + area();

}
18
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

public class Test {

public static void main(String[] args)

Shape s1 = new Circle("Red", 2.2);

Shape s2 = new Rectangle("Yellow", 2, 4);

[Link]([Link]());

[Link]([Link]());

19
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

EXPERIMENT 9

Write a program showing inner class

Java inner class or nested class is a class that is declared inside the class or [Link] use inner
classes to logically group classes and interfaces in one place to be more readable and maintainable
.Additionally, it can access all the members of the outer class, including private data members and
methods.

Java inner class Example :

class TestMemberOuter1{

private int data=30;

class Inner{

void msg(){[Link]("data is "+data);}

public static void main(String args[]){

TestMemberOuter1 obj=new TestMemberOuter1();

[Link] in=[Link] Inner();

[Link](); }}

20
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

EXPERIMENT 10

Write a Multithreaded program :

Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for
maximum utilization of CPU. Each part of such program is called a thread. So, threads are light-weight
processes within a process.

Threads can be created by using two mechanisms :

1. Extending the Thread class

2. Implementing the Runnable Interface

We create a class that extends the [Link] class. This class overrides the run() method
available in the Thread class. A thread begins its life inside run() method. We create an object of our
new class and call start() method to start the execution of a thread. Start() invokes the run() method
on the Thread object.

Example :-

// Java code for thread creation by extending

// the Thread class

class MultithreadingDemo extends Thread {

public void run()

try {

// Displaying the thread that is running

[Link](

"Thread " + [Link]().getId()

+ " is running");

catch (Exception e) {

// Throwing an exception

[Link]("Exception is caught");

}
21
Shri Vaishnav Vidhyapeeth Vishwavidhyalaya Indore
Department of Information and Technology
Branch:-[Link](ICS ) Session : July2024-December2024 student Name:-Shubham Vishwakarma
Subject code: BTIT307N Enrollment No.23100BTCSICS14483
Subject Name: Introduction to core java Class:- II Year/III Semester

// Main Class

public class Multithread {

public static void main(String[] args)

int n = 8; // Number of threads

for (int i = 0; i < n; i++) {

MultithreadingDemo object

= new MultithreadingDemo();

[Link]();

22

You might also like