0% found this document useful (0 votes)
26 views49 pages

Basics, OOP, and Interfaces

The document discusses Java programming concepts like classes, objects, inheritance, polymorphism, interfaces, packages and abstract classes. It provides examples of defining classes with methods and constructors. It also explains concepts like inheritance, polymorphism, abstraction, final methods and variables.
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)
26 views49 pages

Basics, OOP, and Interfaces

The document discusses Java programming concepts like classes, objects, inheritance, polymorphism, interfaces, packages and abstract classes. It provides examples of defining classes with methods and constructors. It also explains concepts like inheritance, polymorphism, abstraction, final methods and variables.
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/ 49

Day 2

What did we discuss yesterday?

• Course outline
• Programming basics
• Variables
• If, if-else, nested if, and if else ladder
Today

• Finish the core concepts

• Classes and objects


Switch Case

Checking for specific value instead of range.

Syntax
switch(variable){
case <value1>:{Statement 1; break;}
case <value 2> {Statement2; break;}
default: {statement 3; break;}
}
Switch case example
public static void main(String args[]){
char letter = 'B';
switch(letter){
case 'A': {
System.out.println("Excellent"); break;
}
case 'B': {
System.out.println("Good"); break;
}
case 'C': {
System.out.println("Average"); break;
}
case 'D': {
System.out.println("Pass"); break;
}
default:{
System.out.println("Failed"); break;
}
}
}
Conditional Expression

• <expression> ? <output for true>:<output for false>;

Example:
int discount = price>=10000 ? 10 : 5 ;

String result = marks>=40? “Passed” : “Failed”;


Iterative processes

• For loop
– if the number of iterations is known

• While loop
– until a condition is met

• Do-while loop
– run the code at least once before checking the condition
For loop

for (initialize; condition; modification){


statements;
}
For loop example

for (int rounds=1; rounds<=10; rounds++){


System.out.print(rounds+” “);
}

output: 1 2 3 4 5 6 7 8 9 10
For loop example
for (int rounds=1; rounds<=10; rounds++){
//print only the odd numbers
if(rounds%2 == 0){
continue;
}
System.out.print(round+” “);
}

output: 1 3 5 7 9
While loop

initialization;

while (condition){
statement
}
While loop
int steps = 1;
while(steps<=10){
System.out.print(steps+” “);
steps++;
}

output: 1 2 3 4 5 6 7 8 9 10
Do while loop

initialization;

do{
statement;
}while();
Do while loop
import java.util.Scanner;

char letter = ‘e’;


Scanner scannerObj = new Scanner(System.in);
do{
System.out.print(“Enter a letter : “);
letter = scannerObj.next.charAt(0);
}while(letter!=’e’);
Classes and Objects
Chapter 2
Class and objects

• Class is the blue print of the object


• Class can creates many instances, which is known as
objects of that class.
• Class can have member variable(s) and/or member
function(s)
Declaration of a class

class <Class Name> {


<visibility> <data type> <variable>;
...
<visibility> <return type> <method>();
}
Declaration of a class - Example
class Student {
private String name;

public void SetName(String name){


this.name = name;
}
public String GetName(){
return name;
}
}
Creating an object from a class

<Class Name> <Object name> = new <Class Name>();

Example:
Student studentOne = new Student();
Access members

• A member variable or method can be directly access if it


is public.
• In general, IDE will show the public member list once a
dot is placed after the object name.

• Syntax
– <Object Name>.<member variable>
– <Object Name>.<member function>()
Put all to gather
class Student{
....
}
public static void main(String args[]){
Student studentOne = new Student();
studentOne.SetName(“Peter”);
studentOne.GetName();
}
Visibility Controllers / Access Modifiers
public protected private

• Visible to its own class • Visible to its own class • Visible to its own class

• Can be inherited by sub class • Can be inherited by sub class • Cannot inherited by sub class

• Visible to outside classes • Not visible to outside classes • Not visible to outside classes
Constructor

• A method
• Name is equalent to the class name
• Do not return, no need to mention void
Constructor
class Student{
private String name;

public Student(){
this.name = “John”;
}
}
Constructor - overload
class Student{
private String name;

public Student(){
this.name = “John”;
}
public Student(String name){
this.name = name;
}
}
Constructor - overload
class SampleTwo{
public static void main(String args[]){
Student studentOne = new Student();
Student studentTwo = new Student(“David”);
System.out.println(studentOne.GetName());
System.out.println(studentTwo.GetName());
}
}
Static members

• A variable value common for all the instances of the class.

Example
class Employee{
private static int noOfLeaves = 10;
public static void SetNoOfLeaves(int leaves){
this.noOfLeaves = leaves;
}
}
Inheritance

• Possess the members from another class or classes.


• Type of inheritance
– Single
– Multiple
– Hierarchical
– Multilevel

• Multiple inheritance is generally implemented with interfaces in


Java.
Inheritance - Syntax
class <sub class name> extends <super class name>{
member variable;
member function();
}
Inheritance - Example
class Room{
protected float floorArea;
public void SetArea(float area) {
this.floorArea = area;
}
}
class BedRoom extends Room{
private float height;
public float Volumn(float height) {
return floorArea*height;
}
}
Inheritance - constructor

• The call to super class constructor must appear as the


first statement within the subclass constructor.

• Parameter in the super class must match the order and


type of the instance variable declared in the super class.
Inheritance - constructor example
class Room{ class BedRoom extends Room{
protected int length, width; protected int height;
public static void Room(int public static void BedRoom(int
x, int y){ x, int y, int z){
this.length = x; Room(x,y);
this.width = y; this.height = z;
} }
} }
Polymophism

• This means that the object can take many different forms.

• This allows the method to be in different forms at different


level.
Function Overloading vs Overriding

• Overloading
– Functions with same name but different input parameters

• Overriding
– Inherited class / Sub class redefine the function
Final methods, variables, and classes

• If a method, variable should not be override in the sub class,


• And, class should not be inherited further

– Declare it as final

Variable: final public int count;


Method: final public static void SetPrice(float
price){}
Class: final public class BedRoom(){}
Abstraction

• Can be applied on
– Classes
– Methods

• Only declares the class or method, but no definition is


provided.
• Inherited class SHOULD redefinition.
Abstraction - Example
abstract class Room{
abstract void DisplayName();
}

class BedRoom extends Room{


public void DisplayName(){
System.out.println(“Bed Room”);
}
}
Interfaces and Packages
Chapter 3
Interfaces

• Helping multiple inheritance


• It is just like the class BUT with
– abstract methods
– final variables
– constants
• Inherited class should define the methods
Interface - example
interface item{
static final int code = 1234;
static final String name = “Fan”;
void display();
}
Implement interface
class <class name> implements <interface name>{

Also

class <sub class name> extends <super class name>


implements <interface1>, <interface2>{

}
Extending interface
interface <interface1> extends <interface2>{
}

Example
interface itemConstant{
int code = 1234;
String name = “Fan”;
}
interface item extends itemConstant{
void Display();
}
Combine interfaces
interface itemConstant{
int code = 1234;
String name = “Fan”;
}

interface itemMethod{
void Display();
}

interface Item extends itemConstant, itemMethod{


}
Accessing interface variables
interface A{
int value = 10;
}

class B implements A{
int count = value;
void methodX(){
}
}
Example: Create multiple inheriance class

• Create a sub class Circle which inherites Center super


class and Radius interface.
• Center class has two private parameters: x and y. It also
has six public functions: SetX, SetY, GetX, GetY, SetXY,
and DisplayXY.
• Radius interface has one public variable called radius. It
has two functions public functions SetRadius and
GetRadius.
Center.java
public class Center { public int GetX(){
private int x; return x;
private int y; }
Center(){ public int GetY(){
x=0; return y;
y=0; }
} public void SetXY(int x, int y){
this.x = x;
Center(int x, int y){ this.y = y;
this.x = x; }
this.y = y; public void DisplayXY(){
} System.out.println("Center is at
public void SetX(int x){ ("+x+","+y+")");
this.x = x; }
} }
public void SetY(int y){
this.y = y;
}
Radius.java
public interface Radius {
int radius = 1;
public void SetRadius(int r);
public int GetRadius();
}
Circle.java
public class Circle extends Center public void SetRadius(int r){
implements Radius { circleRadius = r;
private int circleRadius = radius; }

public int GetRadius(){


Circle(){ return circleRadius;
System.out.println("Circle }
constructor");
} public void DisplayParameters(){
System.out.println("x = "+
Circle(int x, int y, int r){ GetX());
System.out.println("y = "+
SetX(x); GetY());
SetY(y); System.out.println("r = "+
circleRadius = r; circleRadius);
} }

}
InterfaceExample.java
public class InterfaceExample {
public static void main(String[] args) {
Circle circleOne = new Circle(10,30,20);
circleOne.DisplayParameters();
}
}

You might also like