Unit Ii
Unit Ii
class Super{
..... .....
}
class Super1 extends Super
{
..... .....
}
C
• Hierarchical inheritance: refers to a child and
parent class relationship where more than one
classes extends the same class. For example,
Child 1 & Child 2 extends the same Parent.
void does(){
System.out.println("Teaching");
}}
System.out.println(obj.collegeName);
System.out.println(obj.designation);
System.out.println(obj.mainSubject);
obj.does();
}}
Multilevel inheritance
class Zahir{ public void distance()
int Za = 156556; {
int Ja = 80; System.out.println("Chennai to
void Zahir() Madurai Distance = " + Dis);
{ }
System.out.println("Zahir Roll No =" + Za); }
} public class ClassAdvi extends Parent{
public void mark()
{ public ClassAdvi()
System.out.println("Zahir Java Mark =" + Ja ); {
}} System.out.println("The Details of
class Parent extends Zahir{ zahir parent");
int Pr = 95956445; }
String Add = "North Street"; public static void main(String args[])
int Dis = 400; {
public Parent() ClassAdvi obj=new ClassAdvi();
{ obj.Zahir();
System.out.println("Zahir Parent Contact Details =" +obj.mark();
Pr);
} obj.address();
public void address() obj.distance();
{ }}
System.out.println("Parent Address =" + Add);
}
Hierarchical inheritance
class Car{
{ {
System.out.println("Class Car");
System.out.println("Max: 90Kmph");
}
}
}
public void vehicleType()
public class Maruti800 extends Maruti{
{
Usage
int Za = 156556;
void Zahir()
}}
obj.Zahir();
}}
Method Overloading
int za = 80;
void Zahir(int a)
void Zahir(String a)
{ Output
System.out.println("Zahir Information= " + z);
} Zahir Roll No = 80
} Zahir Information= I am Zahir
public class Test2 extends Zahir
obj.Zahir(1);
obj.Zahir("");
}}
Abstract Classes and Methods
• Data abstraction is the process of hiding certain details and
showing only essential information to the user.
»abstract (Keyword)
• To used for classes and methods:
• Abstract class: is a restricted class that cannot be used to create
if(a<100)
BCA (CTIS - B)
A grade
{
System.out.println("A grade");
• To create java program by using method
overriding and overloading and abstract
class and method.
• Multiple inheritance
Interfaces
• Interface in Java is a blueprint of a class.
https://www2.cs.duke.edu/csed/java/jdk1.5/docs/api/index.html
Scanner, To used to get user input
import java.util.Scanner;
import java.util.Scanner;
class MyClass {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
String userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}}
Access Modifiers
• java provides a number of access modifiers to set access levels
for classes, variables, methods, and constructors. The four
access levels are,
Public Method → Accessible everywhere.
Protected Method →Accessible within the same package
and subclasses.
Default Method→ Accessible only within the same package.
Private Method → Accessible only within the same class,
but accessed indirectly using accessPrivate().
2. Private (private)
• Accessible only within the same class.
• Used for encapsulation (hiding implementation
details).
class PrivateExample {
{ System.out.println("Private method");
}}
3. Protected (protected)
• Accessible within the same package and by subclasses in
different packages.
• Used to allow subclass access while restricting outside
access.
class ProtectedExample {
System.out.println("Protected method");
}}
4. Default (No Modifier)
• Also called package-private.
• Accessible only within the same package.
• If no access modifier is specified, the default is applied.
class DefaultExample {
void display()
}}